File View from Google Drive Android Intent

2019-03-12 14:03发布

问题:

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.

回答1:

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:

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!



回答3:

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



回答4:

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)