Now you can Subscribe using RSS

Submit your Email

Thursday

How to get full sized Image from Camera Android

Vishal Shrestha
Getting Image from Camera in Android is pretty simple, however it gets pretty tricky if you need to get full quality image from the camera. When you launch intent with action image capture and only use the data.getExtras().get("data"), what you basically get is just the thumbnail. For getting the high quality full size photo, you need to save the high quality photo in the phone storage or external storage and then get that photo using Uri. Without saving the photo, you cannot get a high quality photo from the camera. Luckily it's not that complex also, here are the steps involved in getting full sized picture from the camera in Android.

full-size-from-action-image-capture-intent-android

Get full sized image from camera in Android.

  1.  Create FileProvider: Most important thing to get a full sized image is to use a file provide. It is using file provider that you access the file that has been stored after been taken from the camera. To create fileProvider, write the following code in Manifest.xml:
      <provider
                android:name="android.support.v4.content.FileProvider"
                android:authorities="com.vysh.fullsizeimage.fileprovider"
                android:exported="false"
                android:grantUriPermissions="true">
                <meta-data
                    android:name="android.support.FILE_PROVIDER_PATHS"
                    android:resource="@xml/file_paths"></meta-data>
            </provider>
  2. Create file path in xml: Now you need to create a file in xml that defines path where images will be stored. Create a new folder in res called xml and create a new file called file_paths.xml inside it and add the following code:

    <?xml version="1.0" encoding="utf-8"?>
    <paths>
        <external-path name="my_images" fullpath="storage/emulated/0/Pictures/" />
    </paths>
  3. Get file write permission: As I've already mentioned you'll need to save the high quality full resolution first, before you can use it. So you need to the file write permissions so that you can save the photo first. Define this in manifest first - 

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

    Now if you plan on using the app on devices greater then Marshmallow, which you probably should right now you will need  to ask for permission on runtime. Here's how you do it-

    requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);

    Now you can handle whether the user has selected give permission of not in onRequestPermissionsResult. Once you have the permission, create an Image_capture intent.
  4. Create Image capture intent: Now create a new intent with image capture action. Here's the code:
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
  5. Create a file and get file path and Start the action image capture intent:Now you need to create file, this is the file in which the image will be saved. Once the file is created get the path of the file.

       File photoFile = null;
                try {
                    photoFile = createImageFile();
                } catch (IOException ex) {
                    // Error occurred while creating the File
                    Log.d("mylog", "Exception while creating file: " + ex.toString());
                }
                // Continue only if the File was successfully created
                if (photoFile != null) {
                    Log.d("mylog", "Photofile not null");
                    Uri photoURI = FileProvider.getUriForFile(ActivityMain.this,
                            "com.vysh.fullsizeimage.fileprovider",
                            photoFile);
                    takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
                    startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
                }


    Here's the code for createImageFile()

    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 = 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 = image.getAbsolutePath();
            Log.d("mylog", "Path: " + mCurrentPhotoPath);
            return image;
        }
  6. Handle the intent response in onActivityResult(){}. Now you've got everything in place, now just get the full size image from the storage and display in an imageView or upload it or do anything you want with it. Here how you get the full size image from storage after it's been captured.
    @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
                setPic();
            } else if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK) {
                Uri uri = data.getData();
                try {
                    Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
                    ivGallery.setImageBitmap(bitmap);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
You are done, now you should be able to successfully get full sized image from your phone's camera. If you have any problem, feel free to comment below. Happy coding!

Vishal Shrestha / Author & Founder

A developer by profession, a born Adventurer. I mainly do Android but like to get my hands dirty with web development and a little bit of Python. I would't rather go on a Trek than a party and you can find me having a few rounds with the heavy bag to let out the steam ;)

For Business info : My Portfolio Site.

0 comments:

Post a Comment

Coprights @ 2017 | The Code City by Vishal Shrestha Vishal Shrestha