请参阅下面的EDIT 1
我正在使用此类FileUtils
处理Uri
:
public class FileUtils {
private FileUtils() {
}
private static final String TAG = "FileUtils";
private static final boolean DEBUG = false;
private static boolean isExternalStorageDocument(Uri uri) {
return "com.android.externalstorage.documents".equals(uri.getAuthority());
}
private static boolean isDownloadsDocument(Uri uri) {
return "com.android.providers.downloads.documents".equals(uri.getAuthority());
}
private static boolean isMediaDocument(Uri uri) {
return "com.android.providers.media.documents".equals(uri.getAuthority());
}
private static boolean isGooglePhotosUri(Uri uri) {
return "com.google.android.apps.photos.content".equals(uri.getAuthority());
}
private static String getDataColumn(Context context, Uri uri, String selection,
String[] selectionArgs) {
Cursor cursor = null;
final String column = "_data";
final String[] projection = {
column
};
try {
cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs,
null);
if (cursor != null && cursor.moveToFirst()) {
if (DEBUG)
DatabaseUtils.dumpCursor(cursor);
final int column_index = cursor.getColumnIndexOrThrow(column);
return cursor.getString(column_index);
}
} finally {
if (cursor != null)
cursor.close();
}
return null;
}
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
public static String getPath(final Context context, final Uri uri) {
if (DEBUG)
Log.d(TAG + " File -",
"Authority: " + uri.getAuthority() +
", Fragment: " + uri.getFragment() +
", Port: " + uri.getPort() +
", Query: " + uri.getQuery() +
", Scheme: " + uri.getScheme() +
", Host: " + uri.getHost() +
", Segments: " + uri.getPathSegments().toString()
);
final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
// DocumentProvider
if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {
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;
}
从设备中选择视频后,我在MainActivity
内的onActivityResult
中调用它:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == SELECT_VIDEO_REQUEST && resultCode == RESULT_OK) {
if (Build.VERSION.SDK_INT >= 19) {
//Calling FileUtils class
String sourcePath = FileUtils.getPath(getApplicationContext(), data.getData());
Intent intent = new Intent();
intent.setClass(MainActivity.this, PlayerActivity.class);
intent.putExtra("videoUri", sourcePath);
startActivity(intent);
} else {
//Not relevant to the question
.......
}
}
if (requestCode == SELECT_VIDEO_REQUEST && resultCode != RESULT_OK) {
Toast.makeText(getApplicationContext(), "Failed to select video", Toast.LENGTH_LONG).show();
} else {
super.onActivityResult(requestCode, resultCode, data);
}
}
在我的PlayerActivity
中,我得到String
并尝试解析Uri
,如下所示:
//Getting String from Intent
mStringFilePath = getIntent().getStringExtra("videoUri");
//Parse Uri from String
mVideoUri = Uri.parse(mStringFilePath);
在我的设备上进行测试时,我没有任何问题。我得到正确的路径,并且Uri.parse
工作正常。但是我在我的crashlytics上看到了很多崩溃,他们说-nullpointerexception at line 59
,指的是mVideoUri = Uri.parse(mStringFilePath);
有人可以告诉我我做错了什么吗?为什么它可以在我的设备上运行,而在其他一些设备上返回null?
编辑1:
我忘记在清单中添加<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
。我到了至少无法获得nullpointerexception
的地步,但是我得到了以下内容-
java.io.FileNotFoundException: Not found (No such file or directory)
当我从SD卡中选择文件时,Uri
不正确是什么原因?
答案 0 :(得分:0)
崩溃可能是由于mStringFilePath = getIntent().getStringExtra("videoUri");
上的null字符串引起的,因为getIntent().getStringExtras()
不会在请求的Extras没有值的情况下抛出nullpointerexception异常,但也不会将数据提供给分配的变量mStringFilePath
的最终结果将为null以解决此问题,请确保将uri解析的给定字符串包装在以下位置:
if(mStringFilePath!= null){mVideoUri = Uri.parse(mStringFilePath); }