0

I have an index.html file and some images displayed on this page stored in res/raw folder.

I need to open this file in android browser. So, I copy this file into sdcard.

But if i use R.raw.index the index file only copied other files are not copied. Since other html and images are not copied to sdcard: I don't see the images when I open index.html in browser.

Here is my current code to copy raw resource:

private File copyFile(int resourceId, String filename) {
        InputStream in = null; 
        OutputStream out = null;
        File outFile = null;
        try {
            in = mContext.getResources().openRawResource(resourceId);
            outFile = new File(mContext.getExternalFilesDir(null), filename);
            Log.d("TestHTML", "output file" +  outFile.getAbsolutePath());
            out = new FileOutputStream(outFile);
            byte[] buffer = new byte[1024];
            int read;
            while((read = in.read(buffer)) != -1){
                out.write(buffer, 0, read);
            }
        } catch(IOException e) {
            Log.e("TestHTML", "Failed to copy file", e);
        } finally {
            try {
                in.close();
                out.flush();
                out.close();
                in = null;
                out = null;
            } catch (Exception e){}
        }
        return outFile;
    }

And here is the layout of the res/raw folder

enter image description here

Can you give me a hint on how to copy the whole content of the res/raw directory to the internal storage ?

ben75
  • 29,217
  • 10
  • 88
  • 134
  • do you have a question to ask ? – ben75 May 14 '14 at 10:01
  • I asked my question. wasn't you understand? – user3602987 May 14 '14 at 10:04
  • I don't see the question mark. When I read your question, here is the abstract that I can make : "I copy only one file from raw resources to internal storage and after the copy : only one file is copied to internal storage" (and so what's the question here ?) (you can also read this : http://stackoverflow.com/help/how-to-ask ) – ben75 May 14 '14 at 10:07
  • Can you see my image? Inside raw folder i have other folders and files. But index.html file only copied to sdcard. So i can't get the image display and other pages links are not working – user3602987 May 14 '14 at 10:10
  • 1
    So I suggest you to improve your question with this title : "How can I copy the whole content of a directory from raw resources to internal storage ?" – ben75 May 14 '14 at 10:12

1 Answers1

1

From the android doc about the res/raw folder:

Arbitrary files to save in their raw form. To open these resources with a raw InputStream, call Resources.openRawResource() with the resource ID, which is R.raw.filename.

However, if you need access to original file names and file hierarchy, you might consider saving some resources in the assets/ directory (instead of res/raw/). Files in assets/ are not given a resource ID, so you can read them only using AssetManager.

Since you will use all this stuff in html : you need to keep an access by filename. So don't put those files in res/raw. Put them in your assets folder.

Once this is done you can copy all of them one by one and recursively to to internal storage.

The entry point to list files in some assets folder is getAssets().list(path) where path denote the path to the directory containing the index.html.

You also have to update your copyFile method to make it recursive and listing files from assets.

ben75
  • 29,217
  • 10
  • 88
  • 134