gpt4 book ai didi

android - 如何打开云端 "virtual"文件的输入流?

转载 作者:行者123 更新时间:2023-12-02 20:48:50 25 4
gpt4 key购买 nike

尝试转换从 Intent.ACTION_GET_CONTENT 获取的 URI,输入流对于本地文件可以正常打开,但对于来自驱动器的 URI (/document/acc=1;doc=4089) 我收到一个 FileNotFoundException,表示该文件是“虚拟”的。如何打开此类文件的输入流?

获取 URI:

 Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.setType("*/*"); //No I18N
try{
startActivityForResult(Intent.createChooser(i, "Pick a file"), REQUEST_CODE_UPLOAD_FILE_FOR_IMPORT);
}catch (android.content.ActivityNotFoundException ex){
Log.e("Error","FileManager not found!");
}

importedFileUri = data.getData();
System.out.println("URI path: "+importedFileUri.getPath()+" "+importedFileUri.getEncodedPath());
System.out.println("URI Scheme "+importedFileUri.getScheme());
System.out.println("URI Authority :"+importedFileUri.getAuthority());
System.out.println("URI Fragment :"+importedFileUri.getFragment());
System.out.println("URI path segments : ");
for(String str : importedFileUri.getPathSegments()){
System.out.println("\t" +str );
}
String ext;
if (importedFileUri.getScheme().equals(ContentResolver.SCHEME_CONTENT)) {
final MimeTypeMap mime = MimeTypeMap.getSingleton();
ext = mime.getExtensionFromMimeType(mActivity.getContentResolver().getType(importedFileUri));
System.out.println("resolved type (content) : "+ mActivity.getContentResolver().getType(importedFileUri));
} else {
ext = MimeTypeMap.getFileExtensionFromUrl(Uri.fromFile(new File(importedFileUri.getPath())).toString());
System.out.println("resolved type (other) : "+ Uri.fromFile(new File(importedFileUri.getPath())).toString());
}

获取输入流:

InputStream is = null;
try {
is = contentResolver.openInputStream(importedFileUri);
} catch (FileNotFoundException e) {
e.printStackTrace();
}

结果:

URI path: /document/acc=1;doc=4089 /document/acc%3D1%3Bdoc%3D4089

GetString -content://com.google.android.apps.docs.storage/document/acc%3D1%3Bdoc%3D4089

URI Scheme content

URI Authority :com.google.android.apps.docs.storage

URI Fragment :null

URI path segments :

document

acc=1;doc=4089

resolved type (content) : application/vnd.google-apps.spreadsheet

ext : null

异常(exception):

java.io.FileNotFoundException: File is virtual: acc=1;doc=4089

最佳答案

检查 URI 处的文件是否为虚拟:

private static boolean isVirtualFile(Uri uri) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
if (!DocumentsContract.isDocumentUri(activity, uri)) {
return false;
}

Cursor cursor = activity.getContentResolver().query(
uri,
new String[]{DocumentsContract.Document.COLUMN_FLAGS},
null, null, null);
int flags = 0;
if (cursor.moveToFirst()) {
flags = cursor.getInt(0);
}
cursor.close();
return (flags & DocumentsContract.Document.FLAG_VIRTUAL_DOCUMENT) != 0;
} else {
return false;
}
}

使用以下命令从虚拟文件获取输入流:

private static InputStream getInputStreamForVirtualFile(Uri uri, String mimeTypeFilter)
throws IOException {

ContentResolver resolver = activity.getContentResolver();

String[] openableMimeTypes = resolver.getStreamTypes(uri, mimeTypeFilter);

if (openableMimeTypes == null ||
openableMimeTypes.length < 1) {
throw new FileNotFoundException();
}

return resolver
.openTypedAssetFileDescriptor(uri, openableMimeTypes[0], null)
.createInputStream();
}

总体

if (isVirtualFile(sourceuri)) {
input = getInputStreamForVirtualFile(sourceuri, getMimeType(name));
} else {
input = activity.getContentResolver().openInputStream(sourceuri);
}

更多信息here .

关于android - 如何打开云端 "virtual"文件的输入流?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46422736/

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