前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Android无法打开相册查看视频

Android无法打开相册查看视频

作者头像
JarvanMo
发布2018-09-06 15:01:25
1.7K0
发布2018-09-06 15:01:25
举报
文章被收录于专栏:JarvanMo的IT专栏JarvanMo的IT专栏

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

代码语言:javascript
复制
 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也可以正常运行。这个现象真的很莫名其妙啊。经过搜索找到了一种解决方法:原文

代码语言:javascript
复制
// 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 使用以下代码:

代码语言:javascript
复制
someMethod() {
   Intent intent = new Intent(Intent.ACTION_PICK, null);
   intent.setType("video/*");
   startActivityForResult(intent, 1);
}
代码语言:javascript
复制
@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();
    }
}

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

代码语言:javascript
复制
           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);
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018.05.22 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档