Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy directory from Assets to local directory

Tags:

I'm trying to use a directory that I have in my assets folder and access it as a File. Is it possible to access something in the Assets directory as a File? If not, how can I copy a directory from the Assets folder to the application's local directory?

I would copy a file like so:

    try     {         InputStream stream = this.getAssets().open("myFile");         OutputStream output = new BufferedOutputStream(new FileOutputStream(this.getFilesDir() + "/myNewFile"));          byte data[] = new byte[1024];         int count;          while((count = stream.read(data)) != -1)         {             output.write(data, 0, count);         }          output.flush();         output.close();         stream.close();     }     catch(IOException e)     {         e.printStackTrace();     } 

However, I'm not sure how I would be able to do this for a directory.

I would rather not build my infrastructure around something that doesn't work, so how would I copy a directory from Assets to a local directory, or is it possible to access a directory in my Assets as a File?

EDIT

This is how I solved it for my own project:

InputStream stream = null; OutputStream output = null;  for(String fileName : this.getAssets().list("demopass")) {     stream = this.getAssets().open("directoryName/" + fileName);     output = new BufferedOutputStream(new FileOutputStream(this.getFilesDir() + "/newDirectory/" + fileName));      byte data[] = new byte[1024];     int count;      while((count = stream.read(data)) != -1)     {         output.write(data, 0, count);     }      output.flush();     output.close();     stream.close();      stream = null;     output = null; } 
like image 271
RileyE Avatar asked Mar 22 '13 16:03

RileyE


People also ask

How do I retrieve files from assets folder?

Right click on the assets folder, select New >> file (myText. txt) and your text. “Darkness cannot drive out darkness: only light can do that. Hate cannot drive out hate: only love can do that.”

How to copy a folder contents?

Alternatively, right-click the folder, select Show more options and then Copy. In Windows 10 and earlier versions, right-click the folder and select Copy, or click Edit and then Copy. Navigate to the location where you want to place the folder and all its contents.

How to copy folder to another directory in ubuntu?

Answer: Use the cp Command You can use the cp command to copy files locally from one directory to another. The -a option copy files recursively, while preserving the file attributes such as timestamp. The period symbol ( . ) at end of the source path allows to copy all files and folders, including hidden ones.


1 Answers

As suggested by dmaxi in comment above, you can use his link, with this code:

    void displayFiles (AssetManager mgr, String path) {         try {             String list[] = mgr.list(path);             if (list != null)                 for (int i=0; i<list.length; ++i)                 {                     Log.v("Assets:", path +"/"+ list[i]);                     displayFiles(mgr, path + "/" + list[i]);                 }         } catch (IOException e) {              Log.v("List error:", "can't list" + path);         }      } 

I took it on this link. Maybe you can combine this code with precedent one.

EDIT: see also AssetManager.

private void copyFolder(String name) {             // "Name" is the name of your folder!     AssetManager assetManager = getAssets();     String[] files = null;      String state = Environment.getExternalStorageState();      if (Environment.MEDIA_MOUNTED.equals(state)) {         // We can read and write the media         // Checking file on assets subfolder         try {             files = assetManager.list(name);         } catch (IOException e) {             Log.e("ERROR", "Failed to get asset file list.", e);         }         // Analyzing all file on assets subfolder         for(String filename : files) {             InputStream in = null;             OutputStream out = null;             // First: checking if there is already a target folder             File folder = new File(Environment.getExternalStorageDirectory() + "/yourTargetFolder/" + name);             boolean success = true;             if (!folder.exists()) {                 success = folder.mkdir();             }             if (success) {                 // Moving all the files on external SD                 try {                     in = assetManager.open(name + "/" +filename);                     out = new FileOutputStream(Environment.getExternalStorageDirectory() + "/yourTargetFolder/" + name + "/" + filename);                     Log.i("WEBVIEW", Environment.getExternalStorageDirectory() + "/yourTargetFolder/" + name + "/" + filename);                     copyFile(in, out);                     in.close();                     in = null;                     out.flush();                     out.close();                     out = null;                 } catch(IOException e) {                     Log.e("ERROR", "Failed to copy asset file: " + filename, e);                 } finally {                     // Edit 3 (after MMs comment)                     in.close();                     in = null;                     out.flush();                     out.close();                     out = null;                 }             }             else {                 // Do something else on failure             }                }     } else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {         // We can only read the media     } else {         // Something else is wrong. It may be one of many other states, but all we need         // is to know is we can neither read nor write     } }  // Method used by copyAssets() on purpose to copy a file. 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);     } } 

EDIT 2: i'have added an example above: this piece of code copy only a specific folder from assets, to sd card. Let me know if it works!

like image 190
JJ86 Avatar answered Sep 23 '22 21:09

JJ86