Saving drawable as file: Getting black image

All your problems with Audio, Video and Images.

Saving drawable as file: Getting black image

Postby EuroBoilermaker » Mon Oct 18, 2010 3:32 am

Hi,

I'm trying to create a jpeg file from a drawable. For some reason, I keep on getting black images, as if the information didn't saved properly.

My code:
Code: Select all
Drawable image_saved = getResources().getDrawable(R.drawable.icon);
imgview.setImageDrawable(image_saved);
GPS_map = Bitmap.createBitmap(image_saved.getIntrinsicWidth(),image_saved.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);


I then pass the bitmap to an instance of a second class, and there I use the following method:

Code: Select all
public String getFilePath(Bitmap img, String fileName) {
//source: http://android-er.blogspot.com/2010/07/save-file-to-sd-card.html
//source: http://stackoverflow.com/questions/649154/android-bitmap-save-to-location
            
OutputStream fOut = null;
String filePath = null;
         
try {
   String path = Environment.getExternalStorageDirectory().toString();
        File file = new File(path, fileName);
         
   fOut = new FileOutputStream(file);
            
   img.compress(Bitmap.CompressFormat.JPEG, 100, fOut);
            
   fOut.flush();
   fOut.close();
   filePath = file.getPath();
} catch (FileNotFoundException e) {
   fileName = e.getMessage();
} catch (IOException e) {
         
}
         
return filePath;
      
}
EuroBoilermaker
Freshman
Freshman
 
Posts: 5
Joined: Tue Feb 23, 2010 8:55 am

Top

Re: Saving drawable as file: Getting black image

Postby EuroBoilermaker » Mon Oct 18, 2010 8:09 pm

I found out I did do something wrong, I forgot to add code to create a canvas and put the drawable/view into the canvas. However I'm stilling getting a blank image.

Code: Select all
Resources res = getResources();
      Drawable image_saved = getResources().getDrawable(R.drawable.icon);
      imgview.setImageDrawable(image_saved);
      GPS_map = Bitmap.createBitmap(image_saved.getIntrinsicWidth(),image_saved.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
      Canvas canvas = new Canvas(GPS_map);
      image_saved.draw(canvas);


Am I forgetting something?
EuroBoilermaker
Freshman
Freshman
 
Posts: 5
Joined: Tue Feb 23, 2010 8:55 am

Re: Saving drawable as file: Getting black image

Postby tim.choi » Thu Oct 21, 2010 5:18 pm

Hmm.... are u looking for a way to get a drawable from resource and then save it as jpeg in the filesystem? If so, I am afraid that you are making some code more complicated than you want.

That's all as below and can do what you want.

Code: Select all

Bitmap image_saved=BitmapFactory.decodeResource(context.getResources(),R.drawable.icon);

FileOutputStream fOut=new FileOutputStream(path+"/"+fileName);
image_saved.compress(Bitmap.CompressFormat.JPEG,100,fOut);
fOut.flush();fOut.close();
/* I neglected all try-catch commands, the editor will remind you to add at the suitable location. */



Anyway, I think u get confused about the usage of setImageDrawable and Canvas.
tim.choi
Developer
Developer
 
Posts: 25
Joined: Fri Feb 19, 2010 4:30 pm

Re: Saving drawable as file: Getting black image

Postby tim.choi » Thu Oct 21, 2010 5:22 pm

remind u to do "image_saved.recycle()" if you no longer need it after saved.
tim.choi
Developer
Developer
 
Posts: 25
Joined: Fri Feb 19, 2010 4:30 pm

Re: Saving drawable as file: Getting black image

Postby ckiyouy » Sat Oct 30, 2010 3:24 am

remind u to do "image_saved.recycle()" if you no longer need it after saved.
ckiyouy
Freshman
Freshman
 
Posts: 2
Joined: Sat Oct 30, 2010 3:10 am

Top

Return to Multimedia Problems

Who is online

Users browsing this forum: No registered users and 18 guests