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) I
That is no uri. No content scheme. Tell the complete scheme please. – RegainimportedFileUri.toString()
. As that is the real content scheme. – RegainTrying to convert a URI
. I do not see that you are trying to change the uri. That is not needed of course. – RegainURI getString :content://com.google.android.apps.docs.storage/document/acc%3D1%3Bdoc%3D4089
. How can i get an inputstream fro this url/uri? – JeradURI getString
? toString() you mean? Your code is ok. – Regainjava.io.FileNotFoundException: File is virtual: acc=1;doc=4089
when i dois = contentResolver.openInputStream(importedFileUri);
. should i replace it with importedFileUri.toString or something? – Jeraddata.getData()
and made no change. – JeradBAD REQUEST
exception is thrown afterward in my HTTP request(where I upload this file to server). I assume this is the same error (bad request implying there is something wrong with what i sent). so the error seems to be device specific, although in both cases I still cant use the resultant byte array – Jerad