Search code examples
androidassets

Acces to Assets file in android application


I have got a problem with my application. I try to access the Assets folder and download some PDF files in a directory.

When i try the code on my galaxy S9 (API 28) it is working just fine. But when i try the code on my Galaxy S2 tab (API24) nothing happen.

I use this code to access and download the pdf files :

        public void copyAssets(Context context)
        {
            DownloadComplete = false;
            Log.d("tag", "Here 1 ?");
            AssetManager assetManager = context.getAssets();
            Log.d("tag", "Here 2 ?");
            String[] files = null;
            Log.d("tag", "Here 3?");
            try
            {
                files = assetManager.list(resAventure + "/");
                Log.d("tag", "Here  4?");
            } catch (IOException e) {
                Log.d("tag", "Failed to get asset file list.", e);
            }
            if (files != null) for (String filename : files)
            {
                Log.d("tag", "Here 5?");
                InputStream in = null;
                OutputStream out = null;
                try {
                    Log.d("tag", "Here 6?");
                    in = assetManager.open(resAventure + "/" + filename);
                    out = new FileOutputStream(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Download/Escape/" + resAventure + "/" + filename);
                    copyFile(in, out);
                    Toast.makeText(getApplicationContext(), "Téléchargement effectué sur l'appareil", Toast.LENGTH_LONG).show();
                    DownloadComplete = true;

                } catch(IOException e) {
                    Log.d("tag", "Failed to copy asset file: " + filename , e);
                }
                finally
                {
                    if (in != null)
                    {
                        try
                        {
                            in.close();
                            in = null;
                        } catch (IOException e)
                        {
                            Log.d("tag", "Failed A", e);
                        }
                    }
                    if (out != null)
                    {
                        try {
                            out.flush();
                            out.close();
                            out = null;
                        } catch (IOException e)
                        {
                            Log.d("tag", "Failed B", e);
                        }
                    }
                }
            }
        }
        public  void copyFile(InputStream in, OutputStream out) throws IOException
        {
            byte[] buffer = new byte[1024];
            int read;
            while((read = in.read(buffer)) != -1){
                out.write(buffer, 0, read);
            }
        }

In API28 everything is OK. But in API24 i only have in the log :

D/ViewRootImpl@45de4c3[documents]: ViewPostImeInputStage processPointer 1
D/tag: Here 1 ?
Here 2 ?
Here 3?
D/tag: Here  4?
D/ViewRootImpl@45de4c3[documents]: ViewPostImeInputStage processPointer 0 

Is there a difference between the two API for the ASSETS FOLDER ? I don't understand why it's working only once...

Can somoene help me please ?


Solution

  • I have found it.

    I replace : files = assetManager.list(resAventure + "/"); by files = assetManager.list(resAventure);