1

I am not able to copy files from my assets folder to the Sd card. How can I accomplish this? Or is there a way to copy files from assets or any other folder to sd card during installation of the app? Here is my code:

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android.os.Bundle;
import android.app.Activity;
import android.content.res.AssetManager;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.support.v4.app.NavUtils;

public class MainActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    CopyAssets();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

private void CopyAssets() { 
    AssetManager assetManager = getAssets(); 
    String[] files = null; 
    try { 
        files = assetManager.list(""); 
    } catch (IOException e) { 
        Log.e("tag", e.getMessage()); 
    } 
    for(String filename : files) { 
        InputStream in = null; 
        OutputStream out = null; 
        try { 
          in = assetManager.open(filename); 
          out = new FileOutputStream("/sdcard/" + filename); 
          copyFile(in, out); 
          in.close(); 
          in = null; 
          out.flush(); 
          out.close(); 
          out = null; 
        } catch(Exception e) { 
            Log.e("tag", e.getMessage()); 
        }        
    } 
} 
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); 
    } 
}


}
Adam
  • 418
  • 4
  • 14

3 Answers3

3

You should not hard code storage directory . use Environment.getExternalStorageDirectory()

String destFile = Environment.getExternalStorageDirectory().toString().concat("/ans");
try {

        File f2 = new File(destFile);
        InputStream in = getAssets().open("try.xml");
        OutputStream out = new FileOutputStream(f2);

        byte[] buf = new byte[1024];
        int len;
        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
        in.close();
        out.close();
        System.out.println("File copied.");
    } catch (FileNotFoundException ex) {
        System.out
                .println(ex.getMessage() + " in the specified directory.");

    } catch (IOException e) {
        System.out.println(e.getMessage());
    }

add permission in manifest

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Sunny Kumar Aditya
  • 2,806
  • 4
  • 26
  • 38
2

To write files in the sdcard you have to give the permission on the manifest

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Did you?

user370305
  • 108,599
  • 23
  • 164
  • 151
  • And is there a way to do it during the install of the app? – Adam Jul 08 '12 at 12:55
  • [http://stackoverflow.com/questions/7438247/android-copy-assets-on-install-to-sdcard] I read this, I think there is no way to do anything during intallation. – Adam Jul 08 '12 at 12:58
  • No, You can't at installation time, but you can at your application first launch. – user370305 Jul 08 '12 at 15:29
0

Add this code to your onCreate. So the first time your app starts after installation the function to copy assets to SD card and then its never called as you enter the app again.

        SharedPreferences score = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    Editor score_inc = score.edit();
    int counter = score.getInt("counter",0);
    float font_size = score.getFloat("font_size",20.0f);
    if(counter==0)
    {
        //Put your function to copy files here
        score_inc.putInt("counter", ++counter);
        score_inc.commit();
        Toast.makeText(getApplicationContext(), "Success", Toast.LENGTH_SHORT).show();
    }
xtreak
  • 1,376
  • 18
  • 42