-->

Android: Not able to get original photo captured b

2019-02-27 11:04发布

问题:

In my application I capture photo by android camera and then I want to send it to the server. For this I use Client Socket programming. I convert the capture photo into bytearray(byte[]) and then send it to the server. Every thing work perfactally.

Problem is there I am not able to send original photo to the server. Thumbnail photo is sended by the android mobile phone. But when I capture photo by the camera then original photo is there in Gallery.

How to get this original photo to use in my application?

My Code:

Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        cameraIntent.putExtra("outputFormat",Bitmap.CompressFormat.JPEG);
        cameraIntent.putExtra("return-data", true);
    startActivityForResult(Intent.createChooser(cameraIntent, "Select Picture"),
                    CAMERA_REQUEST);

And in onActivityResult method:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == CAMERA_REQUEST) {
        Bitmap photo = (Bitmap) data.getExtras().get("data");
//          byte[] a = (byte[]) data.getExtras().getByteArray("data"); // I also use this but not work


        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        photo.compress(CompressFormat.JPEG, 100, bos);
        byte[] bitmapdata = bos.toByteArray();


    }
}

in bitmapdata photo is there but compressed photo not the original. Some of people say that, change quality field of compress(CompressFormat.JPEG, 100, bos); in between 0 to 100 but nothing will happen.

-- >> is there any other way also to get original photo which is captured by the camera

-- >> When The photo is in folder of my computer then I read this photo in the file by giving the path. ex- File file = new File(c:\newphoto\image.jpg); . can I use this code in android to read original photo because I know the name and location of photo. If yes then what is the path to read photo in gallery. Is this work if I give path as: \DCIM\Camera\photoName.jpg.

-- >> Or some change need in my current code and it will work fine?

回答1:

You are using the Intent to capture the image using ACTION_IMAGE_CAPTURE. If you normally starts your camera using Intent then it will return image as a bitmap in onActivityResult() but it will be for thumbnail purpose.

If you want to get full resolution image from the camera then you should provide a file with the intent which you are firing to start the camera activity.

You can do like below

Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
File out = Environment.getExternalStorageDirectory();
out = new File(out, imagename);
i.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(out));
startActivityForResult(i, CAMERA_RESULT);

Here there is a MediaStore.EXTRA_OUTPUT parameter which takes a Uri of the file in which you want your camera to write a images.

For more information you can refer below example

Capture full resolution image from camera