[英]How to get folder from assets?
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. 然后,我使用
File
类来获取文件夹中的文件。
How can I get a folder from the assets as a File
object? 如何从资产中获取文件夹作为
File
对象?
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. 使用以下代码将Assets文件夹和文件放入字符串数组。
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();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.