在我的应用中,用户可以从图库中选择一张照片,裁剪并设置为他们的头像。 当他们注册时,我将这张照片发送到服务器。
为了发送它,我必须首先获得它,但我得到一个错误。
java.lang.RuntimeException: Failure delivering result ResultInfo
{who=null, request=2, result=-1, data=Intent { act=inline-data (has extras) }}
to activity {net.asdasd.asdasd/net.asdasd.activities.Signup}: java.lang.NullPointerException
这是我的代码:
bSelectPhotoFromFileSignupPage.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Complete action using"), PICK_FROM_FILE);
dialog.dismiss();
}
});
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode != RESULT_OK) return;
switch (requestCode) {
case PICK_FROM_CAMERA:
cropImage();
break;
case PICK_FROM_FILE:
mImageCaptureUri = data.getData();
cropImage();
break;
case CROP_FROM_CAMERA:
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
setAvatarPath(cursor.getString(columnIndex));
// Convert file path into bitmap image using below line.
userAvatarBitmap = BitmapFactory.decodeFile(userAvatarPath);
//Set the button image to be the newly created image instead of the default
iUserAvatar.setImageBitmap(userAvatarBitmap);
//delete the temporary file
File temporaryAvatarFile = new File(mImageCaptureUri.getPath());
if (temporaryAvatarFile.exists()) temporaryAvatarFile.delete();
break;
}
}
public class CropOption {
public CharSequence title;
public Drawable icon;
public Intent appIntent;
}
public class CropOptionAdapter extends ArrayAdapter<CropOption> {
private ArrayList<CropOption> options;
private LayoutInflater inflater;
private int imageView;
public CropOptionAdapter(Context context, ArrayList<CropOption> options, int imageView) {
super(context, R.layout.activity_layout_signup, options);
this.options = options;
this.imageView = imageView;
this.inflater = LayoutInflater.from(context);
}
@Override
public View getView(int position, View convertView, ViewGroup group) {
if (convertView == null)
convertView = inflater.inflate(R.layout.activity_layout_signup, null);
CropOption item = options.get(position);
if (item != null) {
((ImageView) convertView.findViewById(imageView)).setImageDrawable(item.icon);
return convertView;
}
return null;
}
以下是整个日志:
FATAL EXCEPTION: main
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=2, result=-1, data=Intent { act=inline-data (has extras) }} to activity {net.asd.asd/net.asd.activities.Signup}: java.lang.NullPointerException
at android.app.ActivityThread.deliverResults(ActivityThread.java:2536)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:2578)
at android.app.ActivityThread.access$2000(ActivityThread.java:117)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:965)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3691)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.content.ContentResolver.acquireProvider(ContentResolver.java:743)
at android.content.ContentResolver.query(ContentResolver.java:256)
at net.asd.activities.Signup.onActivityResult(Signup.java:357)
at android.app.Activity.dispatchActivityResult(Activity.java:3934)
at android.app.ActivityThread.deliverResults(ActivityThread.java:2532)
... 11 more
第357行如下:
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
答案 0 :(得分:1)
您应该检查空值,因为getData方法可以返回null:
Uri selectedImage = data.getData();
if(selectedImage != null){
...
}