存储位图图像到SD卡中的Android [英] Store Bitmap image to SD Card in Android
问题描述
我面临着一些奇怪的问题,我的Android code, 我有位图变的图像,并要保存该文件到SD卡。 我code如下,
I am facing some strange problem with my android code, I have an image in Bitmap variable and want to save that file to SD card. I code as follow,
Bitmap IMAGE // Loaded from internet servers.;
try {
File _sdCard = Environment.getExternalStorageDirectory();
File _picDir = new File(_sdCard, "MyDirectory");
_picDir.mkdirs();
File _picFile = new File(_picDir, "MyImage.jpg");
FileOutputStream _fos = new FileOutputStream(_picFile);
IMAGE.compress(Bitmap.CompressFormat.JPEG, 100, _fos);
_fos.flush();
_fos.close();
Toast.makeText(this, "Image Downloaded", 7000).show();
} catch (Exception ex) {
ex.printStackTrace();
Toast.makeText(this, ex.getMessage(), 7000).show();
}
我使用索尼Experia还电弧作为我的测试装置,当手机连接到我的电脑中,code工作不错,它存储的图像,并显示在画廊。但是,当我拔下电话从我的电脑和测试应用程序,它不保存图片,并没有表现出任何异常。
I am using Sony Experia Arc as my testing device, when the phone is connected to my computer, the code works nice, it stores image and also displays in gallery. But when I disconnect phone from my computer and test the app, it doesn't save picture and doesn't show any exception.
推荐答案
使用此功能
void saveImage() {
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/saved_images");
String fname = "Image.jpg";
File file = new File (myDir, fname);
if (file.exists ()) file.delete ();
try {
FileOutputStream out = new FileOutputStream(file);
myBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
检查这个答案将提供更多的细节 Android的文件保存到外部存储
Check this answer will give more details Android saving file to external storage
这篇关于存储位图图像到SD卡中的Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!