Photo taken through camera intent is losing on quality

Srujan Barai

The similar SO question-answers is crashing my app.

I am taking a photo through my camera intent and sending it to my php server over net which is then saving it to a directory. It is working fine. But the photo being saved is of compromised quality (~20KB).

I know my mistake. I read Android documentation to realize I am actually sending the photo thumbnail instead of the photo itself. Here is my code

Open Camera Intent to take a photo.

addImage = (ImageButton) findViewById(R.id.imageButton);
addImage.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(cameraIntent, CAMERA_REQUEST);
    }
}

Receiving the Image.

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK){
        photo = (Bitmap) data.getExtras().get("data");
        addImage.setImageDrawable(null);
        addImage.setBackgroundColor(Color.parseColor("#ffffff"));
        addImage.setImageBitmap(photo);

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        photo.compress(Bitmap.CompressFormat.JPEG, 100, baos);
        byte[] photoByte = baos.toByteArray();
        encodedImage = Base64.encodeToString(photoByte,Base64.DEFAULT);

    }
}

What do I want? I simply want to convert my original image (and not the thumbnail) to Base64 encoded string (Just like what I did with the thumbnail).

Any help will be appreciated.

[ Feel free to suggest edits. :) ]

CommonsWare

I simply want to convert my original image (and not the thumbnail)

You do not have an "original image", because you did not include EXTRA_OUTPUT in your ACTION_IMAGE_CAPTURE Intent. Add that, and then you have your full-size image (except with buggy camera apps):

/***
 Copyright (c) 2008-2016 CommonsWare, LLC
 Licensed under the Apache License, Version 2.0 (the "License"); you may not
 use this file except in compliance with the License. You may obtain a copy
 of the License at http://www.apache.org/licenses/LICENSE-2.0. Unless required
 by applicable law or agreed to in writing, software distributed under the
 License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
 OF ANY KIND, either express or implied. See the License for the specific
 language governing permissions and limitations under the License.

 From _The Busy Coder's Guide to Android Development_
 https://commonsware.com/Android
 */

package com.commonsware.android.camcon;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import java.io.File;

public class CameraContentDemoActivity extends Activity {
  private static final String EXTRA_FILENAME=
    "com.commonsware.android.camcon.EXTRA_FILENAME";
  private static final String FILENAME="CameraContentDemo.jpeg";
  private static final int CONTENT_REQUEST=1337;
  private File output=null;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Intent i=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    if (savedInstanceState==null) {
      File dir=
        Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);

      dir.mkdirs();
      output=new File(dir, FILENAME);
    }
    else {
      output=(File)savedInstanceState.getSerializable(EXTRA_FILENAME);
    }

    if (output.exists()) {
      output.delete();
    }

    i.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(output));

    startActivityForResult(i, CONTENT_REQUEST);
  }

  @Override
  protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);

    outState.putSerializable(EXTRA_FILENAME, output);
  }

  @Override
  protected void onActivityResult(int requestCode, int resultCode,
                                  Intent data) {
    if (requestCode == CONTENT_REQUEST) {
      if (resultCode == RESULT_OK) {
        // spawn an IntentService to encode and upload your File
      }
    }
  }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to detect if photo was taken (camera intent)

From Dev

Android camera and photo picker intent

From Dev

Trying to display image taken from camera intent

From Dev

How to set the size and quality of a picture taken with the android camera?

From Dev

Custom camera shows dark picture preview when photo taken on Nexus

From Dev

Android Camera: Get Picture Uri after the photo is taken

From Dev

Is it possible to check if an image is taken by camera or uploaded from photo library by JavaScript?

From Dev

Android: Can't get the size of photo taken by camera acitivity

From Dev

Android - Saving photo taken from Camera , Out of memory

From Dev

ImageView does not display image taken from phone camera or photo gallery

From Dev

How can i set an image taken from the Camera intent into a ImageView?

From Dev

Take photo w/ camera intent and display in imageView or textView?

From Dev

Android : Take a photo from with in my layout without starting a camera intent

From Dev

How to pass through a separate parameter through the Camera intent in android

From Dev

Activity rotates after taking camera through intent in S4

From Dev

Can't receive Intent.extras() from Camera after making a photo

From Dev

Camera intent onActivityResult code saves (blank) image even if photo was not accepted by user

From Dev

Camera intent onActivityResult code saves (blank) image even if photo was not accepted by user

From Dev

Android - Ensuring that photo orientation is preserved when taking photos via Camera Intent?

From Dev

How to add taken photo to MediaStore

From Dev

Detect Camera photo folder

From Dev

Choose photo from camera

From Dev

Camera find saved photo

From Dev

Get uri of picture taken by camera

From Dev

Delete picture taken from camera

From Dev

How can I get the default size (in pixels) of a photo which will be captured through Android camera, before capturing it?

From Dev

I want to read exif info in image in android. I can read exif from image in gallery but i cannot read exif photo taken from the camera

From Dev

Android: Taken photo is bigger than the preview

From Dev

Getting location data for a taken photo in Android

Related Related

  1. 1

    How to detect if photo was taken (camera intent)

  2. 2

    Android camera and photo picker intent

  3. 3

    Trying to display image taken from camera intent

  4. 4

    How to set the size and quality of a picture taken with the android camera?

  5. 5

    Custom camera shows dark picture preview when photo taken on Nexus

  6. 6

    Android Camera: Get Picture Uri after the photo is taken

  7. 7

    Is it possible to check if an image is taken by camera or uploaded from photo library by JavaScript?

  8. 8

    Android: Can't get the size of photo taken by camera acitivity

  9. 9

    Android - Saving photo taken from Camera , Out of memory

  10. 10

    ImageView does not display image taken from phone camera or photo gallery

  11. 11

    How can i set an image taken from the Camera intent into a ImageView?

  12. 12

    Take photo w/ camera intent and display in imageView or textView?

  13. 13

    Android : Take a photo from with in my layout without starting a camera intent

  14. 14

    How to pass through a separate parameter through the Camera intent in android

  15. 15

    Activity rotates after taking camera through intent in S4

  16. 16

    Can't receive Intent.extras() from Camera after making a photo

  17. 17

    Camera intent onActivityResult code saves (blank) image even if photo was not accepted by user

  18. 18

    Camera intent onActivityResult code saves (blank) image even if photo was not accepted by user

  19. 19

    Android - Ensuring that photo orientation is preserved when taking photos via Camera Intent?

  20. 20

    How to add taken photo to MediaStore

  21. 21

    Detect Camera photo folder

  22. 22

    Choose photo from camera

  23. 23

    Camera find saved photo

  24. 24

    Get uri of picture taken by camera

  25. 25

    Delete picture taken from camera

  26. 26

    How can I get the default size (in pixels) of a photo which will be captured through Android camera, before capturing it?

  27. 27

    I want to read exif info in image in android. I can read exif from image in gallery but i cannot read exif photo taken from the camera

  28. 28

    Android: Taken photo is bigger than the preview

  29. 29

    Getting location data for a taken photo in Android

HotTag

Archive