I'd like to Save an Image to the Android Gallery, here's my current code:
image.setDrawingCacheEnabled(true);
image.buildDrawingCache(true);
Bitmap b = image.getDrawingCache();
if(!new File("/"+Environment.DIRECTORY_PICTURES).exists())
Log.e("Error","/"+Environment.DIRECTORY_PICTURES+" Dont exist");
File file = new File(Environment.DIRECTORY_PICTURES+"/myImage.jpg");
file.createNewFile();
FileOutputStream ostream = new FileOutputStream(file);
b.compress(CompressFormat.JPEG, 80, ostream);
image.setDrawingCacheEnabled(false);
ostream.close();
Toast.makeText(Ads.this, "Offer saved", 1).show();
It always returns the same error:
Error: /Pictures Dont exist
Then an IOException:
java.io.IOException: open failed: ENOENT (No such file or directory)
This is on a 4.0 Android Virtual Device.
I've tried to use Environment.getRootDirectory()
to get the root directory of the AVD also, but I still receive the same errors.
What is the correct way to test saving an image to gallery in an AVD?
Since, you already have the Bitmap
you can use this code to insert it into the gallery:
Bitmap b = image.getDrawingCache();
Images.Media.insertImage(getContentResolver(), b, title, description);
title
and description
can be null if you don't care about setting them. If the Uri
returned is non-null, then the insertion was successful.
来源:https://stackoverflow.com/questions/10362259/save-imageview-to-android-emulator-gallery