Take a picture, crop it, then send image as email Intent - android

I've been at this for 3 days now and still no luck. I am trying to take a picture, crop it, then send it an email via intent on Android.
So far, I can take the pic and crop it. However, when I try to setup the email portion part, as soon as i take the pic, the email intent would pop up right after and doesnt allow me to crop. (Cropping is in background if i click on gmail).
So far I have tried :
#Override
public void onPictureTaken(byte[] arg0, Camera arg1) {
//Bitmap bitmapPicture = BitmapFactory.decodeByteArray(arg0, 0, arg0.length);
OutputStream imageFileOS;
try {
imageFileOS = getContentResolver().openOutputStream(uriTarget);
imageFileOS.write(arg0);
imageFileOS.flush();
imageFileOS.close();
Toast.makeText(DigitalSignature.this,
"Image saved: " + uriTarget.toString(),
Toast.LENGTH_LONG).show();
String[] recipients = new String[]{"digital.signature#lads.jetdelivery.com", "",};
Intent intent = new Intent("com.android.camera.action.CROP");
// this will open all images in the Galery
intent.setDataAndType(uriTarget, "image/jpeg");
intent.putExtra("crop", "true");
// this defines the aspect ration
intent.putExtra("aspectX", 20);
intent.putExtra("aspectY", 0);
// this defines the output bitmap size
//intent.putExtra("outputX", 256);
//intent.putExtra("outputY", 256);
// true to return a Bitmap, false to directly save the cropped iamge
intent.putExtra("return-data", false);
//save output image in uri
intent.putExtra(MediaStore.EXTRA_OUTPUT, uriTarget);
startActivity(intent);
Intent intent2 = new Intent(Intent.ACTION_SEND);
intent2.setType("image/jpeg");
intent2.putExtra(Intent.EXTRA_EMAIL, recipients);
intent2.putExtra(Intent.EXTRA_SUBJECT, job);
intent.putExtra(Intent.EXTRA_STREAM, uriTarget.getPath()); // Attaches image to Gmail
//File shareImg = new File(uriTarget);
//intent.putExtra(Intent.EXTRA_STREAM, uriTarget.fromFile(shareImg));
try {
startActivity(intent2);
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(DigitalSignature.this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
}
would anybody be able to help steer me in the right direction please?
Thanks, John.

You have to startActivityForResult on the CROP intent and after receive the result then you e-mail it.

Related

How to use bitmap to send to another app

can someone help me how to send an image to another application in android?
I want to send an image to be used as wallpaper, BBM display picture, and other applications that can use it (like WhatsApp, contacts, etc.)
I use this code, but it can only be used for sending text and not images as I want
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
sendIntent.setType("text/plain");
startActivity(sendIntent);
Then I tried to use this code to set the wallpaper
public void setAsWallpaper(Bitmap bitmap) {
try {
WallpaperManager wm = WallpaperManager.getInstance(_context);
wm.setBitmap(bitmap);
//disinimas
Toast.makeText(_context,
_context.getString(R.string.toast_wallpaper_set),
Toast.LENGTH_SHORT).show();
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(_context,
_context.getString(R.string.toast_wallpaper_set_failed),
Toast.LENGTH_SHORT).show();
}
}
the problem with the code, bitmap images directly applied as wallpaper. I wanted like sending text above, the user can choose to use another application. So I want a bitmap image that can later be used for wallpaper, BBM display picture, or other applications that support it
bitmap variable already contains the image that I want to, the images obtained from the internet with this code:
Bitmap bitmap = ((BitmapDrawable) fullImageView.getDrawable())
.getBitmap();
I use this code and its work, but give me a message BBM: File not found, WhatsApp: File is not an image:
Bitmap icon = bitmap;
Intent share = new Intent(Intent.ACTION_ATTACH_DATA);
share.setType("image/jpeg");
ContentValues values = new ContentValues();
values.put(Images.Media.TITLE, "title");
values.put(Images.Media.MIME_TYPE, "image/jpeg");
Uri uri = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI,
values);
OutputStream outstream;
try {
outstream = getContentResolver().openOutputStream(uri);
icon.compress(Bitmap.CompressFormat.JPEG, 100, outstream);
outstream.close();
} catch (Exception e) {
System.err.println(e.toString());
}
share.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(share, "Share Image"));
Thanks for your help Antrromet, finally I solved my problem with the following code:
Bitmap icon = bitmap;
// Intent share = new Intent(Intent.ACTION_SEND);
// share.setType("image/*");
ContentValues values = new ContentValues();
values.put(Images.Media.TITLE, "title");
values.put(Images.Media.MIME_TYPE, "image/*");
Uri uri = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI,
values);
OutputStream outstream;
try {
outstream = getContentResolver().openOutputStream(uri);
icon.compress(Bitmap.CompressFormat.JPEG, 100, outstream);
outstream.close();
} catch (Exception e) {
System.err.println(e.toString());
}
Intent intent = new Intent(Intent.ACTION_ATTACH_DATA);
intent.addCategory(Intent.CATEGORY_DEFAULT);
intent.setDataAndType(uri, "image/*");
intent.putExtra("mimeType", "image/*");
this.startActivity(Intent.createChooser(intent, "Set as:"));
Now the image can be used as Wallpaper, BBM profile picture, WA display Picture, Coontact display picture, etc. Thanks
Did you try using the following code? It is use to send binary data to other apps.
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, uriToImage);
shareIntent.setType("image/jpeg");
startActivity(Intent.createChooser(shareIntent, getResources().getText(R.string.send_to)));
Or if you dont have the URI with you but have the Bitmap instead, then try using the code as given here.
UPDATE:
Setting wallpaper and profile picture for BBM are totally different things and there is no common intent for them. For setting the wallpaper, you can try the following as given here.
Intent intent = new Intent(Intent.ACTION_ATTACH_DATA);
intent.addCategory(Intent.CATEGORY_DEFAULT);
intent.setDataAndType(uri, "image/jpeg");
intent.putExtra("mimeType", "image/jpeg");
this.startActivity(Intent.createChooser(intent, "Set as:"));
For BBM, the APIs are not open for changing the user image. So you cannot do that through your app.

