保存的ImageView到Android模拟器图库 [英] Save ImageView to Android Emulator Gallery

查看:223
本文介绍了保存的ImageView到Android模拟器图库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想保存图像到Android画廊,这是我目前的code:

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

然后一个IOException:

Then an IOException:

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

这是一个4.0 Android虚拟设备。 我试图使用 Environment.getRootDirectory()获得AVD还的根目录下,但我仍然收到了同样的错误。

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.

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

What is the correct way to test saving an image to gallery in an AVD?

推荐答案

一直以来,你已经拥有了位图您可以使用此code,以将其插入画廊:

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.

这篇关于保存的ImageView到Android模拟器图库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