如何从uri获取视频文件路径

时间:2015-10-22 07:50:23

标签: android cursor uri mediastore

我正在尝试从我的视频uri获取文件路径。

我已经完成了以下链接但仍面临此问题:

get-filename-and-path-from-uri-from-mediastore

get-file-path-from-uri-from-video-chooser

以下是我的代码:

public function ugetstd($id) //update
{
$rules = array(
        'firstname'            => 'required|max:80|regex:/^[\p{L}\p{N} -   ]+$/u',
        'middlename'           => 'required|max:50|regex:/^[\p{L}\p{N} -]+$/u',     //more here

    );
    $messages = array(
        'firstname.required'        => 'First name is required',
        'firstname.max'             => 'First name can only contain 80 characters' //more here
    );

    $validator = Validator::make(Input::all(), $rules, $messages);
    if ($validator->fails()) 
    {
        return Redirect::to('view_students/' . $id)
            ->withErrors($validator)
            ->withInput(Input::except('password'));
    }
    else
    {
        DB::table('dbo_students')->where('StudentID', $id )
            ->update
                (
                    array
                    (
                        'FirstName'                     => Input::get('firstname'),
                        'MiddleName'                    => Input::get('middlename'),
                        'LastName'                      => Input::get('lastname'),
                        'CurrentStatusID'               => Input::get('scs'),
                        'Sex'                           => Input::get('sex'),
                        'ReligionID'                    => Input::get('rel'),
                        'EthnicityID'                   => Input::get('eth'),
                        'StreetAddress'                 => $enAdd,
                        'CityID'                        => Input::get('city'),
                        'YearLevelID'                   => Input::get('yearlevel'),
                        'Telephone'                     => Input::get('telephone'),
                        'Birthdate'                     => Input::get('date'),
                        'Birthplace'                    => Input::get('birthplace'),
                        'SchoolLastAttended'            => Input::get('schoollastattended'),
                        'LastGradeCompleted'            => Input::get('lastgradecompleted'),
                        'CurrentModuleLeft'             => Input::get('currentmoduleleft'),
                        'CurrentModuleCriticalLevel'    => Input::get('modulecriticallevel'),
                        'StudentDescription'            => Input::get('description')
                    )
                );

        return Redirect::to('view_students/' . $id);
    }

内容://com.android.providers.media.documents/document/video%3A1142

但总是将文件路径设为null。

以下是我的日志:

        Uri imageUri = data.getData();
        String[] proj = { MediaStore.Audio.Media.DATA };
        Cursor cursor = PreferenceHelper.getContext().getContentResolver().query(imageUri, proj, null, null, null);
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
        cursor.moveToFirst();
        String filePath = cursor.getString(column_index);
        sendVideo(activity, filePath, windowId, isChatroom);

1 个答案:

答案 0 :(得分:4)

您好我也遇到了同样的问题,但这里是方法

 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)) {

        // ExternalStorageProvider
        else 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
        }

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

我从this url获得

- >其他方法可以从这个 FILE UTILS 文件中获取编码:) - >它让我从uri希望它可以帮助你完整的视频路径:)