Android : 使用 Camera 将图像显示到 ImageView 并保存到 Gallery

Android : Use Camera to display image to ImageView and Save to Gallery

我正在制作一个应用程序,它可以选择从相机或画廊获取图像并将其显示在 ImageView 中。 我从 Android Developers Tutorial. 我正在为此使用 DialogInterface:

private void selectImage() {
        final CharSequence[] items = { "Take Photo", "Choose from Library",
                "Cancel" };

        AlertDialog.Builder builder = new AlertDialog.Builder(ReportIncident.this);
        builder.setTitle("Add Picture");
        builder.setItems(items, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int item) {
                if (items[item].equals("Take Photo")) {
                    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

                        try {
                            photoFile = createImageFile();
                            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));

                            startActivityForResult(takePictureIntent, 0);


                        } catch (IOException ex) {
                            // Error occurred while creating the File
                            ex.printStackTrace();
                        }

                    }
                } else if (items[item].equals("Choose from Library")) {
                    Intent intent = new Intent(
                            Intent.ACTION_PICK,
                            android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                    intent.setType("image/*");
                    startActivityForResult(
                            Intent.createChooser(intent, "Select File"), 1);
                } else if (items[item].equals("Cancel")) {
                    dialog.dismiss();
                }
            }
        });
        builder.show();
    }

createImageFile 创建 directory/File 保存照片的位置:

private File createImageFile() throws IOException {
    // Create an image file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String imageFileName = "FIREFLOOD_" + timeStamp + "_";
    File storageDir = Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_PICTURES);
    File image = File.createTempFile(
            imageFileName,  /* prefix */
            ".jpg",         /* suffix */
            storageDir      /* directory */
    );

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

'Choose from Library' 部分已经在工作,但我对 'Take Photo' 部分有疑问。具体在这一行:

takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
galleryAddPic();
startActivityForResult(takePictureIntent, 0);

Having the putExtra() and galleryAddPic() before the startActivityOnResult() crashes the app after taking the picture but successfully saves the image to gallery. But deleting these two methods from the code displays the photo taken to the ImageView reportImage.

startActivityForResult显示通过activityOnResult拍摄到ImageView的图片:

public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode == Activity.RESULT_OK) {
            if (requestCode == 1) {

                onSelectFromGalleryResult(data);
            }else if (requestCode == 0) {
                Bundle extras = data.getExtras();
                Bitmap imageBitmap = (Bitmap) extras.get("data");
                reportImage.setImageBitmap(imageBitmap);




            }
        }
    }

将图像保存到图库由 galleryAddPic 执行:

private void galleryAddPic() {

        Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
        File f = new File(mCurrentPhotoPath);
        Uri contentUri = Uri.fromFile(f);
        mediaScanIntent.setData(contentUri);
        this.sendBroadcast(mediaScanIntent);
    }

The question is where can I put these two methods (putExtra and galleryAddPic) to make these two funtions work:

  1. display the photo taken to ImageView
  2. save the photo to gallery at the same time.

I can't make these two work altogether. Please help. I tried to put the putExtra and galleryAddPic on the onAcitivityResult but it still crashes. galleryAddPic won't work without the putExtra.

本教程将帮助您尝试这个 link http://www.androidhive.info/2013/09/android-working-with-camera-api/