[英]Picking a photo from gallery and show in a image view
我有一个应用程序,它有一个按钮,可以从您的图库中选择一张照片并且工作正常,选择图像后,我的应用程序显示回到活动并在图像视图中显示图像。
每个都工作正常,但有时,当我选择一些特定的图像时,预览没有显示。 我也试图压缩图像仍然无法正常工作
我的代码在下面.. 在onCreate()
galeryBtn=(Button)findViewById(R.id.buttonGallery);
galeryBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
}
});
在onActivityResult中(int requestCode,int resultCode,Intent data)
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
// String picturePath contains the path of selected Image
// Show the Selected Image on ImageView
ImageView imageView = (ImageView) findViewById(R.id.imgView);
imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}
试试这样吧
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
switch (requestCode) {
case RESULT_LOAD_IMAGE:
if (resultCode == Activity.RESULT_OK) {
Uri selectedImage = intent.getData();
try {
Bitmap bitmapImage =decodeBitmap(selectedImage );
} catch (FileNotFoundException e) {
e.printStackTrace();
}
// Show the Selected Image on ImageView
ImageView imageView = (ImageView) findViewById(R.id.imgView);
imageView.setImageBitmap(bitmapImage);
}
和
public Bitmap decodeBitmap(Uri selectedImage) throws FileNotFoundException {
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(getContentResolver().openInputStream(selectedImage), null, o);
final int REQUIRED_SIZE = 100;
int width_tmp = o.outWidth, height_tmp = o.outHeight;
int scale = 1;
while (true) {
if (width_tmp / 2 < REQUIRED_SIZE || height_tmp / 2 < REQUIRED_SIZE) {
break;
}
width_tmp /= 2;
height_tmp /= 2;
scale *= 2;
}
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
return BitmapFactory.decodeStream(getContentResolver().openInputStream(selectedImage), null, o2);
}
我遇到类似的问题,如从资源,开放流,设置位图等获取游标uri。它一直有bug。
所以我搜索了库并找到了image-chooser-library库。
我打赌你想从image-chooser-library尝试这个项目
它非常易于使用并为您解决所有这些细节问题,例如来自picasa等的图像。
希望它对你有用。
你试图在onActivityResult()
加载位图的方式并不是绝对正确的。 有时您将无法打开图像,您的应用程序可能会崩溃。 你最好使用这样的代码:
Uri imageUri = data.getData();
InputStream imageStream = null;
try {
imageStream = getContentResolver().openInputStream(imageUri);
ImageView imageView = (ImageView) findViewById(R.id.imgView);
imageView.setImageBitmap(BitmapFactory.decodeStream(imageStream));
} catch (FileNotFoundException e) {
// Handle the error
} finally {
if (imageStream != null) {
try {
imageStream.close();
} catch (IOException e) {
// Ignore the exception
}
}
}
在imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath))之后添加;
imageView.setImageURI(selectedImage);
它对我有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.