Android无法打开相册查看视频

最近公司做了一个项目需要查看手机视频,在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);
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值