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?
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"));
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();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.