【问题标题】:select video from gallery not working in Oreo从库中选择视频不适用于奥利奥
【发布时间】:2018-06-09 15:32:20
【问题描述】:

我已使用以下代码从画廊中挑选视频,该视频在棉花糖之前工作正常,但似乎不适用于奥利奥

    try {
        Intent i = new Intent(Intent.ACTION_PICK,
        android.provider.MediaStore.Video.Media.EXTERNAL_CONTENT_URI);
        i.setType("image/* video/*");
        startActivityForResult(i, CAMERA_CAPTURE_VIDEO_REQUEST_CODE);
    } catch (Exception e) {
        e.printStackTrace();
    }

我也试过了,但是没用

     try {
        Intent i = new Intent(Intent.ACTION_GET_CONTENT,
        android.provider.MediaStore.Video.Media.EXTERNAL_CONTENT_URI);
        i.setType("*/*");
        startActivityForResult(i, CAMERA_CAPTURE_VIDEO_REQUEST_CODE);
    } catch (Exception e) {
        e.printStackTrace();
    }

【问题讨论】:

  • 你能分享错误日志吗?
  • 它正在打开画廊,但只显示要选择的图像
  • 请试试这个方法,stackoverflow.com/a/23994787/4394827
  • @HemanthSTobi 你认为我做了什么与你的回答不同的事情吗?
  • used following code to pick video from gallery which was working fine until marshmallow but it is not working for oreo it seems .什么不起作用?如果 onActivityResult() 被触发,它会起作用,data.getData() 是所选视频的 uri。请告诉什么不起作用。

标签: android android-8.1-oreo


【解决方案1】:

试试这个..

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

【讨论】:

  • 试过这段代码,在这种情况下 selectedImagePath 为空。
  • 意图 { dat=content://com.android.providers.media.documents/document/video:24653 flg=0x1 }
  • tried this code and selectedImagePath is null in this case 您甚至不应该尝试从“路径”开始。直接使用内容方案!您认为需要“路径”的尝试是什么?
  • 我正在尝试从图库中选择一个文件,然后在我的应用程序中播放它,稍后用户可以将此视频上传到 aws。为此,我从意图数据 onActivityResult 获得的 URI 生成视频文件。
【解决方案2】:

这是一个权限问题请提供Manifest.permission.CAMERA,Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.READ_EXTERNAL_STORAGE

权限

在 Android 8.0(API 级别 26)之前,如果应用在运行时请求权限并且该权限被授予,系统也会错误地授予该应用属于同一权限组的其余权限,并且在清单中注册。

对于面向 Android 8.0 的应用,此行为已得到纠正。该应用程序仅被授予其明确请求的权限。但是,一旦用户向应用授予权限,该权限组中的所有后续权限请求都会自动授予。

例如,假设一个应用在其清单中同时列出了 READ_EXTERNAL_STORAGE 和 WRITE_EXTERNAL_STORAGE。应用程序请求 READ_EXTERNAL_STORAGE 并且用户授予它。如果应用针对 API 级别 25 或更低,系统也会同时授予 WRITE_EXTERNAL_STORAGE,因为它属于同一个 STORAGE 权限组,并且也在清单中注册。如果应用面向 Android 8.0(API 级别 26),系统此时仅授予 READ_EXTERNAL_STORAGE;但是,如果应用程序稍后请求 WRITE_EXTERNAL_STORAGE,系统会立即授予该权限而不提示用户 Source link

但您必须提供读写外部存储的权限。 对我来说它正在工作

【讨论】:

    猜你喜欢
    • 2018-07-24
    • 1970-01-01
    • 2018-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-28
    相关资源
    最近更新 更多