它将意图转移到相机但没有收到点击的图片,但是当我选择图库选项时,它会收到所选图片

时间:2017-08-06 17:51:11

标签: android android-intent

我试图传递从画廊或相机或文件管理器接收图片的意图。它已成功传递意图但未接收图片但是在图库的情况下它接收所选图片

我的代码:

private void choosePhotoFromGallery() {
    Intent pickIntent = new Intent();
    pickIntent.setType("image/*");
    pickIntent.setAction(Intent.ACTION_GET_CONTENT);
    Intent takePhotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    String pickTitle = "Take or select a photo";
    Intent chooserIntent = Intent.createChooser(pickIntent, pickTitle);
    chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { takePhotoIntent });
    startActivityForResult(chooserIntent, 1);}

protected void onActivityResult(int requestCode, int resultCode, Intent    data) {
    if (resultCode != Activity.RESULT_OK) {
        return;
    }

    switch (requestCode) {
        case 1:
            try {



InputStream inputStream = getContentResolve().openInputStream(data.getData());
                Bitmap bitmap = BitmapFactory.decodeStream(inputStream);

                   imgView.setImageBitmap(scaledBitmap);

            } catch (Exception e) {
                Logger.log(e.getMessage());
            }
            break;

    }
}

1 个答案:

答案 0 :(得分:0)

您的代码存在以下问题:

data.getData()

您需要从返回的Intent中获取额外内容,如下所示:

data.getExtras().get("data");

所以你的InputStream应该像 -

InputStream inputStream = getContentResolve().openInputStream(data.getExtras().get("data"));

更新1:

您还可以通过先创建文件然后将文件URI传递给该意图来修改您的方法。这可以避免您在以前的方法中遇到的设备特定问题。 -

 Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
 // Ensure that there's a camera activity to handle the intent
 if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
                            // Create the File where the photo should go
       File photoFile = null;
       try {
             photoFile = createImageFile();
           } catch (IOException ex) {
             ex.printStackTrace();
           }
      // Continue only if the File was successfully created
      if (photoFile != null) {

            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
                                        Uri.fromFile(photoFile));
                                startActivityForResult(takePictureIntent, 1734);
      }
 }

 @Override
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
      if (requestCode==1734 && resultCode==RESULT_OK)
        {
           BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(path, options);
        final int REQUIRED_SIZE = 200;
        int scale = 1;
        while (options.outWidth / scale / 2 >= REQUIRED_SIZE
                && options.outHeight / scale / 2 >= REQUIRED_SIZE)
            scale *= 2;
        options.inSampleSize = scale;
        options.inJustDecodeBounds = false;
        Bitmap bitmap = BitmapFactory.decodeFile(path, options);
     }
}

// This is just a method to create a File with current timestamp in name
private File createImageFile() throws IOException {
    // Create an image file name
    File image=null;
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String imageFileName = "JPEG_" + timeStamp + "_";
    if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
        File storageDir = getExternalFilesDir(null);
        if (!storageDir.exists()) {
            storageDir.mkdir();
        }
        image = File.createTempFile(
                imageFileName,  /* prefix */
                ".jpg",         /* suffix */
                storageDir      /* directory */
        );

        // Save a file: path for use with ACTION_VIEW intents
        mCurrentPhotoPath = image.getAbsolutePath();
    }
    return image;

}