Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

File View from Google Drive Android Intent

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.

like image 785
user2464901 Avatar asked Jun 07 '13 20:06

user2464901


People also ask

What is virtual file in Android?

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.

Can you see who viewed your Google Drive folder?

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.


1 Answers

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);
}
like image 106
Roger Alien Avatar answered Sep 19 '22 08:09

Roger Alien