If anyone can help I would be really awesome. I am building an app where by I am trying to access my files and display them in an imageview.
I have a button and to that I attach an onClickListener
iButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent photoPickerIntent = new Intent(Intent.ACTION_GET_CONTENT);
photoPickerIntent.setType("image/*");
startActivityForResult(Intent.createChooser(photoPickerIntent, "Select Picture"), 1);
}
});
The intent gives me 3 options Gallery, Dropbox and Google Drive
For the Gallery I am able to access the file lis this and display it in the imageview
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
imageHolder.setImageBitmap(BitmapFactory.decodeFile(picturePath));
For Dropbox I do it like this
imageHolder.setImageBitmap(BitmapFactory.decodeFile(selectedImage.getPath()));
However I am unsure of how to do it for google drive i tried to do it like the gallery but I get the following error
E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: /: open failed: EISDIR (Is a directory)
Any help would be very appreciated.
What is a Virtual File? A virtual file is an extension on an existing under-discussed concept in Android: a Uri does not have to map to anything directly usable by arbitrary apps.
You can see information about the activity on your file, including: Shared with tab: Shows people you've shared the file with. You can also email collaborators. All viewers (organization) tab: Shows people in your organization who have viewed the file.
Correct answer was given originally here: Google Drive GET_CONTENT intent read file
solution is to get it from contentResolver as InputStream
My full code to get image from whatever:
Uri selectedImage = imageReturnedIntent.getData();
if (selectedImage == null) {
return;
}
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
if (cursor != null && cursor.moveToFirst()) {
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
mCurrentPhotoPath = cursor.getString(columnIndex);
cursor.close();
}
if (TextUtils.isEmpty(mCurrentPhotoPath)) {
mCurrentPhotoPath = selectedImage.getPath();
}
Bitmap bitmap = null;
if (imageReturnedIntent.getDataString() != null && imageReturnedIntent.getDataString().contains("docs.file")) {
try {
InputStream inputStream = getContentResolver().openInputStream(selectedImage);
bitmap = BitmapFactory.decodeStream(inputStream);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
if (bitmap == null) {
bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With