Bha*_*try 5 android bitmap sd-card
我的Android代码面临一些奇怪的问题,我在Bitmap变量中有一个图像,并希望将该文件保存到SD卡.我编码如下,
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();
}
Run Code Online (Sandbox Code Playgroud)
我正在使用Sony Experia Arc作为我的测试设备,当手机连接到我的电脑时,代码效果很好,它存储图像并显示在图库中.但是,当我从我的电脑断开手机并测试应用程序时,它不会保存图片,也不会显示任何异常.
使用此功能
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();
}
}
Run Code Online (Sandbox Code Playgroud)
检查此答案将提供更多详细信息Android保存文件到外部存储
| 归档时间: |
|
| 查看次数: |
11947 次 |
| 最近记录: |