Save ImageView to Android Emulator Gallery

三世轮回 提交于 2020-01-11 12:34:07

问题


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?


回答1:


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!