Android:从Gallery uri获取视频

时间:2016-12-18 13:28:24

标签: android video android-intent android-gallery

我正在尝试从Android图库中获取视频并获取完整路径,因此我可以在应用中上传视频...

我正在显示这样的画廊:

Intent intent = new Intent();
intent.setType("video/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
activity.startActivityForResult(Intent.createChooser(intent, context.getResources().getString(R.string.popup_menu_share_video_title)), Const.PICK_VIDEO_REQUEST);

然后我提取uri:

Uri selectedUri = data.getData();
String selectedPath = getRealPathFromURI(this, selectedUri);

并尝试获取路径:

public String getRealPathFromURI(Context context, Uri contentUri) {
    Cursor cursor = null;
    try {
        String[] proj = { MediaStore.Video.Media.DATA };
        cursor = context.getContentResolver().query(contentUri,  proj, null, null, null);
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
}

但我似乎无法走上正轨...

如果我这样做,我可以看到所有视频:

public static void dumpVideos(Context context ) {
    Uri uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
    String[] projection = { MediaStore.Video.Media.DATA };
    Cursor c = context.getContentResolver().query(uri, projection, null, null, null);
    int vidsCount = 0;
    if (c != null) {
        vidsCount = c.getCount();
        while (c.moveToNext()) {
            Log.d("VIDEO", c.getString(0));
        }
        c.close();
    }
    Log.d("VIDEO", "Total count of videos: " + vidsCount);
}

所以我认为必须有一些真正简单的步骤,我很想念......

2 个答案:

答案 0 :(得分:1)

  

我试图从Android图库中获取视频

首先,没有" Android Gallery"。有许多应用程序可以考虑"画廊"应用。有成千上万的设备型号,它们将随附各种各样的设备型号#34;应用程序,以及用户安装的应用程序。

其次,ACTION_GET_CONTENT并不是神奇地限制在"画廊"应用。任何应用都可以选择支持ACTION_GET_CONTENT的{​​{1}}。

  

并获得完整路径

这是不可能的。您无法知道用户选择哪个应用来处理您的video/*请求。你无法知道你会得到什么样的ACTION_GET_CONTENT。您无法知道Uri标识的内容将在何处存储,因为它不必是您可以访问的文件系统上的文件。

  

所以我可以在应用中上传视频

使用Uri上的openInputStream()获取ContentResolver标识的内容InputStream。然后,要么:

  • 直接将该流与首选HTTP客户端API一起使用以上传内容,或

  • 使用该流制作内容的本地文件副本,然后将该文件与首选HTTP客户端一起使用

  

并尝试获取路径:

充其量,该代码将在非常有限的情况下工作:

  • 用户选择处理您的Uri请求的应用会​​从ACTION_GET_CONTENT

  • 返回content Uri
  • 该内容恰好位于外部存储设备上,而非可移动存储设备

由于这不会涵盖所有场景,因此请删除该代码。

  

如果我这样做,我可以看到所有视频

不,您可以为某些视频获取路径

  • MediaStore无法通过ACTION_GET_CONTENT访问每个视频

  • 并非MediaStore中的每个视频都在外部存储上,您可以通过文件系统API访问它

答案 1 :(得分:1)

您可以使用ACTION_PICK而不是ACTION_GET_CONTENT来解决此问题。我相信除非你需要从Dropbox等添加图片/视频的功能,否则它是不值得的。如果您只需要从图库中获取内容,请使用以下代码:

 if (Build.VERSION.SDK_INT < 19) {
           final Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
           photoPickerIntent.setType("image/* video/*");
           startActivityForResult(photoPickerIntent, PICK_PHOTO_ACTIVITY_REQUEST_CODE);
 } else {
           final Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
           photoPickerIntent.setType("*/*");
           photoPickerIntent.putExtra(Intent.EXTRA_MIME_TYPES, new String[]{"image/*", "video/*"});
           startActivityForResult(photoPickerIntent, PICK_PHOTO_ACTIVITY_REQUEST_CODE);
 }

我刚刚尝试使用ACTION_PICK发布的代码,它运行正常!

相关问题