Intent setDataAndType as bitmap

I'm trying to use a method to crop a image. Exactly this method:
private void performCrop() {
try {
//call the standard crop action intent (the user device may not support it)
Intent cropIntent = new Intent("com.android.camera.action.CROP");
//indicate image type and Uri
cropIntent.setDataAndType(**HERE**, "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, 1234);
}
catch(ActivityNotFoundException anfe){
Log.d("debugging","EXCEPTION");/*
//display an error message
String errorMessage = "Whoops - your device doesn't support the crop action!";
Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT);
toast.show();*/
}
}
This line, asks for a Uri Data and String Type.
cropIntent.setDataAndType(**HERE**, "image/*");
But I just want to put this bitmap
Bitmap fotoCreada;
as Data, but I don't know how to do it.
Is there any way to achieve this?
Thanks.
EDIT
Okay, this is my code right now:
There's a button. When you click it:
public void onClick(View v) {
Boolean isSDPresent = android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
if(isSDPresent) {
//Creem carpeta
File folder = new File(Environment.getExternalStorageDirectory().toString()+"/pic/");
folder.mkdirs();
//Recuperem data actual
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss");
String currentDateandTime = sdf.format(new Date());
//Cridem a la camara.
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
resultingFile=new File(folder.toString() + "/"+currentDateandTime+".jpg");
Uri uriSavedImage=Uri.fromFile(resultingFile);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
//Començem activity
startActivityForResult(cameraIntent, 1888);
} else {
Toast toast = Toast.makeText(getActivity(), "Issues while accessing sdcard.", Toast.LENGTH_SHORT);
toast.show();
}
}
As you can see, it's calling startActivityForResult. Basically takes a picture, and I retrieve it on activityResult:
public void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.d("debugging",""+resultCode);
if( requestCode == 1888 && resultCode == -1) { //-1 = TOT HA ANAT BE.
Bitmap photo = BitmapFactory.decodeFile(resultingFile.getPath());
Matrix matrix = new Matrix();
matrix.postRotate(90);
Bitmap photoRotated = Bitmap.createBitmap(photo, 0, 0, photo.getWidth(), photo.getHeight(), matrix, true);
this.fotoCreada=photoRotated;
((ImageView) myFragmentView.findViewById(R.id.fotoCapturada)).setImageBitmap(this.fotoCreada);
try {
FileOutputStream out = new FileOutputStream(this.resultingFile.getPath());
this.fotoCreada.compress(Bitmap.CompressFormat.PNG, 90, out);
} catch (Exception e) {
e.printStackTrace();
}
performCrop();
}
if (requestCode == 1234 && resultCode == -1){
Bitmap bitmap = (Bitmap) data.getParcelableExtra("BitmapImage");
Log.d("debugging",""+bitmap.getHeight());
}
}
And as you can see from previous code, as I retrieve the bitmap, I call method performCrop. It's code, now, is:
private void performCrop() {
try {
//call the standard crop action intent (the user device may not support it)
Intent cropIntent = new Intent("com.android.camera.action.CROP");
//indicate image type and Uri
//cropIntent.setDataAndType(this.picUri, "image/*");
cropIntent.putExtra("BitmapImage", this.fotoCreada);
//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, 1234);
Log.d("debugging","he acabat performCrop");
}
catch(ActivityNotFoundException anfe){
Log.d("debugging","EXCEPTION");
//display an error message
String errorMessage = "Whoops - your device doesn't support the crop action!";
Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT);
toast.show();
}
}
But startActivityForResult from performCrop never ends up calling onActivityResult. Log cat says it just entered once, and should enter Twice. First one from the camera Activity and second from Crop activity.
-1
he acabat performCrop
Hope it's clear enough.
Thanks.
Bitmap implements Parcelable, so you could always pass it in the intent:
Intent intent = new Intent(this, NewActivity.class);
intent.putExtra("BitmapImage", bitmap);
and retrieve it on the other end:
Bitmap bitmap = (Bitmap) intent.getParcelableExtra("BitmapImage");
However, if the bitmap exists as a file or a resource, its is always better to pass the URI or ResourceID of the bitmap and not the bitmap itself. Passing the entire bitmap requires a lot of memory. Passing the URL requires very little memory and allows each activity to load and scale the bitmap as they need it.
Use Intent.putExtra. A bitmap is a parceable, so it will work. setDataAndType is used for URIs, but you aren't referencing a resource on the web or on disk.

