2

I try to copy a folder from the assets to the external storage but I have the error messages:

I/System.out: Error :java.io.FileNotFoundException: /storage/emulated/0/heremaps/appcache/sat/12/CURRENT: open failed: ENOENT (No such file or directory)
I/System.out: Error :java.io.FileNotFoundException: /storage/emulated/0/heremaps/appcache/sat/12/LOCK: open failed: ENOENT (No such file or directory)
I/System.out: Error :java.io.FileNotFoundException: /storage/emulated/0/heremaps/appcache/sat/12/MANIFEST-000008: open failed: ENOENT (No such file or directory)
I/System.out: Error :java.io.FileNotFoundException: /storage/emulated/0/heremaps/assets/here/miami/font/FiraGO-Map.LICENSE: open failed: ENOENT (No such file or directory)
I/System.out: Error :java.io.FileNotFoundException: /storage/emulated/0/heremaps/assets/here/miami/font/FiraGO_Map.ttf: open failed: ENOENT (No such file or directory)
I/System.out: Error :java.io.FileNotFoundException: /storage/emulated/0/heremaps/assets/here/miami/font/HanSans_ExtraLight.ttf: open failed: ENOENT (No such file or directory)
I/System.out: Error :java.io.FileNotFoundException: /storage/emulated/0/heremaps/assets/here/miami/font/Lohit.LICENSE: open failed: ENOENT (No such file or directory)
...

Here is my code:

 private void copyAsset(String path) {
        AssetManager manager = getAssets();

        // If we have a directory, we make it and recurse. If a file, we copy its
        // contents.
        try {
            String[] contents = manager.list(path);

            if (contents == null || contents.length == 0)
                throw new IOException();

            // Création des dossiers
            String destination = Environment.getExternalStorageDirectory().getPath() + File.separator;
            File dir = new File( destination + path);

            dir.mkdirs();

            // Recurse on the contents.
            for (String entry : contents) {
                copyAsset(path + "/" + entry);
            }
        } catch (IOException e) {
            copyFileAsset(path);
        }
    }

    private void copyFileAsset(String path) {
        // File file = new File(Environment.getExternalStorageDirectory().getPath() + "/NTA/" + path);
        String destination = Environment.getExternalStorageDirectory().getPath() + File.separator;
        File file = new File( destination + path);

        try {
            InputStream in = getAssets().open(path);
            OutputStream out = new FileOutputStream(file);
            byte[] buffer = new byte[1024];
            int read = in.read(buffer);
            while (read != -1) {
                out.write(buffer, 0, read);
                read = in.read(buffer);
            }
            out.close();
            in.close();
        } catch (IOException e) {
            System.out.println("Error :" + e);
        }
    }

And my Androidmanifest:

   <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />

    <application 
        android:requestLegacyExternalStorage="true"
        android:name=".ApplicationClass"
        android:allowBackup="true"

Thanks

ΩlostA
  • 2,501
  • 5
  • 27
  • 63
  • 1
    Apps on Android 11+ do no longer have direct access to emulated sdcard storage: https://stackoverflow.com/questions/62782648/android-11-scoped-storage-permissions – Robert Jan 20 '22 at 10:40
  • `dir.mkdirs();` Only call mkdirs if the directory does not exist. And if you call mkdirs check te return value. And if false stop. You are telling us that you cannot create files. But all starts and stops there where you cannot create that directory. – blackapps Jan 20 '22 at 19:20

1 Answers1

7

Actually, you need to define the path as follows:

try{
 .........
 ....
 String destination = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
 ......
}
Rudra Rokaya
  • 637
  • 1
  • 5
  • 9