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

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

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:

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, &quot;/memories&quot;);
        if (!myDir.exists()) {
            myDir.mkdirs();
        }
        String fname = &quot;Image-&quot; + image_name + &quot;.jpg&quot;;
        File file = new File(myDir, fname);
        Log.d(TAG, &quot;File file: &quot; + 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=&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:

确定