【发布时间】:2015-01-12 05:31:37
【问题描述】:
我遇到了奇怪的行为。我正在尝试使用 Intent 调出画廊。然后 onActivityResult() 我想获取 Uri 并将其转换为位图。我面临的问题是首先选择图像会导致我的手机冻结几秒钟。然后当它返回到 onActivityResult() 时,resultCode 值是-1,即使我选择了一个图像。我的步骤中是否遗漏了什么?
启动图库意图
Intent intent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, GALLERY_REQUEST_CODE);
我的活动结果
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == GALLERY_REQUEST_CODE) {
Uri selectedImageUri = data.getData();
if (selectedImageUri != null) {
try {
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(
selectedImageUri, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String filePath = cursor.getString(columnIndex);
cursor.close();
Bitmap mBitmap = BitmapFactory.decodeFile(filePath);
LogUtil.e(TAG, "mBitmap: " + mBitmap);
ByteArrayOutputStream bs = new ByteArrayOutputStream();
mBitmap.compress(Bitmap.CompressFormat.PNG, 50, bs);
Intent intent = new Intent(CameraActivity.this,
SecondActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP
| Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("image", bs.toByteArray());
startActivity(intent);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} else {
return;
}
}
更新: 我在清单中有权限。我也收到了正确的 Uri。但是在选择图像后,我总是收到 resultCode = -1。我似乎收到了失败的活页夹交易。
我可能缺少任何权限吗?提前谢谢!
【问题讨论】:
-
@Praveen,这是我在编程时引用的代码。没关系,因为选择图像后我的 resultCode 为 -1。我无法获得 OK 状态值
标签: android android-intent android-gallery