Wednesday, 15 January 2014

Copy file from asset to sdcard in android

Copy fileAsset to sdcard android:

In some apps need to move some files from asset to sdcard or phone memory for feature use. This tutorial helps you to move file from asset to sdcard or phone memory.
Follw the below steps to do:
1)First,get the files from the asset,so that you have to create AssetManager object.
AssetManager assetManager = getAssets();

2)get the file from the asset folder using asset manager
Stri ng[] files;
files = assetManager.list("Files");

3)Create input & output stream for set input and output file locations.
InputStream in = null;
OutputStream out = 
null;

4)Finally,copy file to sdcard using input and output locations using CopyFile() method.
in = assetManager.open("Files/"+filename);
out = new FileOutputStream(Environment.getExternalStorageDirectory().toString() +"/" + filename);
copyFile(in, out);
Here,CopyFile method read all content of the file and write the data into the destination location
privatevoidcopyFile(InputStream in, OutputStream out) throws IOException {
byte[] buffer = newbyte[1024];
int read;
while((read = in.read(buffer)) != -1){
out.write(buffer, 0, read);
}
}
Screenshot:
Copy file asset To sdcard android Example
Copy file asset To sdcard android Example

Copy file asset To sdcard android Example



No comments: