Android Beam json文件
我有一个简单的活动,启动一个文件选择器,然后通过Android beam发送文件,如下所示:Android Beam json文件,android,json,nfc,android-json,android-beam,Android,Json,Nfc,Android Json,Android Beam,我有一个简单的活动,启动一个文件选择器,然后通过Android beam发送文件,如下所示: @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode==0 && resultCode==RESULT_OK) { adapter.setBea
@Override
protected void onActivityResult(int requestCode, int resultCode,
Intent data) {
if (requestCode==0 && resultCode==RESULT_OK) {
adapter.setBeamPushUris(new Uri[] {data.getData()}, this);
Button btn=new Button(this);
btn.setText("Done");
btn.setOnClickListener(this);
setContentView(btn);
}
}
该活动能够正确地发射光束
图像和.txt文件。然而,当我读取Beam.json文件时,我得到了“Beam未完成”
我认为这是因为,接收器上没有查看json文件的应用程序,所以我创建了另一个版本来查看收到的txt文件。舱单上有
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/*" />
</intent-filter>
当我将json文件的扩展名更改为.txt时,文件被正确发送,接收应用程序被启动。但是,当我将扩展名改回.json,将接收器的mimetype改回“application/json”时,仍然会收到“Beam未完成”消息
你知道为什么吗
谢谢
你知道为什么吗
使用BeamUri
s数组中第一个文件的文件扩展名在内部映射中查找相应的MIME类型,然后将其与通过蓝牙对象推送配置文件(OPP)启动文件传输的一起发送
如果未找到文件扩展名或匹配的MIME类型,则的MIME类型将设置为null
,并且根本不会启动蓝牙OPP文件传输
解决方法
发送扩展名未在中列出的文件时,请使用两个元素的梁阵列:
uris[0]
:扩展名为.txt
的虚拟文本文件(稍后删除)uris[1]
:要传输的文件(具有无法识别的扩展名)
在您的具体情况下:
adapter.setBeamPushUris(
new Uri[] { dummyTxtFileUri, data.getData() },
this);
Android Beam将向Bluetooth发送MIME类型为text/plain
的意向,以及两个文件的s,Bluetooth OPP文件传输将正常进行。请注意,当一次传送多个文件时,接收设备将文件存储在beam/
中的子目录中,通常命名为beam YYYY-MM-DD/
背景
我比较了发送设备上的日志,发送一个扩展名为.json
的文件和一个扩展名为.txt
的文件副本。第一个显著的区别是:
日志:beamingtest.json
03-02 13:19:34.665: D/BluetoothOppHandover(32332): Handing off outging transfer to BT
03-02 15:32:19.437: D/BluetoothOppHandover(3268): Handing off outging transfer to BT
03-02 15:32:19.445: D/BluetoothOppUtility(3309): putSendFileInfo: uri=file:///storage/emulated/0/Download/test.txt@2cb672fa sendFileInfo=com.android.bluetooth.opp.BluetoothOppSendFileInfo@2cb672fa
日志:beamingtest.txt
03-02 13:19:34.665: D/BluetoothOppHandover(32332): Handing off outging transfer to BT
03-02 15:32:19.437: D/BluetoothOppHandover(3268): Handing off outging transfer to BT
03-02 15:32:19.445: D/BluetoothOppUtility(3309): putSendFileInfo: uri=file:///storage/emulated/0/Download/test.txt@2cb672fa sendFileInfo=com.android.bluetooth.opp.BluetoothOppSendFileInfo@2cb672fa
正在搜索AOSP以“向BT移交外发转账”:
在继续之前,请注意,的MIME类型仅在Intent
中发送数组中的第一个Uri
。以下MimeTypeUtil.getMimeTypeForUri()
:
因此,如果它没有识别扩展,它将返回null
作为MIME类型。以下是MimeTypeMap.getSingleton().getMimeTypeFromExtension()
因此,如果没有找到匹配项,MIME类型最终返回为null
。再深入一点就可以看出这一点的重要性:
因为在保存文件信息和开始传输之前,MIME类型上有一个空检查,所以蓝牙OPP文件传输永远不会启动。请注意,当存在
null
时,其他两个条件块返回
,因此在Log.d()
调用之后,这一块似乎缺少返回
,这可能是一个错误。。。。JSON文件的URI是什么样子的?@unrulygno-URI是:file:///storage/sdcard0/Android/data/com.test.nfctest/files/test.jsonI 不要认为有任何应用程序明确表示它们接受JSON文件。您可能应该将它们作为文本发送。您可能可以保留.json,只要确保mimetype是text/*
@kentarosu-当我使用文件资源管理器查看.json文件的属性时,我没有看到任何mimetype。我如何确保json文件具有文本/*mimetype我几乎可以肯定所有.json
文件都只是.txt
文件,最后具有不同的扩展名。就这样。
public String getMimeTypeFromExtension(String extension) {
return MimeUtils.guessMimeTypeFromExtension(extension);
}
public final class MimeUtils {
private static final Map<String, String> mimeTypeToExtensionMap = new HashMap<String, String>();
private static final Map<String, String> extensionToMimeTypeMap = new HashMap<String, String>();
// ...
public static String guessMimeTypeFromExtension(String extension) {
if (extension == null || extension.isEmpty()) {
return null;
}
return extensionToMimeTypeMap.get(extension);
}
/**
* Returns the value of the mapping with the specified key.
*
* @param key
* the key.
* @return the value of the mapping with the specified key, or {@code null}
* if no mapping for the specified key is found.
*/
public V get(Object key) {
@Override
public void onReceive(Context context, Intent intent) {
// ...
if (action.equals(Constants.ACTION_HANDOVER_SEND)) {
String type = intent.getType();
Uri stream = (Uri)intent.getParcelableExtra(Intent.EXTRA_STREAM);
if (stream != null && type != null) {
// Save type/stream, will be used when adding transfer
// session to DB.
BluetoothOppManager.getInstance(context).saveSendingFileInfo(type,
stream.toString(), true);
} else {
if (D) Log.d(TAG, "No mimeType or stream attached to handover request");
}
// ...
// we already know where to send to
BluetoothOppManager.getInstance(context).startTransfer(device);