Problem: How to copy files from Assets to SD-Card?
Solution:
CopyFileAssetsToSDCardActivity.java
package com.paresh.copyfileassetstoAssets; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import android.app.Activity; import android.content.res.AssetManager; import android.os.Bundle; import android.os.Environment; import android.util.Log; public class CopyFileAssetsToSDCardActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); CopyAssets(); } private void CopyAssets() { AssetManager assetManager = getAssets(); String[] files = null; try { files = assetManager.list("Files"); } catch (IOException e) { Log.e("tag", e.getMessage()); } for(String filename : files) { System.out.println("File name => "+filename); InputStream in = null; OutputStream out = null; try { in = assetManager.open("Files/"+filename); // if files resides inside the "Files" directory itself out = new FileOutputStream(Environment.getExternalStorageDirectory().toString() +"/" + 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); } } }
main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> </LinearLayout>
Download Example: https://github.com/PareshMayani/Android-CopyFiles