java - 如何访问已使用 Android 11 的附近连接 API 传输的文件,因为文件存储在附近的范围存储中?

标签 java android google-nearby google-nearby-connections

我正在尝试使用 google 附近连接 API 传输文件。很大程度上,我可以让传输的所有组件正常工作,以便传输所有文件数据,但问题是文件数据随后存储在附近的范围存储中,因此我无法从我的应用程序访问它以进行处理将该数据转换为适当的文件类型,然后根据需要保存并重命名。

我当前用来尝试处理有效负载的方法是

private void processFilePayload(long payloadId) {

// BYTES and FILE could be received in any order, so we call when either the BYTES or the FILE
// payload is completely received. The file payload is considered complete only when both have
// been received.
    Payload filePayload = completedFilePayloads.get(payloadId);
    String filename = filePayloadFilenames.get(payloadId);

    if (filePayload != null && filename != null) {
        completedFilePayloads.remove(payloadId);
        filePayloadFilenames.remove(payloadId);

// Get the received file (which will be in the Downloads folder)
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {

              File fromPayload = filePayload.asFile().asJavaFile();
              Uri uri = Uri.fromFile(fromPayload);

              try {
              // Copy the file to a new location.
                   InputStream in = getApplicationContext().getContentResolver().openInputStream(uri);
                   copyStream(in, new FileOutputStream(new File(getApplicationContext().getCacheDir(), filename)));
                  } catch (IOException e) {
                      // Log the error.
                      Log.e("copy file", e.toString());
                  } finally {
                      // Delete the original file.
                      getApplicationContext().getContentResolver().delete(uri, null, null);
                  }

            } else  {
            File payloadFile = filePayload.asFile().asJavaFile();

            // Rename the file.
            payloadFile.renameTo(new File(payloadFile.getParentFile(), filename));
            }
        }
    }

});

由于 Android 11 的作用域存储能够访问该文件,因此需要使用文件 Uri 来创建带有内容解析器的输入流来访问该文件。

根据附近的文档,应该有一个方法 Payload.File.asUri,这样我就可以使用行 Uri payloadUri = filePayload.asFile().asUri(); 但这是尽管使用了最新版本的 Nearby,但实际上在 API 中并不可用。

此外,根据 google 附近文档,应弃用 Payload.File.AsJavaFile() 的使用

我已经看到了类似问题的一些其他答案,其中建议使用 Media.Store 但这是不可能的,因为该文件还没有任何扩展名,因此不会显示为任何特定的文件类型。

注意:我已在 list 中和运行时请求读/写外部存储权限。

最佳答案

===更新===

Payload.asFile.asUri() 可在 com.google.android.gms:play-services-nearby:18.0.0 中使用

============

对此感到抱歉。我们将很快发布更新,并正确暴露#asUri。

同时,如果您的目标是 API 29,则可以使用 requestLegacyExternalStorage=true 作为解决方法。 (查看更多:https://developer.android.com/about/versions/11/privacy/storage)

关于java - 如何访问已使用 Android 11 的附近连接 API 传输的文件,因为文件存储在附近的范围存储中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66670258/

相关文章:

java - 此硬件不支持帧缓冲区。 Java Processing library 2.2.1 和 Unfolding Map 错误

java - Couchbase View 结果与 JAVA 和 REST 不同

android - 在 MVP 项目中正确使用 onActivityResult

Android谷歌地图自定义布局

android - 附近连接最大连接设备 - 说明

java - 依赖于不同 Xalan 实现的两个程序如何在同一个 VM/系统类加载器中共存?

java - 通过 API 进行 Magento 客户身份验证

android - 水平EditText的光标

android - 在哪里向 Google 报告 API 错误? (谷歌附近消息 API)

android - 订阅附近的服务 - MessageListener 从未调用过