当我调用此函数时,图像视图bitmapFactory.decodefile(filename)
中没有图像显示为null。请帮帮忙。
下面是我的代码:
public Bitmap ShowImage(String imageName,String userImageName )
{
File sdcard_mainDirectory = new File(Environment.getExternalStorageDirectory(),"UserImages").getAbsoluteFile();
File file = new File(sdcard_mainDirectory, userImageName).getAbsoluteFile();
if (file != null) {
try {
String imageInSD = "/sdcard/UserImages/"+userImageName;
Bitmap bitmap = BitmapFactory.decodeFile(imageInSD);
return bitmap;
}
catch (Exception e) {
e.printStackTrace();
}
}
return null;
}
发布于 2011-12-09 16:06:02
您好,这是null
,因为可能是图像大小和得到异常,请检查您的日志,看看是否有任何错误的输出内存位图,如果是,然后使用选项:
BitmapFactory.Options options;
try {
String imageInSD = "/sdcard/UserImages/" + userImageName;
Bitmap bitmap = BitmapFactory.decodeFile(imageInSD);
return bitmap;
} catch (OutOfMemoryError e) {
try {
options = new BitmapFactory.Options();
options.inSampleSize = 2;
Bitmap bitmap = BitmapFactory.decodeFile(imageInSD, null, options);
return bitmap;
} catch(Exception excepetion) {
Log.e(excepetion);
}
}
发布于 2015-03-31 20:55:08
尝试添加SD卡访问权限READ_EXTERNAL_STORAGE和/或WRITE_EXTERNAL_STORAGE。这对我很管用。
发布于 2020-06-24 21:12:27
如果您正尝试在Android10中尝试在清单android:requestLegacyExternalStorage="true"
中将其添加到您的应用程序中,这可能是Android10的权限问题,因为它不允许直接访问文件
https://stackoverflow.com/questions/8442316
复制相似问题