javaandroidandroid-manifestandroid-assets

Android/Java: I/System.out: Error :java.io.FileNotFoundException: /storage/emulated/0/heremaps/...: open failed: ENOENT (No such file or directory)


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


Solution

  • Actually, you need to define the path as follows:

    try{
     .........
     ....
     String destination = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
     ......
    }