I've made an application with an ImageView that displays a Bitmap drawn by the user and everything goes well, but now I want to give the possibility to the user to save the image from the ImageView to a jpeg file when I click on a button.
How can I do this?
Here is my code:
ImageView imageview;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
imageview=(ImageView)findViewById(R.id.imageviewcomponent);
imageview.setDrawingCacheEnabled(true);
}
//This method sets the Bitmap into the ImageView from the ActivityResult of the Drawing Activity
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == get_code && resultCode == RESULT_OK)
{
iv.setImageBitmap((Bitmap) data.getParcelableExtra("Painting"));
}
}
//This is the onClick method of the button for saving the image
public void SaveAsImage(View btsave) throws FileNotFoundException {
Bitmap b=Bitmap.createBitmap(imageview.getWidth(), imageview.getHeight(), Bitmap.Config.ARGB_8888);
OutputStream fOut = null;
new File("/sdcard/signatures").mkdir();
fOut = new FileOutputStream("/sdcard/myPaintings/image.jpg");
b.compress(CompressFormat.JPEG, 95, fOut);
}
The code for creating the folder and save the image is working, because I can find an image.jpg file in the myPainting folder, but the image's content is completely black, without the image drawn by the user.
How can I get the content of the ImageView? (rather in Bitmap format then other things)
Try this code and let me know what happen..
//This is the onClick method of the button for saving the image
public void SaveAsImage(View btsave) throws FileNotFoundException {
Bitmap b = Bitmap.createBitmap(imageview.getWidth(), imageview.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(b);
imageview.draw(canvas);
OutputStream fOut = null;
new File("/sdcard/signatures").mkdir();
fOut = new FileOutputStream("/sdcard/myPaintings/image.jpg");
b.compress(CompressFormat.JPEG, 95, fOut);
}
A slighly modified version of user370305 code should also work
static void saveDrawable(ImageView imageView) throws FileNotFoundException{
Drawable drawable = imageView.getDrawable();
Rect bounds = drawable.getBounds();
Bitmap bitmap = Bitmap.createBitmap(bounds.width(),bounds.height(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
drawable.draw(canvas);
OutputStream fOut = null;
try {
new File("/sdcard/signatures").mkdir();
fOut = new FileOutputStream("/sdcard/myPaintings/image.jpg");
bitmap.compress(Bitmap.CompressFormat.JPEG, 95, fOut);
} finally {
if ( fOut != null ){
try {
fOut.close();
} catch (IOException e) {
//report error
}
}
}
}
Imageview internally use ScaleType.
it will scale your image according the the pixels of the screen,if you want to get the original quality of the image,use drawing cache,
public void SaveAsImage(Holder imageview) throws FileNotFoundException {
View view=findViewById(R.id.holder_frame);
Bitmap bmp=viewToBitmap(imageview);
imageview.setDrawingCacheEnabled(true);
imageview.buildDrawingCache(true);
Bitmap bitmap1=imageview.getDrawingCache();
OutputStream fOut = null;
new File("/sdcard/Pic").mkdir();
fOut = new FileOutputStream("/sdcard/Pic/image4.jpg");
bitmap1.compress(Bitmap.CompressFormat.JPEG, 100, fOut);
imageview.setDrawingCacheEnabled(false);
imageview.destroyDrawingCache();
}