我想在viewHolder imageview中设置图像,我在适配器中发送图像路径,我不能在 imageview中设置,因为我有转换为空值,我不能将文件路径转换为位图,请帮助我。
if(dataModel.getPhoto()!=null)
{
BitmapFactory.Options options = new BitmapFactory.Options();
Bitmap bm = BitmapFactory.decodeFile(dataModel.getPhoto(), options);
Log.e("decodefile",":"+bm);
viewHolder.photo.setImageBitmap(bm);
}
你没有正确使用BitmapFactory.Options试试这个:-。
public static Bitmap decodeBitmapFromFile(String filePath, int reqWidth,
int reqHeight) {
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(filePath, options);
options.inSampleSize = calculateInSampleSize(options, reqWidth,
reqHeight);
options.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(filePath, options);
}
public static int calculateInSampleSize(BitmapFactory.Options options,
int reqWidth, int reqHeight) {
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
final int heightRatio = Math.round((float) height
/ (float) reqHeight);
final int widthRatio = Math.round((float) width / (float) reqWidth);
inSampleSize = heightRatio <= widthRatio ? heightRatio : widthRatio;
}
return inSampleSize;
}
你能分享一些更多的细节吗?
可能是你的路径不正确.你是否使用了BitmapFactory.选项?绝对路径 的 形象如果你不使用它,就把它改为 绝对路径.