Java 如何解析从清单中的意图过滤器接收的内容数据?
有一个用于Java 如何解析从清单中的意图过滤器接收的内容数据?,java,android,xml,android-manifest,intentfilter,Java,Android,Xml,Android Manifest,Intentfilter,有一个用于kml文件的意向过滤器,可以工作,但我必须以内容方案的形式接收数据。 档案是从档案指挥官那里挑选的 资料 content://com.mobisystems.fileman.RemoteFiles/ZmlsZTovLy9zdG9yYWdlL2VtdWxhdGVkLzAvTG9jYXRpb24lMjBUcmFja2luZy8yMDE5LTA0LTA0JTIwMTclM0EyNyUzQTU5Lmtt/bA/0 现在的问题是如何读取此文件? 我如何处理这个问题? 任何帮助都将不胜感激 到目前
kml
文件的意向过滤器,可以工作,但我必须以内容方案的形式接收数据。
档案是从档案指挥官那里挑选的
资料
content://com.mobisystems.fileman.RemoteFiles/ZmlsZTovLy9zdG9yYWdlL2VtdWxhdGVkLzAvTG9jYXRpb24lMjBUcmFja2luZy8yMDE5LTA0LTA0JTIwMTclM0EyNyUzQTU5Lmtt/bA/0
现在的问题是如何读取此文件?
我如何处理这个问题?
任何帮助都将不胜感激
到目前为止,我已经试过了
var uri = Uri.parse(trackString)
FileInputStream(contentResolver.openFileDescriptor(uri, "r").fileDescriptor)
contentResolver.openInputStream(uri) // not working
manifest.xml中的
<!--Mime type set -->
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="content" />
<!-- Valid mime types -->
<data android:mimeType="application/vnd.google-earth.kml+xml" />
<data android:mimeType="application/vnd.google-earth.kmz" />
<data android:mimeType="application/gpx+xml" />
<!-- Invalid mime types used by some bad software -->
<data android:mimeType="application/kml" />
<data android:mimeType="application/kmz" />
<data android:mimeType="application/gpx" />
<data android:mimeType="application/kml+xml" />
<data android:mimeType="application/kmz+xml" />
<data android:mimeType="application/vnd.google-earth.kml" />
<data android:mimeType="application/vnd.google-earth.gpx" />
<data android:mimeType="application/vnd.google-earth.kmz+xml" />
<data android:mimeType="application/vnd.google-earth.gpx+xml" />
<data android:mimeType="text/kml" />
<data android:mimeType="text/kmz" />
<data android:mimeType="text/gpx" />
<data android:mimeType="text/kml+xml" />
<data android:mimeType="text/kmz+xml" />
<data android:mimeType="text/gpx+xml" />
<data android:mimeType="text/xml+kml" />
<data android:mimeType="text/xml+kmz" />
<data android:mimeType="text/xml+gpx" />
</intent-filter>
<!-- Mime type not set but valid extensions -->
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="content" />
<data android:host="*" />
<data android:pathPattern="/.*..*..*..*..*\\.gpx" />
<data android:pathPattern="/.*..*..*..*..*\\.kml" />
<data android:pathPattern="/.*..*..*..*..*\\.kmz" />
<data android:pathPattern="/.*..*..*..*\\.gpx" />
<data android:pathPattern="/.*..*..*..*\\.kml" />
<data android:pathPattern="/.*..*..*..*\\.kmz" />
<data android:pathPattern="/.*..*..*\\.gpx" />
<data android:pathPattern="/.*..*..*\\.kml" />
<data android:pathPattern="/.*..*..*\\.kmz" />
<data android:pathPattern="/.*..*\\.gpx" />
<data android:pathPattern="/.*..*\\.kml" />
<data android:pathPattern="/.*..*\\.kmz" />
<data android:pathPattern="/.*\\.gpx" />
<data android:pathPattern="/.*\\.kml" />
<data android:pathPattern="/.*\\.kmz" />
</intent-filter>
<!-- Invalid mime type but valid extensions -->
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="content" />
<data android:host="*" />
<data android:mimeType="*/*" />
<data android:pathPattern="/.*..*..*..*..*\\.gpx" />
<data android:pathPattern="/.*..*..*..*..*\\.kml" />
<data android:pathPattern="/.*..*..*..*..*\\.kmz" />
<data android:pathPattern="/.*..*..*..*\\.gpx" />
<data android:pathPattern="/.*..*..*..*\\.kml" />
<data android:pathPattern="/.*..*..*..*\\.kmz" />
<data android:pathPattern="/.*..*..*\\.gpx" />
<data android:pathPattern="/.*..*..*\\.kml" />
<data android:pathPattern="/.*..*..*\\.kmz" />
<data android:pathPattern="/.*..*\\.gpx" />
<data android:pathPattern="/.*..*\\.kml" />
<data android:pathPattern="/.*..*\\.kmz" />
<data android:pathPattern="/.*\\.gpx" />
<data android:pathPattern="/.*\\.kml" />
<data android:pathPattern="/.*\\.kmz" />
</intent-filter>
使用
ContentResolver
和:
查找内容的MIME类型(因为您的getType()
支持多种类型)
读取内容openInputStream()
通过在
上下文中调用getContentResolver()
可以获得ContentResolver
,您好,您能否在阅读时阐明您想要实现的目标?不清楚你想做什么。实际上我想在地图上显示kml。但它是一个内容:\\sceme无法解析,到目前为止,我尝试了var uri=uri.parse(trackString)FileInputStream(contentResolver.openFileDescriptor(uri,“r”).fileDescriptor)轨迹字符串=content://com.mobisystems.fileman.RemoteFiles/ZmlsZTovLy9zdG9yYWdlL2VtdWxhdGVkLzAvTG9jYXRpb24lMjBUcmFja2luZy8yMDE5LTA0LTA0JTIwMTclM0EyNyUzQTU5Lmtt/bA/0 不工作contentResolver.openInputStream(uri)->不工作上次我这么做时,错误是我从通过intent bundle发送的uri.path解析uri,所以得到了错误的东西,所以发送uri而不是uri.path对我有用,我会更新问题
Cursor returnCursor = context.getContentResolver().query(yourFileUri, null, null, null, null);
int nameIndex = returnCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
int sizeIndex = returnCursor.getColumnIndex(OpenableColumns.SIZE);
returnCursor.moveToFirst();
String name = (returnCursor.getString(nameIndex));
File file = new File(context.getCacheDir(), name);
try {
InputStream inputStream = context.getContentResolver().openInputStream(yourFileUri);
FileOutputStream outputStream = new FileOutputStream(file);
int read = 0;
int maxBufferSize = 1 * 1024 * 1024;
int bytesAvailable = inputStream.available();
int bufferSize = Math.min(bytesAvailable, maxBufferSize);
final byte[] buffers = new byte[bufferSize];
while ((read = inputStream.read(buffers)) != -1) {
outputStream.write(buffers, 0, read);
}
inputStream.close();
outputStream.close();
Log.d(TAG, "Path " + file.getPath());
Log.d(TAG, "Size " + file.length());
} catch (Exception e) {
Log.e("Exception", e.getMessage());
}
// To get the mime type of your file
public String getMimeType(Uri uri, Context ctx) {
String mimeType = null;
if (uri.getScheme().equals(ContentResolver.SCHEME_CONTENT)) {
ContentResolver cr = ctx.getContentResolver();
mimeType = cr.getType(uri);
} else {
String fileExtension = MimeTypeMap.getFileExtensionFromUrl(uri
.toString());
mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(
fileExtension.toLowerCase());
}
return mimeType;
}
Cursor returnCursor = context.getContentResolver().query(yourFileUri, null, null, null, null);
int nameIndex = returnCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
int sizeIndex = returnCursor.getColumnIndex(OpenableColumns.SIZE);
returnCursor.moveToFirst();
String name = (returnCursor.getString(nameIndex));
File file = new File(context.getCacheDir(), name);
try {
InputStream inputStream = context.getContentResolver().openInputStream(yourFileUri);
FileOutputStream outputStream = new FileOutputStream(file);
int read = 0;
int maxBufferSize = 1 * 1024 * 1024;
int bytesAvailable = inputStream.available();
int bufferSize = Math.min(bytesAvailable, maxBufferSize);
final byte[] buffers = new byte[bufferSize];
while ((read = inputStream.read(buffers)) != -1) {
outputStream.write(buffers, 0, read);
}
inputStream.close();
outputStream.close();
Log.d(TAG, "Path " + file.getPath());
Log.d(TAG, "Size " + file.length());
} catch (Exception e) {
Log.e("Exception", e.getMessage());
}
// To get the mime type of your file
public String getMimeType(Uri uri, Context ctx) {
String mimeType = null;
if (uri.getScheme().equals(ContentResolver.SCHEME_CONTENT)) {
ContentResolver cr = ctx.getContentResolver();
mimeType = cr.getType(uri);
} else {
String fileExtension = MimeTypeMap.getFileExtensionFromUrl(uri
.toString());
mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(
fileExtension.toLowerCase());
}
return mimeType;
}