来自Google Drive Android Intent的文件查看

如果有人能帮助我真的很棒我正在构建一个应用程序,我正在尝试访问我的文件并将其显示在图像视图中.

我有一个按钮,我附加一个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和Google Drive

对于画廊,我可以访问该文件并将其显示在图像视图中

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我这样做

imageHolder.setImageBitmap(BitmapFactory.decodeFile(selectedImage.getPath()));

不过我不确定如何做这个谷歌的驱动器我试图像画廊,但我得到以下错误

E / BitmapFactory:无法解码流:java.io.FileNotFoundException:/:open failed:EISDIR(is a directory)

任何帮助将非常感激.

正确答案是在这里给出的: Google Drive GET_CONTENT intent read file

解决方案是从contentResolver获取它作为InputStream

我的完整代码从任何以获得图像:

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

代码日志版权声明:

翻译自:http://stackoverflow.com/questions/16992461/file-view-from-google-drive-android-intent

我来评几句
登录后评论

已发表评论数()

相关站点

+订阅
热门文章