1

In my Android app I have packaged a file in the /assets folder that needs to be copied to the SDCARD. When I list the contents of the assets folder to a String[] I get "images", "sounds", "webkit" and "kioskmode" but not my file manually added to the assets folder.

My code is here:

private void copyAsset () {
    AssetManager am = getApplicationContext().getAssets();
    String[] files = null;
    try {
      files = am.list("");
    } catch (IOException e) {

    }

    for (String filename : files) {
        if (filename.equals("images") || filename.equals("kioskmode") ||
                filename.equals("sounds") || filename.equals("webkit")) {
            Log.i(TAG, "Skipping folder " + filename);
            continue;
        }
        InputStream in = null;
        OutputStream out = null;
        try {
            in = am.open(filename);
            File outFile = new File(Environment.getExternalStorageDirectory().getPath(), filename);
            out = new FileOutputStream(outFile);
            copyFile(in, out);
            in.close();
            in = null;
            out.flush();
            out.close();
            out = null;
        } catch (IOException e) {
            e.printStackTrace();
            Log.e(TAG, "Error copying asset ", e);
        }
    }
}

Does it make a difference that this is a second class in my app and is called in my MainActivity using Intent showHelper = new Intent(this, HelperManager.class); startActivity(showHelper); ?

I have tried the 2nd line (AssetManager am = ...) with and without the getApplicationContext() bit, tried moving the file into a subfolder of /assets and tried files = am.list("") with leading and trailing slashes. If I use a subfolder the files array is empty when the code runs (set a breakpoint on the files = am.list(""); line and inspected it at run time. The strange thing is that it worked once - when I first wrote the code, but for further testing, I deleted the file from the /sdcard folder on the phone, and it never worked since even though the file is still in the assets folder. I am using Android Studio if that matters. Thanks

Mark
  • 55
  • 2
  • 10
  • Do you have something inside the assets folder? – Blackbelt Jul 16 '13 at 08:14
  • Yes I do @blackbelt I can see it in the Project Layout in Android Studio and also in Windows explorer if I browse to the project location. Perhaps I will try and remove and re-add the file (it is an .apk btw). – Mark Jul 16 '13 at 08:48
  • if the files are bundled with the apk you are good. Do you understand the meaning of the `continue` keyword ? Do you want "images", "sounds", "webkit" and "kioskmode to be written inside SdCard? – Blackbelt Jul 16 '13 at 08:52
  • I have removed/deleted the embedded file from assets, rebuilt it, copied it back in and also renamed the file to be all lowercase characters and still no joy. – Mark Jul 16 '13 at 08:58
  • 1
    No @blackbelt, the four folders mentioned are not actually in the assets folder and I don't want them (hence the code above to skip copying if they are found). It seems like my getAssets() is looking in some other assets folder, like mine isn't being included when I run the app and its including some other assets folder, perhaps a system or other libraries assets folder..? – Mark Jul 16 '13 at 09:02
  • I can not really understand what your assets folder contains and what do you want to copy. Try prinying the content of files – Blackbelt Jul 16 '13 at 09:04
  • My assets folder contains one file named leh.apk and using the code above I want to copy/extract that file into /sdcard. What is happening at run time is that the `files` array isn't listing my leh.apk file, but instead it contains the four folder names mentioned even though I have no idea where they have come from and I don't want them. My assets folder looks like this: `/assets/leh.apk` What happens when running the app on my phone: `/assets/images/`, `/assets/sounds/` etc – Mark Jul 16 '13 at 09:11
  • you are using the Activity's context does it make any difference? Instead of getApplicationContext().getAssets(); try with getAssets(); or getActivity().getAssets(); – Blackbelt Jul 16 '13 at 09:18
  • @blackbelt I have tried just `getAssets()`, can't remember if I have tried with `getActivity()` but I think my project may have become corrupt so I am going to create a new blank project and try again. – Mark Jul 16 '13 at 09:34
  • Found something interesting - the directories that are mysteriously being added to my assets file list are contained in the Android.jar assets folder that is listed under **External Libraries < Android 4.2.2 Platform >** So how do I make my app use my assets folder under myapp/src/main/res/assets? – Mark Jul 16 '13 at 11:33
  • The correct path is myapp/assets – Blackbelt Jul 16 '13 at 12:01
  • myapp/src/main/res/assets is how it is laid out in Project Explorer in Android Studio and was given as reference. Anyone have any experience with Gradle - I recall somewhere seeing someone modifying the build.gradle file to tell it the app included assets and to priorities those ones. – Mark Jul 16 '13 at 12:14
  • Managed to find a fix. [http://stackoverflow.com/questions/16821182/google-android-studio-load-a-simple-text-file](http://stackoverflow.com/questions/16821182/google-android-studio-load-a-simple-text-file) – Mark Jul 16 '13 at 12:39

1 Answers1

0

Managed to get a solution using Load a simple text file in Android Studio as a fix. It still puts the 4 folders in the files array but I Can skip them using code as given above, although I should rather check for the file I want rather than the 4 I don't!

Community
  • 1
  • 1
Mark
  • 55
  • 2
  • 10