【问题标题】:Android OS Calling onActivityResult before picking up image from galleryAndroid OS 在从图库中获取图像之前调用 onActivityResult
【发布时间】:2015-04-21 03:04:24
【问题描述】:

我在从图库中挑选图片并设置为 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 上测试。

【问题讨论】:

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


    【解决方案1】:

    检查这个: onActivityResult return before pick up image

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

    【讨论】:

      猜你喜欢
      • 2021-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多