我的应用从其他应用接收文件。所以,我收到了URI但是......如何访问文件名和数据?
现在,我正在做这样的事情:
if ("file".equals(dataUri.getScheme())){
File file = new File(dataUri.getPath));
// I do needed operations with the file here
}
else if ("content".equals(dataUri.getScheme())){
Cursor cursor = getContentResolver().query(dataUri, new String[]{MediaStore.MediaColumns.DISPLAY_NAME}, null, null, null);
if (cursor.moveToFirst() && (nameIndex = cursor.getColumnIndex(cursor.getColumnNames()[0])) >= 0){
String fileName = cursor.getString(nameIndex);
InputStream inputStream = getContentResolver().openInputStream(dataUri);
// I do needed operations with the file here
}
}
这足以处理每一个案件吗?
答案 0 :(得分:2)
您无需另行处理file
和content
,可以通过调用getContentResolver().openInputStream(uri)
来处理这两种情况。
从该页面上的其他文档中,您可以看到ContentResolver
引用了三个方案,并且该方法可以处理所有这些方案。
如果您深入挖掘,可以在此处获取有关打开文档的更多详细信息: http://developer.android.com/guide/topics/providers/document-provider.html#client
虽然它通常是在讨论API 19中提供的更新选项,但以下部分也适用于旧版本。
检查文档元数据
public void dumpImageMetaData(Uri uri) {
// The query, since it only applies to a single document, will only return
// one row. There's no need to filter, sort, or select fields, since we want
// all fields for one document.
Cursor cursor = getActivity().getContentResolver()
.query(uri, null, null, null, null, null);
try {
// moveToFirst() returns false if the cursor has 0 rows. Very handy for
// "if there's anything to look at, look at it" conditionals.
if (cursor != null && cursor.moveToFirst()) {
// Note it's called "Display Name". This is
// provider-specific, and might not necessarily be the file name.
String displayName = cursor.getString(
cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
Log.i(TAG, "Display Name: " + displayName);
int sizeIndex = cursor.getColumnIndex(OpenableColumns.SIZE);
// If the size is unknown, the value stored is null. But since an
// int can't be null in Java, the behavior is implementation-specific,
// which is just a fancy term for "unpredictable". So as
// a rule, check if it's null before assigning to an int. This will
// happen often: The storage API allows for remote files, whose
// size might not be locally known.
String size = null;
if (!cursor.isNull(sizeIndex)) {
// Technically the column stores an int, but cursor.getString()
// will do the conversion automatically.
size = cursor.getString(sizeIndex);
} else {
size = "Unknown";
}
Log.i(TAG, "Size: " + size);
}
} finally {
cursor.close();
}
}
打开文档
位图
private Bitmap getBitmapFromUri(Uri uri) throws IOException {
ParcelFileDescriptor parcelFileDescriptor = getContentResolver().openFileDescriptor(uri, "r");
FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();
Bitmap image = BitmapFactory.decodeFileDescriptor(fileDescriptor);
parcelFileDescriptor.close();
return image;
}
输入流
private String readTextFromUri(Uri uri) throws IOException {
InputStream inputStream = getContentResolver().openInputStream(uri);
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder stringBuilder = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
stringBuilder.append(line);
}
inputStream.close();
return stringBuilder.toString();
}