2
我使用下面的代碼來獲取照片路徑,在使用照相機拍照後保存圖像,並且只在Nexus 5上的Android 6.0上發生NullPointerException。 在升級到Android 6.0在相同的手機上運行良好。android 6.0 NullPointerException獲取照片路徑
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;
}
它拋出內部 DocumentsContract.isDocumentUri(context, uri)
UPDATE
一個NullPointerException我發現,問題出在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);
不......它在#isDocumentUri方法內的某個地方拋出NullPointerException異常。附上至少部分日誌輸出...是不可能的上下文或uri爲空? – VizGhar
@VizGhar這似乎是最明智的,但如果是這樣,這是糟糕的API設計,它會更好,如果'DocumentsContract.isDocumentUri'檢查爲空參數並拋出IllegalArgumentException – NickJ
如果它是在函數內部的問題,那麼它是在' DocumentContract.isDocumentUri - > uri.getPathSegments();或'DocumentContract.isDocumentsProvider - > context.getPackageManager()' – VizGhar