从谷歌推动Android意向文件视图视图、意向、文件、Android

2023-09-04 11:12:36 作者:在你身后

如果有人可以帮助我会很真棒。我建立一个应用程序,其中由我试图访问我的文件,并在ImageView的显示出来。

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.

我有一个按钮,并给我附加一个onClickListener

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

这样做的目的给了我3个选项画廊,Dropbox的和谷歌驱动器

The intent gives me 3 options Gallery, Dropbox and Google Drive

有关图库我能够访问该文件LIS这一点,并在ImageView的显示它

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));

有关Dropbox的我不喜欢这样

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:无法取消code流:java.io.FileNotFoundException:/:打开失败:EISDIR(是一个目录)

E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: /: open failed: EISDIR (Is a directory)

任何帮助将是非常美联社preciated。

Any help would be very appreciated.

推荐答案

正确答案是给原本此处的谷歌驱动器GET_CONTENT意图读取文件

Correct answer was given originally here: Google Drive GET_CONTENT intent read file

解决方案是从ContentResolver的把它作为的InputStream

solution is to get it from contentResolver as InputStream

我完全code,以获得图像的任何

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);
}
 
精彩推荐
图片推荐