前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >android N(API24) 版本兼容

android N(API24) 版本兼容

原创
作者头像
八神太一
修改2021-04-06 11:15:57
6410
修改2021-04-06 11:15:57
举报
文章被收录于专栏:androudandroud

1.适配Intent打开文件

  • 在AndroidManifest.xml添加
代码语言:javascript
复制
        <provider
            android:name="androidx.core.content.FileProvider"
            android:authorities="com.tmgp.sgame.conan.myapplication.fileprovider"
            android:grantUriPermissions="true"
            android:exported="false"
            >
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths" />
        </provider>
  • 在 res 下新建一个 xml 目录,然后创建一个名为 file_paths.xml 文件 ,该文件内容如下:
代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<paths>
    <external-path path="Android/data/com.tmgp.sgame.conan.myapplication/"        name="files_root" />
    <external-path path="." name="external_storage_root" />
</paths>
  • com.tmgp.sgame.conan.myapplication是包名,替换成自己的包名
  • packageinstaller安装apk方式
代码语言:javascript
复制
      File file = new File("/storage/emulated/0/1/1.apk");
      Intent intent = new Intent(Intent.ACTION_VIEW);
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            Uri contentUri = FileProvider.getUriForFile(MainActivity.this,"com.tmgp.sgame.conan.myapplication.fileprovider", file);
            intent.setDataAndType(contentUri, "application/vnd.android.package-archive");
        } else {
            intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        }
        if (getPackageManager().queryIntentActivities(intent, 0).size() > 0) {
            startActivity(intent);
        }
  • 打开Intent,文件选择方式
代码语言:javascript
复制
        File file = new File("/storage/emulated/0/1/1.zip");
        Intent intent = new Intent(Intent.ACTION_VIEW);
          if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            Uri contentUri = FileProvider.getUriForFile(MainActivity.this,"com.tmgp.sgame.conan.myapplication.fileprovider", file);
            //打开音频资源audio,视频资源video,图片资源image,文本资源text,所有能打开应用资源application
            intent.setDataAndType(contentUri, "application/*");
        } else {
            intent.setDataAndType(Uri.fromFile(file), "application/*");
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        }
        if (getPackageManager().queryIntentActivities(intent, 0).size() > 0) {
            startActivity(intent);
        }

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

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

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

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

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