15
votes

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.

4
What I discovered so far: the Uri returned is in the format: content://com.google.android.apps.docs.files/exposed_content/[base64-encoded-bytes]. [base64-encoded-bytes] is in fact a reversed, line-feed-and-semi-colon separated URL-encoded string, eg. 123...%3D%3D%0A%3B456... should be decoded into 456...123...== Base64-decoding 456...123...== results in 64 bytes of data, but I can't understand what those bytes are. Here's an example of the resulting 64-bytes I got: cl.ly/3Y2C1s0v3O0Q And it doesn't make more sense in HEX: goo.gl/7WV9KGuillaume Boudreau
Did you try any of the official documentation? developers.google.com/drive/integrate-android-uiDarwind

4 Answers

3
votes

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);
}
2
votes

Yeah, this can be like beating your head against a wall. Due to some drive side api idiosyncrasies, you might run into trouble using ACTION_GET_CONTENT. I ended up throwing in a hack to launch from that context menu and just used the standard integration code. Good luck!

1
votes

Right now you cannot be using ACTION_GET_CONTENT in Google Drive. It was wrongly supported by Google in the older version. But latest version of Drive (1.1.470.15) the intents have been disabled and ACTION_GET_CONTENT will not throw up Drive as an option.

Read below the issue discussion.
https://productforums.google.com/forum/#!topic/drive/siSKHXdE-ao/discussion

1
votes

If you want to open any file which you have retrieved from Drive, then this can be done easily while using the resources of Google Drive app. First of all install the Google Drive app from play market Install Google Drive

After that use this code to open your file.

   FileID=file.getId();//it is your file ID which you want to display

  String url = "https://docs.google.com/file/d/"+FileID;
  Intent i = new Intent(Intent.ACTION_VIEW);
  i.setData(Uri.parse(url));
   startActivity(i);

It will display your file of any type(image, pdf, doc, txt etc)