我想将图像文件保存在android中,系统库可以查看或删除它。我使用以下代码,但发现图库找不到它,更不用说查看或删除它了。有什么建议吗?
private void saveImage(Bitmap finalBitmap){
String root = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString();
File myDir = new File(root + getString(R.string.folderName));
myDir.mkdirs();
Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
String fname = "Image-"+ n +".png";
File file = new File (myDir, fname);
if (file.exists ()) file.delete();
try {
FileOutputStream out = new FileOutputStream(file);
finalBitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
out.flush();
out.close();
sendBroadcast(new Intent(
Intent.ACTION_MEDIA_MOUNTED,
Uri.parse("file://" + Environment.getExternalStorageDirectory())));
} catch (Exception e) {
e.printStackTrace();
}
}
只需更改此行:
sendBroadcast(new Intent(
Intent.ACTION_MEDIA_MOUNTED,
Uri.parse("file://" + Environment.getExternalStorageDirectory())));
至:
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,
Uri.parse("file://"+ Environment.getExternalStorageDirectory())));
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句