android - 如何从图库中选择视频并获取其真实路径?

标签 android android-gallery

我可以用这个代码打开画廊,

    Intent intent = new Intent(Intent.ACTION_PICK);
    intent.setType("video/*");

    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(Intent.createChooser(intent, "Select Video"), PICK_VIDEO_REQUEST );

它在三星 Galaxy S5 上运行良好。但在某些手机中,图像会与视频一起显示。

我需要对代码进行哪些更改才能打开图库以显示视频?

返回结果时如何获取所选视频的真实路径?

最佳答案


这是从图库中选择后获取视频路径的完整代码。

Intent intent = new Intent();
intent.setType("video/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,"Select Video"),REQUEST_TAKE_GALLERY_VIDEO);

public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            if (requestCode == REQUEST_TAKE_GALLERY_VIDEO) {
                Uri selectedImageUri = data.getData();

                // OI FILE Manager
                filemanagerstring = selectedImageUri.getPath();

                // MEDIA GALLERY
                selectedImagePath = getPath(selectedImageUri);
                if (selectedImagePath != null) {

                    Intent intent = new Intent(HomeActivity.this,
                            VideoplayAvtivity.class);
                    intent.putExtra("path", selectedImagePath);
                    startActivity(intent);
                }
            }
        }
    }

    // UPDATED!
    public String getPath(Uri uri) {
        String[] projection = { MediaStore.Video.Media.DATA };
        Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
        if (cursor != null) {
            // HERE YOU WILL GET A NULLPOINTER IF CURSOR IS NULL
            // THIS CAN BE, IF YOU USED OI FILE MANAGER FOR PICKING THE MEDIA
            int column_index = cursor
                    .getColumnIndexOrThrow(MediaStore.Video.Media.DATA);
            cursor.moveToFirst();
            return cursor.getString(column_index);
        } else
            return null;
    }

关于android - 如何从图库中选择视频并获取其真实路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31044591/

相关文章:

android - 我是否需要我的移动应用程序中的私钥才能通过 SSL 与服务器通信?

java - 同时运行动画和图像切换器

Android:倒计时后隐藏图库

android - 从图库 View 中选择多个图像

android - 如何从Android图库中选择多张图片

android - 类转换异常 : AbsListView$LayoutParams cannot be cast to Gallery$LayoutParams

java - 不要将工具栏连接到抽屉导航怎么办?

android - sdk 管理器没有加载,但 eclipse 已设置,其他一切都很好

android - 计算等于 firebase 中某个值的变量数

Android 闹钟无法使用 AlarmManager