Analysis and solution of Android beam file transfer failure

Recently, some bugs in the Android 7.0 native platform have been modified, among which there are many problems about Android beam transfer files. So I took time to sum up the pits I had stepped on.

1. When the transmitted file name contains Chinese, the transmission fails

It may be that Google does not consider localization differences, which leads to a direct prompt of transmission failure when transmitting files with Chinese file names.

In fact, I forgot to say that the above problem only occurs when I enter Android beam sharing from the file manager. Because when sharing pictures from other ways, such as from the gallery, it is through the content URI( content://com.xx.xxx/xxx )It is shared in form, so it will not directly include the real path of the file, so there is no Chinese problem.

When entering Android beam sharing from the file manager, it is through the file URI( file://storage/xxx/aa.jpg )For formal sharing, the process in else if will be followed when obtaining the mimeType of the file.

As can be seen from the above code, when obtaining the file suffix, the file name is regularly matched at the end. The file name containing Chinese must fail to match, resulting in an empty suffix, so a null mimeType is returned. Finally, the file transfer failed.

Now that the problem is found, the modification method is relatively simple. Just remove the regular judgment layer above; Because this is a method in FWK, in order not to affect the calls of other modules. You can also copy the entire method into the NFC module and remove the regular judgment.

2. When the transmitted file name contains special characters, the transmission fails

For example, if the file name contains "#", the transmission will fail. In fact, this problem contains two problems: (1) when sharing from the file manager through Android beam, the direct transfer fails. In fact, this problem is similar to the problem in 1 above:

If the problem in 1 is fixed, that is, the following regular judgment is removed, and finally a null mimeType is returned, resulting in transmission failure. The repair method is also relatively simple. Remove the "#" processing code above and the following regular judgment.

(2) . after fixing the problem in (1), it is found that the transmission fails after several tests, whether through the file manager or the gallery. However, different from the above: the progress of the notification bar shows that the transmission is completed, which is 100%, but after a while, the receiving end prompts that the transmission fails, and the sending end shows that the transmission is successful.

Through checking, the file is indeed transmitted to the receiving end, but it is not in the beam directory, but in the Bluetooth directory. As we all know, Android beam transfers files through Bluetooth. However, the receiver and sender do not need Bluetooth pairing, but establish their connection through NFC.

Analyzing the code flow, it is found that the files are first transferred to the Bluetooth folder, and then the files transferred to the Bluetooth directory are moved to the beam directory through renameto.

Therefore, it is suspected that an exception occurred during renameto, which is also confirmed in the log. Moreover, the "#" in the file name is removed from the file path printed by srcfile, so the file cannot be found in the Bluetooth directory. Of course, renameto will fail.

By tracking the places where URI parameters are passed step by step, it is found that the URI sent by Bluetooth module through broadcast is a pure file path, / storage / emulated / 0 / Bluetooth / weeww#. Jpg. Therefore, getscheme returns null. When calling uri.getpath, the "#" in the file path is automatically removed.

The solution is also relatively simple. Instead of calling uri.getpath, we can directly pass the uristring into the parameters of constructing the file.

summary

The above is the Android beam file transfer failure analysis and solutions introduced by Xiaobian. I hope it will help you. If you have any questions, please leave me a message, and Xiaobian will reply to you in time. Thank you very much for your support for the programming tips website!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>