我尝试从图库中显示位图。这是我的网址
file:///storage/emulated/0/DCIM/Camera/IMG_20161103_180603.jpg
我知道如何使用onActivityResult()
获取位图,但我不知道如何获取位图
这是我的来源
final ImageView imageView=(ImageView)findViewById(R.id.imageView);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
final Bitmap bitmap = BitmapFactory.decodeFile("file:///storage/emulated/0/DCIM/Camera/IMG_20161103_180603.jpg", options);
runOnUiThread(new Runnable() {
@Override
public void run() {
imageView.setImageBitmap(bitmap);
}
});
如何解决我的问题?
答案 0 :(得分:0)
请参阅以下代码
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
//Get our saved file into a bitmap object:
File file = new File(Environment.getExternalStorageDirectory()+File.separator + "DCIM"+ File.separator + "Camera "+File.separator + "IMG_20161103_180603.jpg");
Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath(), options);
imageView.setImageBitmap(bitmap);
答案 1 :(得分:0)
由于无法在运行时处理大图像尺寸而导致同样的问题。用它来解决它。
// Decodes image and scales it to reduce memory consumption
public static Bitmap decodeFile(File f) {
try {
// Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(new FileInputStream(f), null, o);
// The new size we want to scale to
final int REQUIRED_SIZE = 200;
// Find the correct scale value. It should be the power of 2.
int scale = 1;
while (o.outWidth / scale / 2 >= REQUIRED_SIZE &&
o.outHeight / scale / 2 >= REQUIRED_SIZE) {
scale *= 2;
}
// Decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return null;
}
使用https://stackoverflow.com/a/20559418/3758972
检索File
f
只需将您想要的缩略图大小(通常是您的imageview的大小)放在REQUIRED_SIZE
中即可。它会为您提供一个缩放图像,您可以将其设置为imageview
。
答案 2 :(得分:0)
decodeFile( "file:///storage/emulated/0/DCIM/Camera/IMG_20161103_180603.jpg
更改为:
decodeFile( "/storage/emulated/0/DCIM/Camera/IMG_20161103_180603.jpg