試圖轉換我從Intent.ACTION_GET_CONTENT
得到的URI
,輸入流打開罰款的本地文件,但URI
從驅動器(/document/acc=1;doc=4089
)我得到FileNotFoundException
,說該文件是「虛擬」。我怎樣才能打開這樣的文件的輸入流?如何在雲上打開「虛擬」文件的inputstream?
獲取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:
InputStream is = null;
try {
is = contentResolver.openInputStream(importedFileUri);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
結果:
URI路徑:/ document/acc = 1; doc = 4089/document/acc%3D1%3Bdoc%3D4089
GetString - content://com.google.android.apps.docs.storage/document/acc% 3D1%3Bdoc%3D4089
URI方案內容
URI機關:com.google.android.apps.docs.storage
URI片段:空
URI路徑段:
文件
ACC = 1; DOC = 4089
解決類型(內容):應用/ vnd.google-apps.spreadsheet
EXT:空
除外:
java.io.FileNotFoundException:文件是虛擬的:acc = 1; doc = 4089
當然可以。請顯示你的代碼。 – greenapps
'但是對於來自驅動器的URI(/ document/acc = 1; doc = 4089)I'這不是uri。沒有內容計劃。請告訴完整的方案。 – greenapps
@greenapps這是我的代碼和我的結果 – ColonD