gpt4 book ai didi

android - 从 Android Kitkat 中的 Uri 获取路径抛出非法参数异常

转载 作者:太空狗 更新时间:2023-10-29 15:41:23 26 4
gpt4 key购买 nike

我正在尝试从 uri 获取路径。它适用于 kitkat 以下的设备。我用谷歌搜索,发现我需要获取文档 ID,然后使用图库项目查询它。它有效。但是我在视频方面遇到了问题。当我从视频部分选择视频时,它起作用了。但是当我从图库中选择它时,我的应用程序崩溃,给出非法参数异常 - 不是文档类型。我知道为什么会这样。这是因为我正在画廊部分寻找文档类型的视频。但是如何防止这种情况。这样我就可以从图库和视频部分中选择视频。

这是我的代码:

public static String getRealPathFromURI(Context context, Uri contentUri,String type) {
if(isKitkatBelow()) {
String[] proj = { MediaStore.Images.Media.DATA };
Cursor cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}else{

String docId= DocumentsContract.getDocumentId(contentUri);
String id= docId.split(":")[1];
if(type== TYPE_IMAGE){
String[] proj = { MediaStore.Images.Media.DATA };
String whereClause=MediaStore.Images.Media._ID + "=?";
Cursor cursor=context.getContentResolver().query(getUri(type),proj,whereClause,new String[]{id},null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}else{
String[] proj = { MediaStore.Video.Media.DATA };
String whereClause=MediaStore.Video.Media._ID + "=?";
Cursor cursor=context.getContentResolver().query(getUri(type),proj,whereClause,new String[]{id},null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}



}
}

发生这种情况是因为我正在尝试在图库中搜索类型为 MediaStore.Video.Media.DATA 的文件。但是对于低于 kitkat 的设备,MediaStore.Images.Media.DATA 也从图库中获取视频 uri,而不会导致任何崩溃。提前致谢..

最佳答案

您可以使用从所选文件的 Uri 获取绝对路径,因为 KitKat(API 19)和 Android 中的 Kitkat 之间存在差异,可以使用以下代码实现此目的,

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];
}

// TODO handle non-primary volumes
}
// 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 getDataColumn(context, uri, null, null);
}
// File
else if ("file".equalsIgnoreCase(uri.getScheme())) {
return uri.getPath();
}

return null;
}

/**
* Get the value of the data column for this Uri. This is useful for
* MediaStore Uris, and other file-based ContentProviders.
*
* @param context
* The context.
* @param uri
* The Uri to query.
* @param selection
* (Optional) Filter used in the query.
* @param selectionArgs
* (Optional) Selection arguments used in the query.
* @return The value of the _data column, which is typically a file path.
*/
public 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()) {
final int column_index = cursor.getColumnIndexOrThrow(column);
return cursor.getString(column_index);
}
} finally {
if (cursor != null)
cursor.close();
}
return null;
}

/**
* @param uri
* The Uri to check.
* @return Whether the Uri authority is ExternalStorageProvider.
*/
public static boolean isExternalStorageDocument(Uri uri) {
return "com.android.externalstorage.documents".equals(uri
.getAuthority());
}

/**
* @param uri
* The Uri to check.
* @return Whether the Uri authority is DownloadsProvider.
*/
public static boolean isDownloadsDocument(Uri uri) {
return "com.android.providers.downloads.documents".equals(uri
.getAuthority());
}

/**
* @param uri
* The Uri to check.
* @return Whether the Uri authority is MediaProvider.
*/
public static boolean isMediaDocument(Uri uri) {
return "com.android.providers.media.documents".equals(uri
.getAuthority());
}

关于android - 从 Android Kitkat 中的 Uri 获取路径抛出非法参数异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27497520/

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