Android Camera Intent: how to get full sized photo?

Viewed 96716

I am using intent to launch camera:

Intent cameraIntent = new Intent(
    android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
getParent().startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);

and using:

Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
photoImage.setImageBitmap(thumbnail);
photoImage.setVisibility(View.VISIBLE);

But it is only a thumbnail, how do I get the full bitmap? I know I can use my own Activity and use:

Camera.PictureCallback()

But is there anyway to do it using Intent?

Thanks!

edit:

I also tried:

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    Uri uri = data.getData();
    imageView.setImageURI(uri);
}

It works for photo selected from gallery, but for camera intent, data.getData() returns null.

8 Answers

API Level 29

I tried the accepted answer, but both Environment.getExternalStorageDirectory() used in the accepted answer and Environment.getExternalStoragePublicDirectory() used in a subsequent answer are deprecated in API level 29. So is the MediaStore.Images.Media.DATA used in the third answer deprecated in API level 29. The following code (in Kotlin) stores the full image in MediaStore as shown in this stackoverflow answer to a different question and doesn't rely on the FileProvider:

    var imageUri: Uri? = null

    fun takePhoto(view: View?) {
        val values = ContentValues()
        values.put(MediaStore.MediaColumns.DISPLAY_NAME, "myimage.jpg")
        values.put(MediaStore.MediaColumns.MIME_TYPE, "image/jpeg")
        values.put(MediaStore.MediaColumns.RELATIVE_PATH, Environment.DIRECTORY_PICTURES)
        imageUri = contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values)

        val intent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
        intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri)
        startActivityForResult(intent, TAKE_PHOTO_REQUEST)
    }

To capture maximum picture size from camera, i hope these simple steps will be quite useful

 public static Camera mCamera;

Camera.Parameters parameters = mCamera.getParameters();
  parameters.getSupportedPictureSizes();
List<Camera.Size> supportedSizes = parameters.getSupportedPictureSizes();
  mSizePicture1 = supportedSizes.get(0);
  int cameraSize = supportedSizes.size();
  mSizePicture2 = supportedSizes.get(cameraSize - 1);

    if (mSizePicture1.height < mSizePicture2.height)
       mSizePicture = supportedSizes.get(cameraSize - 1);
    else
       mSizePicture = supportedSizes.get(0);

parameters.setPictureSize(mSizePicture.width, mSizePicture.height);

Here, the supported size of the each mobile is taken, from that which size is maximum that is fixed as picture size to capture.

Ok Time to pay back. So u have your permissions on Mainfest

    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-feature android:name="android.hardware.camera" android:required="true" />
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

and u also have your provider meta-data

 <provider
        android:name="androidx.core.content.FileProvider"
        android:authorities="${applicationId}.fileprovider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/file_paths"></meta-data>
    </provider>

One missed detail is (android:authorities applicationId) u need to add your own app package name. So u have xml file under res folder as we mentioned on manifest and under it u have created file_paths with ;

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-files-path
        name="my_images"
        path="Pictures" />
</paths>

we are done with copy paste part 1. Now on our activity above onCreate(savedInstanceState: Bundle?) define these beauties

    val REQUEST_IMAGE_CAPTURE = 1
    lateinit var currentPhotoPath: String
    var cameraIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)

U might like to have a look at original resource but missing details as usual Android Developers : link

An other missing detail is packageName + ".fileprovider", be carefull u need to give your own package name if u allredy have method.

// org android developers
    private fun dispatchTakePictureIntent() {
        Intent(MediaStore.ACTION_IMAGE_CAPTURE).also { takePictureIntent ->
            // Ensure that there's a camera activity to handle the intent
            takePictureIntent.resolveActivity(packageManager)?.also {
                // Create the File where the photo should go
                val photoFile: File? = try {
                    createImageFile()
                } catch (ex: IOException) {
                    // Error occurred while creating the File
                    ex.message
                    null
                }
                // Continue only if the File was successfully created
                photoFile?.also {
                    val photoURI: Uri = FileProvider.getUriForFile(
                        this,
                        packageName + ".fileprovider",
                        it
                    )
                    takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI)
                    startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE)
                }
            }
        }
    }

createImageFile function

 // org android developers
    @Throws(IOException::class)
    private fun createImageFile(): File {
        // Create an image file name
        val timeStamp: String = SimpleDateFormat("yyyyMMdd_HHmmss").format(Date())
        val storageDir: File = this!!.getExternalFilesDir(Environment.DIRECTORY_PICTURES)!!
        return File.createTempFile(
            "JPEG_${timeStamp}_", /* prefix */
            ".jpg", /* suffix */
            storageDir /* directory */
        ).apply {
            // Save a file: path for use with ACTION_VIEW intents
            currentPhotoPath = absolutePath
        }

    }

Testing. call your dispatchTakePictureIntent() method with onClick event, make sure permissions are permitted

 @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
  if (resultCode == Activity.RESULT_OK && requestCode == REQUEST_IMAGE_CAPTURE) {
var  mBitmap_org = MediaStore.Images.Media.getBitmap(
                this.getContentResolver(),
                //Uri.parse(currentPhotoPath)
                Uri.fromFile(File(currentPhotoPath))
            );
        //mImg_display?.setImageBitmap(mBitmap_org)
}
    }

Don't check data we will get it through imagePath. If u are checking Uri.parse(currentPhotoPath) make sure it's Uri.fromFile(File(currentPhotoPath))

Now u have your bitmap, time to spend other hours/days how to resize decode, save.

There is also a method to save tokken image, maybe you could help me to see where should I put it, if I need tokken image to be save on gallery

  // org android developers
    private fun galleryAddPic() {
        Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE).also { mediaScanIntent ->
            val f = File(currentPhotoPath)
            mediaScanIntent.data = Uri.fromFile(f)
            sendBroadcast(mediaScanIntent)
        }
    }
Related