简体   繁体   中英

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);
    }

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM