无法保存位图图像
Unable to save Bitmap Image
您好,我正在尝试将滤镜应用到 SD 卡或画廊后保存我的新位图图像。我也在使用这段代码来保存我在 Stack overflow 上找到的位图,但这段代码对我不起作用
public void saveImage(View view) {
/*MediaStore.Images.Media.insertImage(getContentResolver(),newBitmap,"Image-Name","description");*/
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/saved_images");
myDir.mkdirs();
Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
String fname = "Image-"+ n +".jpg";
File file = new File (myDir, fname);
if (file.exists ()) file.delete ();
try {
FileOutputStream out = new FileOutputStream(file);
newBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
Toast.makeText(this,"Image Saved In SD Card",Toast.LENGTH_LONG).show();
}
如果我做错了什么,请告诉我?
提前致谢。
确保您已添加写入 sdcard 的权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
您好,我正在尝试将滤镜应用到 SD 卡或画廊后保存我的新位图图像。我也在使用这段代码来保存我在 Stack overflow 上找到的位图,但这段代码对我不起作用
public void saveImage(View view) {
/*MediaStore.Images.Media.insertImage(getContentResolver(),newBitmap,"Image-Name","description");*/
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/saved_images");
myDir.mkdirs();
Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
String fname = "Image-"+ n +".jpg";
File file = new File (myDir, fname);
if (file.exists ()) file.delete ();
try {
FileOutputStream out = new FileOutputStream(file);
newBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
Toast.makeText(this,"Image Saved In SD Card",Toast.LENGTH_LONG).show();
}
如果我做错了什么,请告诉我? 提前致谢。
确保您已添加写入 sdcard 的权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>