您现在的位置是:首页 > 文章详情

Android无法打开相册查看视频

日期:2018-05-21点击:624

最近公司做了一个项目需要查看手机视频,在android 8的模拟器上正常。在android 5.1的模拟器下却报了一个错误:

 Caused by: java.lang.SecurityException: Permission Denial: starting Intent { act=android.intent.action.PICK dat=content://media/external/video/media 
cmp=com.android.music/.VideoBrowserActivity } from ProcessRecord{1b308dad 5422:com.videoclipper.demo/u0a58} (pid=5422, uid=10058) not exported from uid 10036

讲道理不应该有权限问题的。因为target为21,而且api22没有运行时权限,android 8也可以正常运行。这个现象真的很莫名其妙啊。经过搜索找到了一种解决方法:原文


// contentId will have the video content id as given by Content Resolver
// In this nparticular application, contentId is retrieved from ListActivity with custom adapter

Uri contentUri = ContentUris.withAppendedId(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, contentId);

try {
    Intent intent = new Intent(Intent.ACTION_VIEW, contentUri);
    startActivity(intent);
} 
catch (ActivityNotFoundException e) {//SecurityException 也可以
    Toast.makeText(this, "Not Supported", Toast.LENGTH_SHORT).show();
}

要调用 Gallery browser 使用以下代码:

someMethod() {
   Intent intent = new Intent(Intent.ACTION_PICK, null);
   intent.setType("video/*");
   startActivityForResult(intent, 1);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if ((requestCode == 1) && (resultCode == RESULT_OK) && (data != null)) {

        Log.i("---------------------", data.getData().getEncodedPath());
        mIntentFromGallery = data;

        Intent intent = new Intent(Intent.ACTION_MAIN, null);
        intent.setType("video/*");
        intent.setData(data.getData());
        try
        {
            startActivity(intent);
        }
        catch(Exception e)
        {
        }


    } else {
        setResult(RESULT_CANCELED);
        finish();
    }
}

综合起来的解决方案就是:

           Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Video.Media.EXTERNAL_CONTENT_URI);
            intent.setDataAndType(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, "video/*");
            startActivityForResult(intent, REQUEST_CODEE_VIDEO);
原文链接:https://yq.aliyun.com/articles/638013
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章