0
File directory = new File(android.os.Environment.getExternalStorageDirectory()+ File.separator+"folder");

The above code can get a folder in the memory card as a File. Then I use the File class to get the files in the folder.

How can I get a folder from the assets as a File object?

Jon Senchyna
  • 7,867
  • 2
  • 26
  • 46
  • Go to this:[http://stackoverflow.com/questions/4447477/android-how-to-copy-files-in-assets-to-sdcard](http://stackoverflow.com/questions/4447477/android-how-to-copy-files-in-assets-to-sdcard) – M D Feb 12 '14 at 12:38

2 Answers2

0

There is no "absolute path for a file existing in the asset folder". You can use following code to fetch details from assets folder,

uri = Uri.fromFile(new File("//assets/aaa/aaa.txt"));
user3301551
  • 350
  • 1
  • 14
0

Try out as below :

AssetManager am = getAssets();
InputStream inputStream = am.open(file:///android_asset/foldername/filename);
File file = createFileFromInputStream(inputStream);

private File createFileFromInputStream(InputStream inputStream) {

   try{
      File f = new File(my_file_name);
      OutputStream outputStream = new FileOutputStream(f);
      byte buffer[] = new byte[1024];
      int length = 0;

      while((length=inputStream.read(buffer)) > 0) {
        outputStream.write(buffer,0,length);
      }

      outputStream.close();
      inputStream.close();

      return f;
   }catch (IOException e) {
         //Logging exception
   }

return null;
}

Use below code to get the Assets folders and files into an array of Strings.

     final AssetManager assetManager = getAssets();
        try {
            // for assets folder add empty string
                        String[] filelist = assetManager.list("");
                        // for assets/subFolderInAssets add only subfolder name
                        String[] filelistInSubfolder = assetManager.list("subFolderInAssets");
            if (filelist == null) {
                // dir does not exist or is not a directory
            } else {
                for (int i=0; i<filelist.length; i++) {
                    // Get filename of file or directory
                    String filename = filelist[i];
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
GrIsHu
  • 29,068
  • 10
  • 64
  • 102