简体   繁体   English

附近的连接:为什么“下载”文件夹中的有效负载文件为空?

[英]Nearby Connections: Why is the payload file from the Downloads folder null?

I'm following the File transfer example on the Nearby Connections Exchange page (the "more complex example" code snippet). 我正在“ 附近的连接交换”页面上关注文件传输示例(“更复杂的示例”代码段)。

I can send an image and receive it on another device in the Download/Nearby folder. 我可以发送图像并在“下载/附近”文件夹中的另一台设备上接收它。 The image is sent successfully since if I were to change the file name to give it an appropriate extension (eg .jpg), I can open the image in a photo gallery app. 图片发送成功,因为如果我要更改文件名以提供适当的扩展名(例如.jpg),则可以在图库应用程序中打开图片。

        private void processFilePayload(long payloadId) {
            Payload filePayload = completedFilePayloads.get(payloadId);
            String filename = filePayloadFilenames.get(payloadId);
            if (filePayload != null && filename != null) {
                completedFilePayloads.remove(payloadId);
                filePayloadFilenames.remove(payloadId);

                // Retrieve received file from Downloads folder
                Payload.File payloadFile2 = filePayload.asFile();
                File payloadJavaFile = payloadFile2.asJavaFile();

                if (payloadJavaFile == null) {
                    Log.d(TAG, "Payload java file is null in processFilePayload()");
                } else {
                    payloadJavaFile.renameTo(new File(payloadJavaFile.getParentFile(), filename));
                }
            }
        }

Why is the payloadJavaFile variable null? 为什么payloadJavaFile变量为空? From looking at Payload.class, I know that the result of asJavaFile() is a nullable File and that, from the asJavaFile() method description , calling asJavaFile() in processFilePayload() from within onPayloadReceived() (as is done in the example on the API page) may lead to the File not having received all of the payload's contents yet. 通过查看Payload.class,我知道asJavaFile()的结果是可为空的文件,并且从asJavaFile()方法的描述中 ,从onPayloadReceived()内的processFilePayload()中调用asJavaFile()。 API页面上的示例)可能导致文件尚未接收到所有有效内容。 However, I also call processFilePayload() from within onPayloadTransferUpdate() after verifying the success of the PayloadTransferUpdate, and so shouldn't the payload have received all of its contents by this stage (and not be null when calling asJavaFile() on the payload object)? 但是,在验证PayloadTransferUpdate成功之后,我也从onPayloadTransferUpdate()内部调用processFilePayload(),因此有效负载在此阶段不应该已经接收了所有内容(在有效负载上调用asJavaFile()时不能为null)宾语)?

My code is almost the same as the documentation for both sending and receiving the image and file name. 我的代码几乎与发送和接收图像和文件名的文档相同。

Payload.asJavaFile() will be null if the READ_EXTERNAL_STORAGE permission is not properly set up for your application. 如果未正确为您的应用程序设置READ_EXTERNAL_STORAGE权限,则Payload.asJavaFile()将为null。 You need READ_EXTERNAL_STORAGE in the AndroidManifest.xml and you need to also request READ_EXTERNAL_STORAGE permission at runtime since it is a dangerous permission. 您需要AndroidManifest.xml中的READ_EXTERNAL_STORAGE,并且在运行时还需要请求READ_EXTERNAL_STORAGE许可,因为这是危险的许可。 ( https://developer.android.com/training/permissions/requesting ) https://developer.android.com/training/permissions/requesting

To share files using Nearby Connections, your application will need all of the following: 要使用“附近的连接”共享文件,您的应用程序需要满足以下所有条件:

AndroidManifest.xml AndroidManifest.xml中

<!-- Required for Nearby Connections -->
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<!-- Optional: needed to share files -->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

MainActivity.java MainActivity.java

ActivityCompat.requestPermissions(thisActivity,
                new String[]{               
                  Manifest.permission.ACCESS_COARSE_LOCATION,
                  Manifest.permission.READ_EXTERNAL_STORAGE
                },
                PERMISSIONS_REQUEST_CODE);

We will improve the documentation to make this requirement for sharing files more obvious. 我们将改进文档,以使共享文件的要求更加明显。

I checked the source code. 我检查了源代码。 From what I can see, asJavaFile() is always set (and I'm not sure why it's marked as @Nullable). 从我所看到的,总是设置asJavaFile()(而且我不确定为什么将其标记为@Nullable)。

Unfortunately, for me to look into it any further, I'd need you to provide me with a sample app that reproduces the bug you're seeing. 不幸的是,为了让我进一步研究它,我需要您为我提供一个示例应用程序,该程序可以重现您看到的错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我的Android不读取“下载”文件夹中的该文件? - Why isn't my android reading this file in Downloads folder? Android:从“下载”文件夹中删除下载的文件快捷方式 - Android: Remove Downloaded file shortcut from "Downloads" folder 从 Android 的下载文件夹中获取 pdf 或 doc 文件 - Getting pdf or doc file from Downloads Folder in Android 在特定设备上使用 Google Nearby Connections 发送文件时出现问题 - Problem sending file using Google Nearby Connections on specific device 如何在下载文件夹中存储 pdf 文件 - How to store a pdf file in downloads folder 使用 Android 10 (Q) 中的 SAF 文件选择器将文件从下载文件复制到本地应用程序文件夹 - Using SAF File Picker in Android 10 (Q) to copy files from Downloads to local app folder 尝试使用Android Studio从内部存储的downloads文件夹中读取.csv文件 - Trying to read a .csv file from downloads folder of internal storage with Android Studio 如何访问已使用附近连接 API 和 android 11 传输的文件,因为文件存储在附近的范围存储中? - How to access file that has been transferred using Nearby Connections API with android 11, as file is stored in Nearby's scoped storage? Google Nearby Connections - 定时广告 - Google Nearby Connections - Timed Advertising 从文件的有效载荷中获取文档? - Get Document from payload of file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM