Unable to save file to an external storage directory in Android.

huangapple go评论118阅读模式
英文:

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:

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:

  1. public void saveImageBitmap(Bitmap image_bitmap, String image_name) {
  2. String root = Environment.getExternalStorageDirectory().toString();
  3. File myDir = new File(root, "/memories");
  4. if (!myDir.exists()) {
  5. myDir.mkdirs();
  6. }
  7. String fname = "Image-" + image_name + ".jpg";
  8. File file = new File(myDir, fname);
  9. Log.d(TAG, "File file: " + file);
  10. if (file.exists()) {
  11. file.delete();
  12. }
  13. try {
  14. file.createNewFile(); // if file already exists will do nothing
  15. FileOutputStream out = new FileOutputStream(file);
  16. image_bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
  17. out.flush();
  18. out.close();
  19. } catch (Exception e) {
  20. e.printStackTrace();
  21. }
  22. MediaScannerConnection.scanFile(context, new String[]{file.toString()}, new String[]{file.getName()}, null);
  23. }
英文:

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:

  1. public void saveImageBitmap(Bitmap image_bitmap, String image_name) {
  2. String root = Environment.getExternalStorageDirectory().toString();
  3. File myDir = new File(root, &quot;/memories&quot;);
  4. if (!myDir.exists()) {
  5. myDir.mkdirs();
  6. }
  7. String fname = &quot;Image-&quot; + image_name + &quot;.jpg&quot;;
  8. File file = new File(myDir, fname);
  9. Log.d(TAG, &quot;File file: &quot; + file);
  10. if (file.exists()) {
  11. file.delete();
  12. }
  13. try {
  14. file.createNewFile(); // if file already exists will do nothing
  15. FileOutputStream out = new FileOutputStream(file);
  16. image_bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
  17. out.flush();
  18. out.close();
  19. } catch (Exception e) {
  20. e.printStackTrace();
  21. }
  22. MediaScannerConnection.scanFile(context, new String[]{file.toString()}, new String[]{file.getName()}, null);
  23. }

答案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=&quot;true&quot; 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.

huangapple
  • 本文由 发表于 2020年7月28日 03:06:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/63121916.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定