Android无需权限保存文件,即使使用用户权限,也无法在外部存储上保存文件[Android]...
我正在為
Android上的圖像處理開發一個應用程序,但我仍然在編寫用于保存圖像的代碼.
這是我使用的方法:
private void saveImageToExternalStorage(Bitmap finalBitmap) {
String root = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).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);
finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
}
catch (Exception e) {
e.printStackTrace();
}
// Tell the media scanner about the new file so that it is
// immediately available to the user.
MediaScannerConnection.scanFile(this, new String[]{file.toString()}, null,
new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
Log.i("ExternalStorage", "Scanned " + path + ":");
Log.i("ExternalStorage", "-> uri=" + uri);
}
});
}
每次執行此操作時,FileOutputStream都會給出FileNotFoundException.
我的清單:
package="com.example.gabrielerestuccia.testimmagini">
...
我正在Linux上的Android Studio 1.5.1上開發這個,使用內置的虛擬Nexus 5和Android 6.0.
謝謝您的幫助.
總結
以上是生活随笔為你收集整理的Android无需权限保存文件,即使使用用户权限,也无法在外部存储上保存文件[Android]...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 欧陆战争5终极阵容是什么(如何评价宾利欧
- 下一篇: glide缩略图存储 android,G