gpt4 book ai didi

java - 获取照片路径时的android 6.0 NullPointerException

转载 作者:行者123 更新时间:2023-11-30 10:55:56 25 4
gpt4 key购买 nike

我使用以下代码获取在使用相机拍照后保存图像的照片路径,并且仅在 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(上下文, 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);

最佳答案

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

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

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

关于java - 获取照片路径时的android 6.0 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33166693/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com