How to Save a PictureDrawable as a JPEG/PNG file in Android?
-
I have PictureDrawable that I wish to save as an image (JPEG/PNG) but I can't seem to find any information how to go about this. I tried this but it does not seem to work
PictureDrawable myDrawable = GetPictureDrawable();
Bitmap bitmap = Bitmap.createBitmap(200, 200, Bitmap.Config.ARGB_8888);
myDrawable.draw(new Canvas(bitmap));
bitmap.compress(CompressFormat.JPEG, 100, new FileOutputStream("/MyLocation/MyImage.jpg"));What am I doing wrongly?
www.belfox.net ...Simplicity at its best
-
I have PictureDrawable that I wish to save as an image (JPEG/PNG) but I can't seem to find any information how to go about this. I tried this but it does not seem to work
PictureDrawable myDrawable = GetPictureDrawable();
Bitmap bitmap = Bitmap.createBitmap(200, 200, Bitmap.Config.ARGB_8888);
myDrawable.draw(new Canvas(bitmap));
bitmap.compress(CompressFormat.JPEG, 100, new FileOutputStream("/MyLocation/MyImage.jpg"));What am I doing wrongly?
www.belfox.net ...Simplicity at its best
as PictureDrawable can be instanated usind a picture, it should be fairly possible to convert it back. 2D Graphics @ android Devguide[^] Documentation on android.graphics.PictureDrawable[^] That one states a function
getPicture()
that returns a Picture Documentation on android.graphics.Picture[^] that one has a functionwriteToStream()
- should be worth some try? regards TorstenI never finish anyth...
-
I have PictureDrawable that I wish to save as an image (JPEG/PNG) but I can't seem to find any information how to go about this. I tried this but it does not seem to work
PictureDrawable myDrawable = GetPictureDrawable();
Bitmap bitmap = Bitmap.createBitmap(200, 200, Bitmap.Config.ARGB_8888);
myDrawable.draw(new Canvas(bitmap));
bitmap.compress(CompressFormat.JPEG, 100, new FileOutputStream("/MyLocation/MyImage.jpg"));What am I doing wrongly?
www.belfox.net ...Simplicity at its best
public static void savePic(Bitmap bitmap, String saveFilePath) throws Exception {
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(saveFilePath));
bitmap.compress(Bitmap.CompressFormat.JPEG, 80, bos);
bos.flush();
bos.close();
}<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
add in the file "AndroidManifest.xml" 3.
Bitmap bitmap=BitmapFactory.decodeResource(this.getResources(), R.drawable.g7);
try {
savePic(bitmap, "/mnt/sdcard/test.jpg");
} catch (Exception e) {
e.printStackTrace();
}modified on Friday, June 3, 2011 1:22 AM
-
public static void savePic(Bitmap bitmap, String saveFilePath) throws Exception {
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(saveFilePath));
bitmap.compress(Bitmap.CompressFormat.JPEG, 80, bos);
bos.flush();
bos.close();
}<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
add in the file "AndroidManifest.xml" 3.
Bitmap bitmap=BitmapFactory.decodeResource(this.getResources(), R.drawable.g7);
try {
savePic(bitmap, "/mnt/sdcard/test.jpg");
} catch (Exception e) {
e.printStackTrace();
}modified on Friday, June 3, 2011 1:22 AM
Your answer shows how to save a Bitmap to a the SDCard. However, my question was how to save a PictureDrawable to the memory card. I needed to know how to convert the PictureDrawable to a Bitmap.
www.belfox.net ...Simplicity at its best
-
Your answer shows how to save a Bitmap to a the SDCard. However, my question was how to save a PictureDrawable to the memory card. I needed to know how to convert the PictureDrawable to a Bitmap.
www.belfox.net ...Simplicity at its best
private static Bitmap pictureDrawable2Bitmap(PictureDrawable pictureDrawable){
Bitmap bitmap=Bitmap.createBitmap(pictureDrawable.getIntrinsicWidth(),pictureDrawable.getIntrinsicHeight(), Config.ARGB_8888);
Canvas canvas=new Canvas(bitmap);
canvas.drawPicture(pictureDrawable.getPicture());
return bitmap;
}