我正在使用浏览按钮获取图片文件路径……之后,我想使用该文件路径将该图像设置到图像视图中
我正在使用浏览按钮获取图片文件路径……之后,我想使用该文件路径将该图像设置到图像视图中
如果你指的是一个File
对象,我会尝试:
File file = ....
Uri uri = Uri.fromFile(file);
imageView.setImageURI(uri);
imageView.setImageBitmap(BitmapFactory.decodeFile(yourFilePath));
BitmapFactory会将给定的图像文件解码成一个Bitmap对象,然后您将该对象设置到imageView对象中。
file.getPath()
而不是 yourFilePath
。 - user2052706 File file = new File(Environment.getExternalStorageDirectory()+File.separator + "image.jpg"); //your image file path
mImage = (ImageView) findViewById(R.id.imageView1);
mImage.setImageBitmap(decodeSampledBitmapFromFile(file.getAbsolutePath(), 500, 250));
当调用decodeSampledBitmapFromFile
函数时:
public static Bitmap decodeSampledBitmapFromFile(String path,
int reqWidth, int reqHeight) { // BEST QUALITY MATCH
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, options);
// Calculate inSampleSize
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
options.inPreferredConfig = Bitmap.Config.RGB_565;
int inSampleSize = 1;
if (height > reqHeight) {
inSampleSize = Math.round((float)height / (float)reqHeight);
}
int expectedWidth = width / inSampleSize;
if (expectedWidth > reqWidth) {
//if(Math.round((float)width / (float)reqWidth) > inSampleSize) // If bigger SampSize..
inSampleSize = Math.round((float)width / (float)reqWidth);
}
options.inSampleSize = inSampleSize;
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(path, options);
}
您可以更改数字(在此示例中为500和250)以更改 ImageView
的位图质量。
Bitmap bitmap = BitmapFactory.decodeFile(pathToPicture);
pathToPicture
路径正确,您可以将此位图图像添加到ImageView
中,如下所示:ImageView imageView = (ImageView) getActivity().findViewById(R.id.imageView);
imageView.setImageBitmap(BitmapFactory.decodeFile(pathToPicture));