Android 操作系统在从图库中获取图像之前调用 onActivityResult

标签 android imageview android-imageview android-gallery android-bitmap

我在从图库中选取图像并设置为 ImageView 时遇到了一个奇怪的问题。早些时候,我的代码工作正常,但是当我现在测试它时,它会在从图库中选择图像之前进入onActivityResult。下面是我的代码:

调用图库的代码:

  Intent intent = new Intent();
  intent.setType("image/*");
  intent.setAction(Intent.ACTION_GET_CONTENT);
  startActivityForResult(Intent.createChooser(intent, "Select Picture"),
    PICK_IMAGE);

内部代码onActivityResult

 if (requestCode == PICK_IMAGE && resultCode == Activity.RESULT_OK
   && null != data) {
  Uri selectedImage = data.getData();
  String[] filePathColumn = { MediaStore.Images.Media.DATA };

  Cursor cursor = getActivity().getContentResolver().query(selectedImage,
    filePathColumn, null, null, null);
  cursor.moveToFirst();

  int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
  String picturePath = cursor.getString(columnIndex);
  cursor.close();

  decodeFile(picturePath);

 }

decodeFile函数的代码:

public void decodeFile(String filePath) {
 // Decode image size
 BitmapFactory.Options o = new BitmapFactory.Options();
 o.inJustDecodeBounds = true;
 BitmapFactory.decodeFile(filePath, o); 

 // The new size we want to scale to
 final int REQUIRED_SIZE = 2048;

 // Find the correct scale value. It should be the power of 2.
 int width_tmp = o.outWidth, height_tmp = o.outHeight;
 int scale = 1;
 while (true) {
  if (width_tmp < REQUIRED_SIZE && height_tmp < REQUIRED_SIZE)
   break;
  width_tmp /= 2;
  height_tmp /= 2;
  scale *= 2;
 }

 // Decode with inSampleSize
 BitmapFactory.Options o2 = new BitmapFactory.Options();
 o2.inSampleSize = scale;
 bitmap = BitmapFactory.decodeFile(filePath, o2);

 customerIdProofUrlImageView.setImageBitmap(bitmap);
}

不知道问题出在哪里。在 Galaxy Grand 和 Micromax A104 上进行了测试。

最佳答案

检查一下: onActivityResult return before pick up image

尝试删除 list 文件中的 android:launchMode="singleInstance" 行。

关于Android 操作系统在从图库中获取图像之前调用 onActivityResult,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29762155/

相关文章:

android - 具有复杂数据和每行复杂布局的复杂 ListView 示例?

android - AdMob 测试 ID 有效但真实 ID 无效

android - 将 ImageView 设置为后台 Android

java - Android ListView - onItemClickListener

android - 如何知道两张图片是否相交而一张图片在android中移动?

android - 小部件上的方向更改按钮没有响应后

java - 将自定义弹出窗口/ View 添加到 android 中的 Activity

java - ImageView 像另一个 ImageView Android 的边框

java - 从外部存储读取图像文件并放置在 ImageView 中

android - 设置折叠工具栏中 TextView 和 ImageView 的对比度颜色