简体   繁体   中英

What is the difference between these two ways of taking photos in Android?

I want to understand what is going on in these two scenarios. I am using code directly from the official docs at http://developer.android.com/training/camera/photobasics.html

Assume that all the necessary read/write permissions are present in the Manifest file.

Scenario 1:

static final int REQUEST_IMAGE_CAPTURE = 1;

private void dispatchTakePictureIntent() {
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
        startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
        Bundle extras = data.getExtras();
        Bitmap imageBitmap = (Bitmap) extras.get("data");
        mImageView.setImageBitmap(imageBitmap);
    }
}

This code appears to work for me, but I don't know what it's doing exactly. It basically starts up the camera and lets me take a picture, and then in onActivityResult, I can get the thumbnail this way bu pulling it from the intent.

Scenario 2:

String mCurrentPhotoPath;

private File createImageFile() throws IOException {
    // Create an image file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String imageFileName = "JPEG_" + 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;
}

static final int REQUEST_TAKE_PHOTO = 1;

private void dispatchTakePictureIntent() {
    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) {
            // Error occurred while creating the File
            ...
        }
        // Continue only if the File was successfully created
        if (photoFile != null) {
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
                    Uri.fromFile(photoFile));
            startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
        }
    }
}

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
            Bundle extras = data.getExtras(); //data is null here!!!
            Bitmap imageBitmap = (Bitmap) extras.get("data");
            mImageView.setImageBitmap(imageBitmap);
        }
    }

This scenario, on the other hand, is supposed to let me get the full file, but it doesn't work and throws an error for me because the Intent variable is null, so I don't have access to anything.

My questions:

  1. When you take a picture using the Android camera, does it typically create two files -- a thumbnail and a full file?

  2. In scenario 1, these file(s) are not being written to internal or external storage, but just the RAM, right? The thumbnail is saved in the Intent, but does this imply that the full file is lost / unattainable?

  3. In scenario 2, the Intent is null because I had used takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile)); for some reason. How do I know where this file is? Do I even really need mPhotoPath or should I be keeping access to File photoFile , which I've lost by the time I reach the onActivityResult method? What is the proper practice for keeping hold of this file? Making it a member variable?

  4. The image creation function in this example uses getExternalStoragePublicDirectory -- is this different from saving in internal storage? I had assumed there were many ways to save a file: Internal storage, RAM, SD Card, storage device connected to the phone (such as an external HDD), the Cloud, etc. How do I know what is what?

  1. It will either return a thumbnail in the Intent (if you don't specify a filename) or save the file to the filename you specified in the original Intent extras. I don't believe there is a way to do both. If you want to save the full image and then make a thumbnail, you'll have to create the thumbnail yourself. See here for details on the Intent: http://developer.android.com/reference/android/provider/MediaStore.html#ACTION_IMAGE_CAPTURE
  2. The file is being saved to the photoFile Uri that you specified in the Intent here: takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));

  3. You should store the Uri somewhere, probably as a member variable, so you can access it later. That is the Uri that the image will be saved to if a picture is successfully taken.

  4. This is going to be the Android user's default Photos folder in this case. getExternalStoragePublicDirectory will be on whatever media is used for shared data - probably an SD card or internal SD storage.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM