英文:
Unable to save file to an external storage directory in android
问题
I have looked at all of the questions related to saving a file in the external storage.
Links:
- https://stackoverflow.com/questions/8330276/write-a-file-in-external-storage-in-android
- https://stackoverflow.com/questions/7887078/android-saving-file-to-external-storage
I even copied the entire method for the same but still cannot get the same result. Here is the log I am getting for the same:
W/System.err: java.io.IOException: No such file or directory
W/System.err: at java.io.UnixFileSystem.createFileExclusively0(Native Method)
code:
public void saveImageBitmap(Bitmap image_bitmap, String image_name) {
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root, "/memories");
if (!myDir.exists()) {
myDir.mkdirs();
}
String fname = "Image-" + image_name + ".jpg";
File file = new File(myDir, fname);
Log.d(TAG, "File file: " + file);
if (file.exists()) {
file.delete();
}
try {
file.createNewFile(); // if file already exists will do nothing
FileOutputStream out = new FileOutputStream(file);
image_bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
MediaScannerConnection.scanFile(context, new String[]{file.toString()}, new String[]{file.getName()}, null);
}
英文:
I have looked at all of the questions related to saving a file in the external storage.
Links:
- <https://stackoverflow.com/questions/8330276/write-a-file-in-external-storage-in-android>
- <https://stackoverflow.com/questions/7887078/android-saving-file-to-external-storage>
I even copied the entire method for the same but still cannot get the same result. Here is the log I am getting for the same:
W/System.err: java.io.IOException: No such file or directory
W/System.err: at java.io.UnixFileSystem.createFileExclusively0(Native Method)
code:
public void saveImageBitmap(Bitmap image_bitmap, String image_name) {
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root, "/memories");
if (!myDir.exists()) {
myDir.mkdirs();
}
String fname = "Image-" + image_name + ".jpg";
File file = new File(myDir, fname);
Log.d(TAG, "File file: " + file);
if (file.exists()) {
file.delete();
}
try {
file.createNewFile(); // if file already exists will do nothing
FileOutputStream out = new FileOutputStream(file);
image_bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
MediaScannerConnection.scanFile(context, new String[]{file.toString()}, new String[]{file.getName()}, null);
}
答案1
得分: 2
自Android 10(Q)以来,存储数据到媒体文件夹发生了一些变化。最简单的方法是在清单文件的应用程序部分添加android:requestLegacyExternalStorage="true"
,但这更像是一种权宜之计。
在这里搜索“在Android 10中存储图像”,你会找到适合你的正确方法。
英文:
there are a few changes with storing data to media folder since android 10 (Q).the easiest way is to add android:requestLegacyExternalStorage="true"
in the manifest under application, but it is more like a work around.
search for "store image android 10" here and you find the right way for you.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论