【发布时间】:2016-04-13 06:06:40
【问题描述】:
我有 2 个选项来设置图像,从图库中选择或捕获图像。
当用户从图库中选择图像时,它会返回一个叮当的 ImageView,当用户在捕获图像后尝试设置图像时,应用程序崩溃并出现以下错误:java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=0, result=-1, data=Intent { act=inline-data (has extras) }} to activity {com.abc.xyz/com.abc.xyz.Activity}: java.lang.NullPointerException: uri
这是我启动选择器的方式:
protected DialogInterface.OnClickListener mDialogListener = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int position) {
switch (position) {
case 0: // Take picture
Intent takePhotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(takePhotoIntent, TAKE_PHOTO_REQUEST);
break;
case 1: // Choose picture
Intent choosePhotoIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
choosePhotoIntent.setType("image/*");
startActivityForResult(choosePhotoIntent, PICK_PHOTO_REQUEST);
break;
}
}
};
这是我将图像设置到 ImageView 的方式:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK) {
if (requestCode == PICK_PHOTO_REQUEST || requestCode == TAKE_PHOTO_REQUEST) {
if (data == null) {
// display an error
return;
}
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
// error on the line below
Cursor cursor = this.getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
//
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
Picasso.with(this)
.load(picturePath)
.into(hPic);
hPicTag.setVisibility(View.INVISIBLE);
}
} else if (resultCode == Activity.RESULT_CANCELED) {
Toast.makeText(getBaseContext(), "Something went wrong!", Toast.LENGTH_LONG).show();
}
}
请告诉我这里出了什么问题。
抱歉,问题的格式不正确。我还是个初学者。
【问题讨论】:
-
看这个对你有帮助..stackoverflow.com/questions/33938665/…
-
这个答案对我有用:stackoverflow.com/a/5991757/6144372
标签: android nullpointerexception uri picasso android-cursor