【问题标题】:Android 11: How to access app specific files from BrowserAndroid 11:如何从浏览器访问应用程序特定文件
【发布时间】:2022-01-05 09:35:48
【问题描述】:

我的应用正在创建一些用户可以使用网络浏览器打开的 html 文件(报告)。

但从 Android 11 数据存储隐私来看,浏览器无法从应用私有存储中读取 html 文件。

存储访问框架需要人工干预。

是否有任何解决方法,以便网络浏览器可以访问我的应用特定存储中生成的 html 文件?

【问题讨论】:

  • 使用 FileProvider 提供文件的 ACTION_VIEW 就可以了。
  • 试过了,但它仅适用于公共目录,不适用于特定于应用程序的私有目录(适用于 Android 11)。虽然 html 报告仅在应用程序包内生成。
  • 下面是代码: Intent intent = new Intent(Intent.ACTION_VIEW, FileProvider.getUriForFile(mActivity, mActivity.getPackageName()+".provider",file));意图.addFlags(意图.FLAG_GRANT_READ_URI_PERMISSION);开始活动(意图);
  • Intent 的具体情况如何?我可以看到用户可能没有支持 content 方案的 Web 浏览器。
  • 意图用于浏览器,它将打开文件 uri。虽然 Android 11 对外部存储的限制,但文件 uri 正在返回导致崩溃的空值。

标签: android local-storage scoped-storage


【解决方案1】:

我现在可以通过使用存储访问框架(范围存储)而不是使用应用程序私有存储来访问它。

参考:https://developer.android.com/training/data-storage/shared/documents-files

使用 Intent: ACTION_OPEN_DOCUMENT_TREE 获取范围存储权限。然后将文件写入存储:

try {
            ParcelFileDescriptor pfd = getContentResolver().openFileDescriptor(uri, "w");
            FileOutputStream fileOutputStream = new FileOutputStream(pfd.getFileDescriptor());
            fileOutputStream.write(("Overwritten at " + System.currentTimeMillis() +
                    "\n").getBytes());
            // Let the document provider know you're done by closing the stream.
            fileOutputStream.close();
            pfd.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

可以使用浏览器意图访问这些文件。

【讨论】:

  • 您的意思是 ACTIIN_VIEW?但是 ` 标签中的图片不会被加载。还是他们?
猜你喜欢
  • 2013-02-05
  • 1970-01-01
  • 1970-01-01
  • 2010-09-26
  • 1970-01-01
  • 1970-01-01
  • 2013-07-20
  • 2015-08-18
  • 2018-11-12
相关资源
最近更新 更多