how can I save a file after cropping?

I am creating an android application. It takes a picture then allows you to crop it and then displays it. Problem is that it only saves the taken image not the cropped one. Basically i need it to save the cropped image. how can I save a file after cropping?
Code:
private void performCrop(){
//take care of exceptions
try {
//call the standard crop action intent (the user device may not support it)
Intent cropIntent = new Intent("com.android.camera.action.CROP");
//indicate image type and Uri
cropIntent.setDataAndType(picUri, "image/*");
//set crop properties
cropIntent.putExtra("crop", "true");
//indicate aspect of desired crop
cropIntent.putExtra("aspectX", 1);
cropIntent.putExtra("aspectY", 1.5);
//indicate output X and Y
cropIntent.putExtra("outputX", 256);
cropIntent.putExtra("outputY", 256);
//retrieve data on return
cropIntent.putExtra("return", 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){
//display an error message
String errorMessage = "Your device does not support cropping";
Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT);
toast.show();
}
}
Just add something like this:
try{
File file = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES
), fileName+".png"); //save to your pictures folder
outputFileURI = Uri.fromFile(file);
cropIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileURI);
startActivityForResult(cropIntent, PIC_CROP);
} catch (IOException ioe){
// handle your exception
}
Remember to refresh the gallery after saving so that its instantly available in the gallery. So use this code maybe in your onActivityResult method?
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse
("file://" + Environment.getExternalStorageDirectory())));
Edit: Found a better way to refresh the gallery since sendBroadcast can be a inefficient if you are just refreshing for one image. Use a MediaScanner to scan the file like so
Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
intent.setData(outputFileURI); // Add the path to the file
sendBroadcast(intent);
This will just scan for the new file and refresh that instead of the whole gallery.

Fail Image cropping in Android

