Moe
Moe

Reputation: 4792

Save ImageView to Android Emulator Gallery

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?

Upvotes: 1

Views: 2278

Answers (1)

Jason Robinson
Jason Robinson

Reputation: 31283

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.

Upvotes: 4

Related Questions