【发布时间】:2014-07-17 20:55:48
【问题描述】:
我需要从图库中获取图像,并将其转换为位图。
这是发送 Intent 的代码:
Intent i = new Intent(
Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
onActivityResult :
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == mResultLoadImage && resultCode == RESULT_OK && data != null) {
Uri pickedImage = data.getData();
String[] filePath = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(pickedImage, filePath,
null, null, null);
cursor.moveToFirst();
String imagePath = cursor.getString(cursor
.getColumnIndex(filePath[0]));
bitmap = BitmapFactory.decodeFile(imagePath);
someFunction(bitmap);
cursor.close();
}
}
我总是得到一个 NullPointerException,并且使用调试器,我发现数据 Intent 为空。 有什么解决办法吗?
【问题讨论】:
-
图像选择器意图是否打开?
-
是的,Intent 选择器打开了,但是当我选择图像时,它给了我错误。
-
@mvd3 你找到解决办法了吗?
标签: java android android-intent