Thursday 25 August 2016

File transfer from Assets folder to Phone Storage with AsyncTask


Add below permission to AndroidManifest.xml


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


MainActivity.java



import java.io.File;
import java.util.ArrayList;
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.view.Menu;
import android.widget.Toast;
public class MainActivity extends Activity {
// this where your file will be stored in sdcard in this case in folder (YOUR_FILE)
static String BASE_FILE = Environment.getExternalStorageDirectory()+"/YOUR_FILE/";
ArrayList<String> TmpList = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// create dynamic array to add your files
ArrayList<String> MyFiles = new ArrayList<String>();
MyFiles.add("demo_voice.mp3");
MyFiles.add("demo_img.jpg");
// add your files here
// create base folder if not exists
File f = new File(BASE_FILE);
if(!f.exists())
f.mkdir();
// this loop to check if files already coped or not or any file delete
for(int i=0;i<MyFiles.size();i++){
File check = new File(BASE_FILE,MyFiles.get(i));
if(!check.exists())
TmpList.add(MyFiles.get(i)); // copy not coped items to other list
}
// now check if not all files copy or something remove
if(TmpList.size()>0)
new AsyncCopy(this, BASE_FILE, TmpList).execute("");
else
Toast.makeText(getApplicationContext(), "all files coped ",Toast.LENGTH_LONG).show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}

AsyncCopy.java


import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.widget.Toast;
public class AsyncCopy extends AsyncTask<String, String, String>{
String savePath;
Activity ctx;
private ProgressDialog pDialog;
ArrayList<String> arr;
AsyncCopy(Activity _ctx,String _savePath,ArrayList<String> Files){
this.ctx = _ctx;
this.savePath = _savePath;
this.arr = Files;
}
@Override
protected void onPreExecute() {
pDialog = new ProgressDialog(ctx);
pDialog.setMessage("Copying files from asset to sdcard");
pDialog.setIndeterminate(true);
pDialog.setCancelable(false);
pDialog.show();
super.onPreExecute();
}
@Override
protected String doInBackground(String... urls) {
File f = new File(savePath);
if(!f.exists())
f.mkdir();
for(int i=0;i<arr.size();i++)
Copy(arr.get(i));
return null;
}
@Override
protected void onPostExecute(String unused) {
Toast.makeText(ctx,"Copy Done",Toast.LENGTH_LONG).show();
pDialog.hide();
}
void Copy(String fname){
try{
int count;
InputStream input= ctx.getAssets().open(fname);
OutputStream output = new FileOutputStream(savePath+"/"+fname);
byte data[] = new byte[1024];
while ((count = input.read(data))>0) {
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
}catch(Exception e){
// error while copying
}
}
}

No comments:

Post a Comment