我在从图库中选取图像并设置为 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/