您好我正在尝试在将过滤器应用到SD卡或图库后保存新的位图图像。我正在使用此代码保存我在堆栈溢出时找到的位图,但此代码对我不起作用
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();
}
请告诉我如果我做错了什么? 提前谢谢。
答案 0 :(得分:1)
确保您已添加写入sdcard的权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>