【发布时间】:2016-07-19 11:44:51
【问题描述】:
我的本地存储中的文件夹中有 20k 个图像文件,我需要通过本地存储中的文件名显示图像。怎么做?我在这里附上了我的代码:
if (Environment.getExternalStorageState()
.equals(Environment.DIRECTORY_PICTURES)) {
String filePath = File.separator + "sdcard" + File.separator + "Android" + File.separator + "obb" + File.separator + "convertMp3ToDb" + File.separator + "ldoce6pics" + File.separator + fileName;
try {
FileInputStream fis = new FileInputStream(filePath);
String entry = null;
while ((entry = fis.toString()) != null) {
if (!entry.toString().isEmpty()) {
File Mytemp = File.createTempFile("TCL", "jpg", getContext().getCacheDir());
Mytemp.deleteOnExit();
FileOutputStream fos = new FileOutputStream(Mytemp);
for (int c = fis.read(); c != -1; c = fis.read()) {
try {
fos.write(c);
} catch (IOException e) {
e.printStackTrace();
}
}
fos.close();
FileInputStream MyFile = new FileInputStream(Mytemp);
final Bitmap bit = BitmapFactory.decodeStream(MyFile);
imageView.setImageBitmap(bit);
}
}
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
【问题讨论】:
-
显示你的 logcat 日志
-
尝试 1. 使用 Glide 之类的图像库(还有其他的我更喜欢 glide) - 将文件路径从字符串转换为 uri 2. 您也可以使用加载器进行相同的设置位图
-
做一些日志并发布
-
没有 logcat,错误但他们没有执行任何操作,图像没有显示
-
您正在使用 try catch。删除它并再次检查日志。该块中可能发生错误。
标签: android image android-studio local-storage fileinputstream