Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Copy assets to internal storage

Tags:

java

android

Good day!

I have just started developing for android. In my app, I need to copy the items in my assets folder to the internal storage.

I have searched a lot on SO including this which copies it to the external storage. How to copy files from 'assets' folder to sdcard?

This is what I want to achieve: I have a directory already present in the internal storage as X>Y>Z. I need a file to be copied to Y and another to Z.

Can anyone help me out with a code snippet? I really don't have any idea how to go on about this.

Sorry for my bad English.

Thanks a lot.

like image 833
Noob Android Avatar asked Oct 07 '13 07:10

Noob Android


People also ask

Where do I put assets in Android?

Step 1: To create an asset folder in Android studio open your project in Android mode first as shown in the below image. Step 2: Go to the app > right-click > New > Folder > Asset Folder and create the asset folder. Step 3: Android Studio will open a dialog box. Keep all the settings default.

How do you copy assets?

Right-click on the asset in the Navigation Panel and choose Copy, or choose Copy from the More menu. Enter a New Page Name for the copied asset.


1 Answers

Use

 String out= Environment.getExternalStorageDirectory().getAbsolutePath() + "/X/Y/Z/" ;           File outFile = new File(out, Filename); 

After Editing in your ref. Link Answer.

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);        String outDir = Environment.getExternalStorageDirectory().getAbsolutePath() + "/X/Y/Z/" ;         File outFile = new File(outDir, 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);      }    } 
like image 172
AmmY Avatar answered Oct 03 '22 03:10

AmmY