得到的URI的Andr​​oid奇巧的路径将抛出非法参数异常 [英] Get the path from Uri in Android Kitkat throws Illegal Argument exception

查看:312
本文介绍了得到的URI的Andr​​oid奇巧的路径将抛出非法参数异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从URI路径。它的工作原理如下奇巧设备。我用Google搜索,发现我需要得到的文档ID,然后用库项目查询。和它的作品。但我面临的问题与视频。当我选择视频部分视频,它的工作原理。但是,当我从库中选择它,我的应用程序崩溃,给非法参数不与异常的文档类型。我知道为什么它的发生。这是因为我正在寻找在多媒体部分的文档类型的视频。但如何prevent这一点。所以,我可以选择从两个画廊及放视频;视频部分。

I'm trying to get the path from uri. it works in devices below kitkat. I googled and found that I need to get the document Id and then query it with the gallery items. And it works. But I'm facing problem with videos. When i select the video from Videos section, it works. But When I select it from Gallery, my app crashes, giving Illegal Argument exception- Not a document type. I know why it's happening. It is because I'm looking for a document type of video in Gallery section. But how to prevent this. So that I can select videos from both Gallery & Videos Section.

下面是我的code:

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的文件。但对于下面奇巧设备,MediaStore.Images.Media.DATA将视频URI,从画廊也不会造成任何崩溃。在此先感谢..

This is happening because I'm trying to search a file of type MediaStore.Video.Media.DATA in Gallery. But for devices below kitkat, MediaStore.Images.Media.DATA gets the video uri, from the gallery too without causing any crash. Thanks in Advance..

推荐答案

您可以在Android中使用用于获取所选文件的URI,因为在这两个奇巧(API 19),差异绝对路径及以下奇巧实现这一目标使用以下code,

You can use for getting absolute path from the Uri of selected file because of differences in both KitKat(API 19) and below Kitkat in android to achieve this using following code,

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());
}

这篇关于得到的URI的Andr​​oid奇巧的路径将抛出非法参数异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