我正在使用浏览按钮获取图像的文件路径....之后我想使用文件路径将此图像设置为图像视图
问问题
55335 次
4 回答
41
如果File
你的意思是一个File
对象,我会尝试:
File file = ....
Uri uri = Uri.fromFile(file);
imageView.setImageURI(uri);
于 2013-04-04T14:56:12.230 回答
9
你可以试试这段代码:
imageView.setImageBitmap(BitmapFactory.decodeFile(yourFilePath));
BitmapFactory 会将给定的图像文件解码为 Bitmap 对象,然后将其设置到 imageView 对象中。
于 2013-04-04T14:57:54.083 回答
8
要从文件中设置图像,您需要执行以下操作:
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
.
于 2013-04-04T15:00:32.100 回答
2
要从文件加载图像:
Bitmap bitmap = BitmapFactory.decodeFile(pathToPicture);
假设您pathToPicture
是正确的,然后您可以将此位图图像添加到ImageView
类似
ImageView imageView = (ImageView) getActivity().findViewById(R.id.imageView);
imageView.setImageBitmap(BitmapFactory.decodeFile(pathToPicture));
于 2017-01-03T05:52:24.350 回答