在解析Intent.ACTION_GET_CONTENT
的结果时:
Intent intent = new Intent();
intent.setType("video/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(
Intent.createChooser(intent, "Select Video"), READ_REQUEST_CODE);
在SDK 27
上,我收到:
dat=content://com.android.providers.media.documents/document/video:14691
在SDK 23
上:
dat=content://com.android.externalstorage.documents/document/primary:some_dir/VID-20171013-WA0010_repaired.mp4
我正在使用RealPathUtils.class
来获取文件的绝对路径。它可以在大多数经过测试的设备上运行,但是在SDK 25
上无法运行。
RealPathUtils :
public class RealPathUtils {
@SuppressLint("NewApi")
public static String getRealPathFromURI_API19(Context context, Uri uri){
String filePath = "";
String wholeID = DocumentsContract.getDocumentId(uri);
// Split at colon, use second item in the array
String id = wholeID.split(":")[1];
String[] column = { MediaStore.Video.Media.DATA };
// where id is equal to
String sel = MediaStore.Video.Media._ID + "=?";
Cursor cursor = context.getContentResolver().query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
column, sel, new String[]{ id.replaceAll("(?i)^.*/", "") }, null);
int columnIndex = cursor.getColumnIndex(column[0]);
if (cursor.moveToFirst()) {
filePath = cursor.getString(columnIndex);
}
cursor.close();
return filePath;
}
@SuppressLint("NewApi")
public static String getRealPathFromURI_API11to18(Context context, Uri contentUri) {
String[] proj = { MediaStore.Video.Media.DATA };
String result = null;
CursorLoader cursorLoader = new CursorLoader(
context,
contentUri, proj, null, null, null);
Cursor cursor = cursorLoader.loadInBackground();
if(cursor != null){
int column_index =
cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA);
cursor.moveToFirst();
result = cursor.getString(column_index);
}
return result;
}
}
我怀疑在生产环境中使用RealPathUtils
而不知道它是否会在未经测试的情况下失败。
SDK > 15
设备上文件的绝对路径?