gpt4 book ai didi

android - 如何处理我的应用程序中收到的文件?

转载 作者:太空狗 更新时间:2023-10-29 13:17:48 25 4
gpt4 key购买 nike

我的应用程序从其他应用程序接收文件。所以,我收到了 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
}
}

这足以处理所有情况吗?

最佳答案

您不需要以不同方式处理 filecontent,两者都可以通过调用 getContentResolver().openInputStream(uri) 来处理.

http://developer.android.com/reference/android/content/ContentResolver.html#openInputStream(android.net.Uri)

从该页面上的其余文档中,您可以看到 ContentResolver 引用了三个方案,并且该方法可以处理所有方案。

  • SCHEME_ANDROID_RESOURCE
  • SCHEME_CONTENT
  • SCHEME_FILE

如果你深入挖掘,这里有更多关于打开文档的详细信息: 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();
}

关于android - 如何处理我的应用程序中收到的文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32886355/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com