Gallery intent.getData()在onActivityResult中返回null

时间:2014-07-15 13:18:00

标签: android android-intent android-gallery onactivityresult

我最近修复了某些设备上发生的应用中的known bug;当用户从我的应用启动的意图中拍摄照片时,onActivityResult Uri uri = intent.getData();会返回null。我设法在suggested manner.中解决了这个问题。但是,当用户需要从手机的照片库中选择一张图片时,我会遇到完全相同的问题。相同的intent.getData() == null

启动意图:

Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(Intent.createChooser(intent, "Select Picture"),     
R.id.SELECT_IMAGE_ACTIVITY_REQUEST_CODE);

onActivityResult:

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();
dataHasChanged(ACTION_PICTURE, filePath);

PS:奇怪的是,如果我开始如下所示的意图,我可以获得intent.getData(),但前提是我使用的是我的三星s4上的“图库”应用程序,而不是GooglePhotos应用程序。

Intent pickImageIntent = new Intent(
    Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
if (pickImageIntent.resolveActivity(getPackageManager()) != null)
    startActivityForResult(pickImageIntent, R.id.SELECT_IMAGE_ACTIVITY_REQUEST_CODE);

我不确定如何继续。我发现整个Android意图很容易让人感到困惑。

2 个答案:

答案 0 :(得分:1)

我给你代码供你参考:

点击应用中的图片后使用以下代码:

    Intent intent = new Intent();
    // call android default gallery
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    try {
        intent.putExtra("return-data", true);
        startActivityForResult(Intent.createChooser(intent,"Complete action using"), PICK_FROM_GALLERY);
    } catch (ActivityNotFoundException e) {
        // Do nothing for now
    }

以下代码将出现在onActivityResult()

        if (requestCode == PICK_FROM_GALLERY) {
            if (resultCode != RESULT_CANCELED) {
                Bundle extras2 = data.getExtras();
                if (extras2 != null) {
                photo = extras2.getParcelable("data");
                bitmap = photo;
                profile_imageView.setImageBitmap(photo);
                new ImageUploadTask().execute();
                }   
            }
        }

希望这能解决您的问题。

答案 1 :(得分:-1)

static final int REQUEST_GALLERY_IMAGE = 14;

----------
 Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
        photoPickerIntent.setType("image/*");
        startActivityForResult(photoPickerIntent, REQUEST_GALLERY_IMAGE );


 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
      if (requestCode == REQUEST_GALLERY_IMAGE && resultCode == RESULT_OK) {
            try {
                final Uri imageUri = data.getData();
                InputStream imageStream = getContentResolver().openInputStream(imageUri);
                final Bitmap selectedImage = BitmapFactory.decodeStream(imageStream);
                imagePreview.setImageBitmap(selectedImage);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }
 }