getContext()。getContentResolver()。query(NullPointerException)

时间:2017-02-06 14:31:09

标签: android exception

在我的片段中,我正在打开画廊。但我想知道我选择的图片的真实路径是什么。我在onActivityResult中获得了我的图片URI,如下所示:

   @Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == Activity.RESULT_OK) {
        if (requestCode == SELECT_FILE) {
            onSelectFromGalleryResult(data);
        }
    }
}


    public void onSelectFromGalleryResult(Intent data) {
    if (data != null) {
        bm = null;

        try {
            selectedImageUri = data.getData();
            bm = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), data.getData());
        } catch (IOException e) {
            e.printStackTrace();
        }

        Log.d(TAG, "selectedImageUri " + selectedImageUri);

        fragmentEditPicture.setImageBitmap(bm);

        File finalFile = new File(getRealPathFromURI(selectedImageUri));
        Log.d("===>", "getRealPathFromURI: " + finalFile);
    }

}

我得到了我的图片URI。我在这个sait中找到了这个方法。 在这个方法中getRealPathFromURI(selectedImageUri)我将URI传递给方法。这个方法的完成实现是:

    public String getRealPathFromURI(Uri uri) {
    Cursor cursor = getContext().getContentResolver().query(uri, null, null, null, null);
    cursor.moveToFirst();
        idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);

    return cursor.getString(idx);
}

但是当我选择我的照片时,我收到了这个错误:

FATAL EXCEPTION: main
                                                                               java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=65536, result=-1, data=Intent { dat=file:///mnt/sdcard/Download/139175_(2560x1600).JPG }} to activity {com.example.sayres.myapplication7/com.example.sayres.myapplication7.mvp.view.profile.ProfileActivity}: java.lang.NullPointerException
                                                                                   at android.app.ActivityThread.deliverResults(ActivityThread.java:3141)
                                                                                   at android.app.ActivityThread.handleSendResult(ActivityThread.java:3184)
                                                                                   at android.app.ActivityThread.access$1100(ActivityThread.java:130)
                                                                                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1243)
                                                                                   at android.os.Handler.dispatchMessage(Handler.java:99)
                                                                                   at android.os.Looper.loop(Looper.java:137)
                                                                                   at android.app.ActivityThread.main(ActivityThread.java:4745)
                                                                                   at java.lang.reflect.Method.invokeNative(Native Method)
                                                                                   at java.lang.reflect.Method.invoke(Method.java:511)
                                                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
                                                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
                                                                                   at dalvik.system.NativeStart.main(Native Method)
                                                                                Caused by: java.lang.NullPointerException
                                                                                   at com.example.sayres.myapplication7.mvp.view.profile.EditProfileFragment.onSelectFromGalleryResult(EditProfileFragment.java:159)
                                                                                   at com.example.sayres.myapplication7.mvp.view.profile.EditProfileFragment.onActivityResult(EditProfileFragment.java:136)
                                                                                   at android.support.v4.app.FragmentActivity.onActivityResult(FragmentActivity.java:176)
                                                                                   at android.app.Activity.dispatchActivityResult(Activity.java:5192)
                                                                                   at android.app.ActivityThread.deliverResults(ActivityThread.java:3137)
                                                                                   at android.app.ActivityThread.handleSendResult(ActivityThread.java:3184) 
                                                                                   at android.app.ActivityThread.access$1100(ActivityThread.java:130) 
                                                                                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1243) 
                                                                                   at android.os.Handler.dispatchMessage(Handler.java:99) 
                                                                                   at android.os.Looper.loop(Looper.java:137) 
                                                                                   at android.app.ActivityThread.main(ActivityThread.java:4745) 
                                                                                   at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                                   at java.lang.reflect.Method.invoke(Method.java:511) 
                                                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 
                                                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
                                                                                   at dalvik.system.NativeStart.main(Native Method) 

错误就在这一行 如您所见,这是我的URI地址:dat=file:///mnt/sdcard/Download/139175_(2560x1600).JPG

但是我得到了什么错误?

2 个答案:

答案 0 :(得分:0)

正确添加onActivityResult代码。交换步骤以获取数据,如果代码有效,请首先检查有效requestCode,然后检查responce。你做的正好相反。

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data){

    if (requestCode == SELECT_FILE) {
        if (resultCode == Activity.RESULT_OK) {
            onSelectFromGalleryResult(data);
        }
    }
}

内部方法:

public void onSelectFromGalleryResult(Intent data) {
if (data != null) {
    final Bitmap bm;

    try {
        selectedImageUri = data.getData();
        bm = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), selectedImageUri);
    } catch (IOException e) {
        e.printStackTrace();
    }

   .......
     .....

答案 1 :(得分:0)

我的代码正在运行(我已经评论过这是你需要的东西'):

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    // Here we need to check if the activity that was triggers was the Image Gallery.
    // If it is the requestCode will match the LOAD_IMAGE_RESULTS value.
    // If the resultCode is RESULT_OK and there is some data we know that an image was picked.
    if (requestCode == SELECT_PHOTO && resultCode == RESULT_OK && data != null) {
        // Let's read picked image data - its URI
        Uri pickedImage = data.getData();
        // Let's read picked image path using content resolver
        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]));

//THIS IS THE TOAST YOU NEED
        Toast.makeText(this, "" + imagePath, Toast.LENGTH_SHORT).show();

BitmapFactory.Options options = new BitmapFactory.Options();
        options.inPreferredConfig = Bitmap.Config.ARGB_8888;
        Bitmap bitmap = BitmapFactory.decodeFile(imagePath, options);
        img.setImageBitmap(bitmap);

        // Do something with the bitmap


        // At the end remember to close the cursor or you will end with the RuntimeException!
        cursor.close();
    }
  }