so I want to choose an image from gallery and then crop it:
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,
"Select Picture"), PHOTO_PICKED_WITH_DATA);
OK, pick the photo and then catch it onActivityResult, then crop it:
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(mAvatarUri, "image/*");
intent.putExtra("crop", true);
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("outputX", ICON_SIZE);
intent.putExtra("outputY", ICON_SIZE);
intent.putExtra("scale", true);
intent.putExtra(MediaStore.EXTRA_OUTPUT, mAvatarUri);
intent.putExtra("return-data", true);
startActivityForResult(intent, CROP_IMAGE);
now, the problem is when i want to transform it into bytes and then send it inside an xml... it doesn't take only the cropped image but instead the whole image itself...
also, i cannot access cropped image uri, it says file not found!
hmmmm, seems like my cropped image weren't saved after all...
how can i suppose to fix this?
Samsung ACE 2.3.4
Check this code in the following link.
Crop Image
It works fine for me..
I don't know how you got that technique to crop image. But, for me, I always use this library. And it never failed to impress me. Work from android 2.1 all the way to 3.2 (never test it on 4.0 onward).
Here's how I do it:
Intent cropIntent = new Intent(imageProcessActivity,
CropImage.class);
cropIntent.putExtra("image-path",
FileUtil.saveTempFile(ImageProcessActivity.processedBitmap, filename));
cropIntent.putExtra("scale", true);
imageProcessActivity.startActivityForResult(cropIntent, ImageProcessActivity.INTENT_CROP_CODE);
and here's how to catch the result:
if (requestCode == INTENT_CROP_CODE && resultCode == RESULT_OK) {
Bundle extras = intent.getExtras();
if (extras != null) {
Uri uri = null;
uri = (Uri) extras.get("imageCrop");
Bitmap bitmap = null;
try {
bitmap = ImageUtil.decodeFile(
new File(new URI(uri.toString())),
AppConstant.MAX_IMAGE_SIZE);
} catch (URISyntaxException e) {
e.printStackTrace();
}
processedBitmap = bitmap;
selectedImage.setImageBitmap(bitmap);
}
}

Email a full sized photo

I'm working on an app that, among other things, uses the device's camera to take a photo and then share it through email.
The problem I'm having is that I can't get the app to take the full sized picture. It always sends a reduced in resolution version of the photo, although the camera is set to 5MP and quality when compressing is set to 100. Below you have my code:
private void takePicture(){
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, 1);
}
public void onActivityResult(int requestCode, int resultCode, Intent data){
if(requestCode == CAMERA_PIC_REQUEST && resultCode == Activity.RESULT_OK){
picture = (Bitmap) data.getExtras().get("data");
pictureView.setImageBitmap(picture);
ContentValues values = new ContentValues();
values.put(Images.Media.TITLE, "Picture");
values.put(Images.Media.BUCKET_ID, "picture_ID");
values.put(Images.Media.DESCRIPTION, "");
values.put(Images.Media.MIME_TYPE, "image/jpeg");
pictureUri = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, values);
OutputStream outstream;
try{
outstream = getContentResolver().openOutputStream(pictureUri);
picture.compress(Bitmap.CompressFormat.JPEG, 100, outstream);
outstream.close();
}catch(FileNotFoundException e){
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
.....
share.setOnClickListener(new OnClickListener(){
public void onClick(View view){
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_SUBJECT, selectedType);
intent.putExtra(Intent.EXTRA_TEXT,Notes + "\nLocation: " + selectedLocation+"\nOwner: " + selectedOwner
+ "\nStatus: " + selectedStatus);
intent.putExtra(Intent.EXTRA_STREAM, pictureUri);
try{
startActivity(Intent.createChooser(intent, "Send mail..."));
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(getApplicationContext(), "There are no email clients installed.", Toast.LENGTH_SHORT).show();
}
}
});
There is an extra for the ACTION_IMAGE_CAPTURE intent with the key MediaStore.EXTRA_OUTPUT which takes an URI to a file as the value. If you don't supply this extra, a size-reduced version of the taken image is returned to onActivityResult() with the data intent.
The reason for this is that a full-sized camera picture is simply too big for the intent system to handle (it might work in theory, but slows down the whole intent processing a lot - intents should be as small as possible in general). So it can't be delivered like the small-size version.
To use this extra modify your takePicture() method, e.g. like this:
private void takePicture() {
File outputFile = new File(Environment.getExternalStorageDirectory(),
"image.jpg");
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(outputFile));
startActivityForResult(intent, 1);
}
This does work like your method above, with the exception that the camera app has written a full-size copy of the image into the file you specified when onActivityResult() is called.
This means you don't have to write the image to the disk on your own, just open it from there when your onClickListener() is executed like you did already.

Categories

Resources