4

I use the following code to copy a particular directory and its contents to the sd card. The directory is placed inside res/raw folder.

Following is the code I use:

public class CopyActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    copyFilesToSdCard();
}

static String extStorageDirectory =  Environment.getExternalStorageDirectory().toString();
final static String TARGET_BASE_PATH = extStorageDirectory+"/Android/data/";

private void copyFilesToSdCard() {
    copyFileOrDir("");
}

private void copyFileOrDir(String path) {
    AssetManager assetManager = this.getAssets();
    String assets[] = null;
    try {
        Log.i("tag", "copyFileOrDir() "+path);
        assets = assetManager.list(path);
        if (assets.length == 0) {
            copyFile(path);
        } else {
            String fullPath =  TARGET_BASE_PATH + path;
            Log.i("tag", "path="+fullPath);
            File dir = new File(fullPath);
            if (!dir.exists())
                if (!dir.mkdirs());
                    Log.i("tag", "could not create dir "+fullPath);
            for (int i = 0; i < assets.length; ++i) {
                String p;
                if (path.equals(""))
                    p = "";
                else 
                    p = path + "/";

                    copyFileOrDir( p + assets[i]);
            }
        }
    } catch (IOException ex) {
        Log.e("tag", "I/O Exception", ex);
    }
}

private void copyFile(String filename) {
    AssetManager assetManager = this.getAssets();

    InputStream in = null;
    OutputStream out = null;
    String newFileName = null;
    try {
        Log.i("tag", "copyFile() "+filename);
        in = assetManager.open(filename);
        if (filename.endsWith(".jpg")) // extension was added to avoid compression on APK file
            newFileName = TARGET_BASE_PATH + filename.substring(0, filename.length()-4);
        else
            newFileName = TARGET_BASE_PATH + filename;
        out = new FileOutputStream(newFileName);

        byte[] buffer = new byte[1024];
        int read;
        while ((read = in.read(buffer)) != -1) {
            out.write(buffer, 0, read);
        }
        in.close();
        in = null;
        out.flush();
        out.close();
        out = null;
    } catch (Exception e) {
        Log.e("tag", "Exception in copyFile() of "+newFileName);
        Log.e("tag", "Exception in copyFile() "+e.toString());
    }

}
}

exception:

 Exception in copyFile() of /mnt/sdcard/Android/data/raw/edu/anees.txt
 Exception in copyFile() java.io.FileNotFoundException:
 /mnt/sdcard/Android/data/raw/edu/anees.txt: open failed: ENOENT (No such file or directory)

Can someone please let me know what causes this issue and solution for the same.

ref: How to copy files from 'assets' folder to sdcard?

EDIT

I could resolve the exception with one of the tips regarding permission which was posted as one answer here.

Now I encounter another issue which is as follows:

The following log says I cannot create a folder in the following path:

Log.i("tag", "could not create dir "+fullPath);// fullPath = /mnt/sdcard/Android/data/raw

I do not want to have the data stored inside /mnt/sdcard/Android/data/raw, but instead, I want to have the contents of the raw folder inside assets to be copied to the path /mnt/sdcard/Android/data which is not happening with the piece of code I use from the reference link I gave. Any obvious reasons that might cause this with the code I gave?

Community
  • 1
  • 1
user1400538
  • 855
  • 5
  • 24
  • 42

1 Answers1

6

You might have forgotten to set the permission in your manifest

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Andro Selva
  • 53,910
  • 52
  • 193
  • 240