我无法在 android 的取景器图像中设置图像
i can't set image in viewholder image in android
我正在尝试在 viewHolder imageview 中设置图像,我在 Adapter 中发送图像路径我无法在 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);
}
你能分享更多细节吗?
可能是你的路径是incorrect.Are你使用图像的绝对路径?
如果您不使用它,请将其更改为 绝对路径.
您没有正确使用 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;
}
我正在尝试在 viewHolder imageview 中设置图像,我在 Adapter 中发送图像路径我无法在 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);
}
你能分享更多细节吗?
可能是你的路径是incorrect.Are你使用图像的绝对路径? 如果您不使用它,请将其更改为 绝对路径.
您没有正确使用 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;
}