Take photo and save original size - android

How can get photo from intent
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
in onActivityResult not in URI format but JPG,PNG orginal size and save it gallery.
Need JPG,PNG because after shot I adding watermark on it.
I don't know why after bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out); returned photo size from 2048x1536 is 256x192.

For saving the image in the gallery you have send a broadcast as shown in the code sample below.
If you need the image with orignal sizes you could simply catch the uri and set it to a ImageView directly. Although it is a good practice to scale it down and use a smaller version of your bitmap to avoid the OutOfMemory Exception.
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
if(requestCode==CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE){
if(resultCode==RESULT_OK){
saveToGallery(mImageUri);
yourImageView.setImageURI(mImageUri);
}
}
}
public void saveToGallery(Uri uri){
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,uri));
}

Related

How to get name of captured image in android onActivityResult

how to get image name from captured image/intent data. any one suggest me. i need through intent data only
Here is my code:
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, PIC_FROM_CAMERA);
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if(resultCode == RESULT_OK && data != null){
Bitmap photo = (Bitmap) data.getExtras().get("data");
}
}
AS from the Android documentation here you could not get any name for your image since android wont name for your file but you could get the time when the image was taken and the dimension of the image from This answer You could name the image by yourself and send that bitmap.

catch `intent not found` and call another method

I want to crop an image and call com.android.camera.action.CROP. As this doesn't exist on all devices I want to have a fallback to catch this and simply add the image w/o cropping.
To do so I rename com.android.camera.action.CROP to xcom.android.camera.action.CROPfor testing purposes and call in the catch another method with another resultCode NO_CROPbut always the actual resultCode PICK_CROPis called also.
How can I ignore PICK_CROPin this case so that it doesn't jump to drawImage()always where data is of course null and avoid a null poiner exception and instead go to noCrop()?
Here's my code:
//Start cropping intent
private void performCrop(int requestCode, Intent data) {
// take care of exceptions
try {
// call the standard crop action intent (the user device may not
// support it)
Intent cropIntent = new Intent("xcom.android.camera.action.CROP");
// indicate image type and Uri
cropIntent.setDataAndType(imgUri, "image/*");
// set crop properties
cropIntent.putExtra("crop", "true");
// indicate aspect of desired crop
cropIntent.putExtra("aspectX", 1);
cropIntent.putExtra("aspectY", 1);
// indicate output X and Y
cropIntent.putExtra("outputX", 256);
cropIntent.putExtra("outputY", 256);
// retrieve data on return
cropIntent.putExtra("return-data", true);
// start the activity - we handle returning in onActivityResult
startActivityForResult(cropIntent, PIC_CROP);
}
// respond to users whose devices do not support the crop action
catch (ActivityNotFoundException anfe) {
// add chosen photo directly to imageview
Bundle extras = data.getExtras();
Bitmap photo = extras.getParcelable("data");
noCrop(photo, NO_CROP, requestCode, data);
}
}
// draw image in circle canvas w/o crop -> fallback if crop doesn't exist on users device
private void noCrop(Bitmap photo, int requestCode, int resultCode, Intent data) {
if (requestCode == NO_CROP) {
ImageView profilepic = (ImageView) findViewById(R.id.profilepic);
GraphicsUtil graphicUtil = new GraphicsUtil();
profilepic.setImageBitmap(graphicUtil.getCircleBitmap(photo, 16));
saveProfile(photo);
} else {
super.onActivityResult(requestCode, resultCode, data);
}
}
// Draw cropped image to imageview with circle canvas
private void drawImage(int requestCode, int resultCode, Intent data) {
if (requestCode == PIC_CROP) {
Bundle extras = data.getExtras();
ImageView profilepic = (ImageView) findViewById(R.id.profilepic);
Bitmap photo = extras.getParcelable("data");
// display the returned cropped image
GraphicsUtil graphicUtil = new GraphicsUtil();
profilepic.setImageBitmap(graphicUtil.getCircleBitmap(photo, 16));
saveProfile(photo);
} else {
super.onActivityResult(requestCode, resultCode, data);
}
}
As you said com.android.camera.action.CROP intent is not supported in all devices so why use it and write a fallback mechanism. There are many libraries available for cropping images. I have used Cropper and simple-image-crop library from github. Have a look at my answer here about simple-image-crop library.
OK, I did a mistake. It's actually working to catch if the intent is not available. After some deeper debugging I found out that it crashes at Bitmap photo = extras.getParcelable("data"); because I'm not passing the correct data :-(
Sorry for any inconvenience!

Bitmap quality is very low

I'm trying to build an app, that captures a picture and sends the picture to another activity.
I'm trying to display this picture in the second activity and apparently the quality of picture is very low.
Here is my code so far
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, PICTURE_TAKEN);
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == RESULT_OK){
Intent canvasIntent = new Intent(this ,canvas.class);
Bundle extras = data.getExtras();
bmpCameraResult = (Bitmap)extras.get("data");
canvasIntent.putExtra("bmp_Image", bmpCameraResult);
startActivity(canvasIntent);
}
and in the canvasActivity, I'm trying to receive the bitmap in the conventional fashion and trying to display it.
Intent intent= getIntent();
Bitmap bmp = (Bitmap) intent.getParcelableExtra("bmp_Image");
ImageView iv = new ImageView(this);
iv.setImageBitmap(bmp);
setContentView(iv);
What could be the reason for this reduction of quality? What is the optimized way of getting the high quality image?
Thank You.
Did you try to set jpegQuality (setJpegQuality) in your Camera Parameters (Camera.Parameters) as you can read here: Camera Feature Documentation?
I think you should also read these answers about the same issue:
Android: Not able to get original photo captured by camera (Able to read compress photo only)
Low picture/image quality when capture from camera

Intent pick Image From Camera compressed or with lower resolution or dimension

I'm using an intent to get an image from Camera and then I should use the data returned from intent in my Activity to display the image in an ImageView.
I want to ask if I can ask to the system to get a lower resolution or dimension image adding to the intent some putExtra(..., ...) options, so I can avoid to compress bitmap and implement all the code (I'm sure system knows better than me how to do :) ).
I hope my request is clear and someone could help me!
Sorry for my english - Regards from Italy
You can get a thumbnail of the image taken by using data.getExtras().get("data");:
final static int CAMERA_PIC_REQUEST = 1; // any int value you want
Intent mCameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(mCameraIntent, CAMERA_PIC_REQUEST);
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
switch (requestCode) {
case CAMERA_PIC_REQUEST:
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
// your ImageView
photoImage.setImageBitmap(thumbnail);
break;
}
}
}
No, you can't ask the camera to do this unfortunately. You have to get the bitmap and then sort it out yourself. See http://developer.android.com/training/displaying-bitmaps/load-bitmap.html

Android take picture and get path?

Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
...
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_PIC_REQUEST) {
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
ImageView image = (ImageView) findViewById(R.id.photoResultView);
image.setImageBitmap(thumbnail);
}
How to get image's path to string?
As it's said in the docs:
The caller may pass an extra EXTRA_OUTPUT to control where this image will be written. If the EXTRA_OUTPUT is not present, then a small sized image is returned as a Bitmap object in the extra field. This is useful for applications that only need a small image. If the EXTRA_OUTPUT is present, then the full-sized image will be written to the Uri value of EXTRA_OUTPUT.

Categories

Resources