获取照片路径时的android 6.0 NullPointerException

时间:2015-10-16 09:17:12

标签: java android android-6.0-marshmallow

我使用以下代码获取使用相机拍照后保存图像的照片路径,并且仅在Nexus 5上的Android 6.0上获得NullPointerException。 在同一部手机上升级到Android 6.0之前,它运行良好。

final Uri selectedImageUri =  data == null ? null : data.getData();
String selectedImagePath = null;
selectedImagePath = getPath(getActivity(), selectedImageUri);


@TargetApi(19)
public static String getPath(final Context context, final Uri uri) {

    final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;

    // DocumentProvider
    if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {
        // ExternalStorageProvider
        if (isExternalStorageDocument(uri)) {
            final String docId = DocumentsContract.getDocumentId(uri);
            final String[] split = docId.split(":");
            final String type = split[0];

            if ("primary".equalsIgnoreCase(type)) {
                return Environment.getExternalStorageDirectory() + "/" + split[1];
            }
        }
        // DownloadsProvider
        else if (isDownloadsDocument(uri)) {

            final String id = DocumentsContract.getDocumentId(uri);
            final Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));

            return getDataColumn(context, contentUri, null, null);
        }
        // MediaProvider
        else if (isMediaDocument(uri)) {
            final String docId = DocumentsContract.getDocumentId(uri);
            final String[] split = docId.split(":");
            final String type = split[0];

            Uri contentUri = null;
            if ("image".equals(type)) {
                contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
            } else if ("video".equals(type)) {
                contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
            } else if ("audio".equals(type)) {
                contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
            }

            final String selection = "_id=?";
            final String[] selectionArgs = new String[] {
                    split[1]
            };

            return getDataColumn(context, contentUri, selection, selectionArgs);
        }
    }
    // MediaStore (and general)
    else if ("content".equalsIgnoreCase(uri.getScheme())) {

        // Return the remote address
        if (isGooglePhotosUri(uri))
            return uri.getLastPathSegment();

        return getDataColumn(context, uri, null, null);
    }
    // File
    else if ("file".equalsIgnoreCase(uri.getScheme())) {
        return uri.getPath();
    }

    return null;
}

它会抛出一个NullPointerException  DocumentsContract.isDocumentUri(context, uri)

更新

我发现问题在于uri为空。 这里是检索URI的代码

@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST_TAKE_PHOTO){
        if( resultCode == Activity.RESULT_OK) {

            final boolean isCamera;
            if(data == null)
            {
                isCamera = true;
            }
            else
            {
                final String action = data.getAction();
                if(action == null)
                {
                    isCamera = false;
                }
                else
                {
                    isCamera = action.equals(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                }
            }

            String createdImagePath = null;
            if(isCamera)
            {
                createdImagePath = mCallbacks.getCurrentPhotoPath();
            }
            else
            {
                final Uri selectedImageUri =  data == null ? null : data.getData();
                String selectedImagePath = null;

                selectedImagePath = getPath(getActivity(), selectedImageUri);

1 个答案:

答案 0 :(得分:1)

也许我找到了解决方案。 我必须在这里添加另一个空检查:

                if(data == null || data.getData() == null)

看起来在Android 6.0上,通过相机拍摄照片后返回的Intent与之前的操作系统版本不同,但它不包含任何数据。 因此,检查数据是否为空,就像我现在所做的那样还不够。