如何从Assets文件夹而不是android.os.Environment.getExternalStorageDirectory()?
获取图像我需要根据我的要求替换此代码:
File directory = new File(
android.os.Environment.getExternalStorageDirectory()
+ File.separator + AppConstant.PHOTO_ALBUM);
由于
修改
ArrayList filePaths = new ArrayList();
if (directory.isDirectory()) {
File[] listFiles = directory.listFiles();
if (listFiles.length > 0) {
for (int i = 0; i < listFiles.length; i++) {
String filePath = listFiles[i].getAbsolutePath();
if (IsSupportedFile(filePath)) {
filePaths.add(filePath);
}
}
} else {
Toast.makeText(
_context,
AppConstant.PHOTO_ALBUM
+ " is empty. Please load some images in it !",
Toast.LENGTH_LONG).show();
}
} else {
AlertDialog.Builder alert = new AlertDialog.Builder(_context);
alert.setTitle("Error!");
alert.setMessage(AppConstant.PHOTO_ALBUM
+ " directory path is not valid! Please set the image directory name AppConstant.java class");
alert.setPositiveButton("OK", null);
alert.show();
}
答案 0 :(得分:0)
这是如何获取已知资产子文件夹中的所有图像的方法。您需要知道的只是包含资产中图像的子文件夹的名称。希望它会有所帮助。
private List<Bitmap> imageListFromAsset(String dirFrom) {
//list of images in assets
List<Bitmap> imageList = new ArrayList<Bitmap>();
Resources res = getResources();
AssetManager am = res.getAssets();
String fileList[];
try {
fileList = am.list(dirFrom);
for (String fileName : fileList) {
imageList.add(getImageFromAsset(dirFrom+ File.separator +fileName, am));
}
} catch (IOException e) {
e.printStackTrace();
}
return imageList;
}
private Bitmap getImageFromAsset(String path, AssetManager assetManager) {
InputStream istr = null;
try {
istr = assetManager.open(path);
} catch (IOException e) {
e.printStackTrace();
}
Bitmap bitmap = BitmapFactory.decodeStream(istr);
return bitmap;
}
如果您只需要这些图片的路径,请停在fileList = am.list(dirFrom);
。不要忘记在fileName之前添加dirName。
根据评论中的要求编辑:
Resources res = getResources();
AssetManager am = res.getAssets();
String fileList[];
try {
fileList = am.list(dirFrom);
for (String fileName : fileList) {
fileName = dirFrom + File.separator + fileName;
}
} catch (IOException e) {
e.printStackTrace();
}
现在,您拥有assets / dirName中的所有文件路径。现在转换为ArrayList;
List<String> imagePaths = new ArrayList<String>(Arrays.asList(fileList));
现在图像路径在列表中。