在我的应用中即时上传和Google+图片

Jer*_*oen 6 android android-gallery google-plus

我目前遇到以下问题:当我想从库中检索图像时,我使用以下代码来启动库的意图.

public void useGallery() {
    this.intentbasedleave=true;
    Intent intent = new Intent(Intent.ACTION_PICK,
            android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    intent.setType("image/*");
    startActivityForResult(
            Intent.createChooser(intent, getString(R.string.pleaseselect_image)), IMAGE_PICK);
}
Run Code Online (Sandbox Code Playgroud)

当我从库中获取数据时,我使用此方法:

private void imageFromGallery(int resultCode, Intent data) {
    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 filePath = cursor.getString(columnIndex);
    cursor.close();

    this.updateImageView(BitmapFactory.decodeFile(filePath));
}
Run Code Online (Sandbox Code Playgroud)

这是有效的,除非所选图像来自Google+或即时上传.然后BitmapFactory.decodeFile(filePath))似乎为空?因为该方法引发了一个nullpointer异常.

因此,我的问题是:如何使用Google+中的图片以及图库中的即时上传?

f2p*_*eek 1

使用BitmapFactory.decodeUri而不是BitmapFactory.decodeFile.

您可以将您的方法简化imageFromGallery

private void imageFromGallery(int resultCode, Intent data) {
  Uri selectedImage = data.getData();
  updateImageView(BitmapFactory.decodeUri(getContext().getContentResolver(), selectedImage));
}
Run Code Online (Sandbox Code Playgroud)

(假设您可以从某处访问上下文)。