Trying to convert a URI that I got from Intent.ACTION_GET_CONTENT, input stream opens fine for local files, but for URI from drive (/document/acc=1;doc=4089) I get a FileNotFoundException, saying the file is "virtual". How can I open an input stream for such files?
Getting the 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!");
}
and
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());
}
Getting the inputstream:
InputStream is = null;
try {
is = contentResolver.openInputStream(importedFileUri);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
Result:
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
The Exception:
java.io.FileNotFoundException: File is virtual: acc=1;doc=4089
but for URI from drive (/document/acc=1;doc=4089) IThat is no uri. No content scheme. Tell the complete scheme please. - greenappsimportedFileUri.toString(). As that is the real content scheme. - greenappsTrying to convert a URI. I do not see that you are trying to change the uri. That is not needed of course. - greenapps