How can I copy a file from my assets folder into my SD card at first run of the app in Android?

How can I copy a file from my assets folder into my SD card at first run of the app in Android ? I need to know the exact steps please.

Upvotes: 0

Views: 1794

Answers (3)

android developer
android developer

Reputation: 116412

1.first run: check if a sharedPreference value that you put has existed before. if not, it's the first run and you should also add the value. if it exists, it's not the first run. example:

SharedPreferences pref=PreferenceManager.getdefaultsharedpreferences‎(this);
if(!pref.contains("firstInstall"))
  {
  //first install, so do some stuff...
  pref.edit().putBoolean("firstInstall",true).commit();
  }

2.add a permission in the manifest to write to the external storage .

3.use inputStream to read a file from the assets , as such:

 AssetManager assetManager = getAssets();
 InputStream is assetManager.open("myFile.txt");

4.use outputStream to write to the target file from the inputStream , as such:

  FileOutputStream os=null;
  try
    {
    File root = android.os.Environment.getExternalStorageDirectory(); 
    File file = new File(root , "myFile.txt");
    os = new FileOutputStream(file);
    byte[] buf = new byte[1024];
    int len;
    while ((len = is.read(buf)) > 0) 
       os .write(buf, 0, len);
    }
  finally
    {
    if(is!=null)
      is.close();
    if(os!=null)
      os.close();
    }

Upvotes: 1

Bette Devine
Bette Devine

Reputation: 1194

private void copyAssets() {
    AssetManager assetManager = getAssets();
    String[] files = null;
    try {
        files = assetManager.list("");
    } catch (IOException e) {
        Log.e("tag", "Failed to get asset file list.", e);
    }
    for(String filename : files) {
        InputStream in = null;
        OutputStream out = null;
        try {
          in = assetManager.open(filename);
          File outFile = new File(getExternalFilesDir(null), filename);
          out = new FileOutputStream(outFile);
          copyFile(in, out);
          in.close();
          in = null;
          out.flush();
          out.close();
          out = null;
        } catch(IOException e) {
            Log.e("tag", "Failed to copy asset file: " + filename, e);
        }       
    }
}
private 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);
    }
}

Upvotes: 1

Ezzored
Ezzored

Reputation: 925

Use standard JAVA I/O. Use Environment.getExternalStorageDirectory() to get to the root of external storage (which, on some devices, is an SD card). You have to add this to your persmissions: <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> You can use the following method for copying the file when you have the paths:

public void copy(File src, File dst) throws IOException {
    InputStream in = new FileInputStream(src);
    OutputStream out = new FileOutputStream(dst);

    // Transfer bytes from in to out
    byte[] buf = new byte[1024];
    int len;
    while ((len = in.read(buf)) > 0) {
        out.write(buf, 0, len);
    }
    in.close();
    out.close();
}

Upvotes: 0

Related Questions