【问题标题】:Save ImageView to Android Emulator Gallery将 ImageView 保存到 Android 模拟器库
【发布时间】:2012-04-28 09:28:05
【问题描述】:

我想将图像保存到 Android 图库,这是我当前的代码:

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();

它总是返回相同的错误:

Error: /Pictures Dont exist

然后是一个 IOException:

java.io.IOException: open failed: ENOENT (No such file or directory)

这是在 4.0 Android 虚拟设备上。 我也尝试使用Environment.getRootDirectory() 来获取 AVD 的根目录,但仍然收到相同的错误。

在 AVD 中测试将图像保存到图库的正确方法是什么?

【问题讨论】:

    标签: android file-io io android-imageview android-gallery


    【解决方案1】:

    由于您已经拥有Bitmap,您可以使用此代码将其插入图库:

    Bitmap b = image.getDrawingCache();
    Images.Media.insertImage(getContentResolver(), b, title, description);
    

    titledescription 如果您不关心设置它们,则可以为 null。如果返回的Uri非空,则插入成功。

    【讨论】:

    • 我看到的每个关于将图像保存到图库的教程都没有提到这种方法!该死的,这一切都是那么容易!谢谢。
    猜你喜欢
    • 2015-09-27
    • 2013-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-14
    • 1970-01-01
    • 1970-01-01
    • 2016-01-08
    相关资源
    最近更新 更多