How to pass a JPEG image to another activity using bundle? - android

I'm now writing an app using android studio. In the main activity, I wrote a function to take a photo by using camera, then I want to pass this photo to the second activity. In the second activity, I want to upload this photo to a web API, and the type of the uploaded image has to be "JPEG".
I'm not sure I should use bundle or not. Or should I use "URI"? But when I call this:
URI photo_uri = photo.toURI();
There would be some problem. I couldn't run it. How can I fix this? This is what I wrote now, I use bundle:
// photo -> photo_uri, "photo.toURI()" doesn't work
Intent intent = new Intent(this, Main4Activity.class);
intent.putExtra("URI", photo_uri);
startActivity(intent);
And how can I receive the data(in JPEG type) in another activity?

You can send the file or uri path to second activity. After getting file or uri path you can convert that path into the file and upload it on the server.

You should check the Android docs about using the camera, there you have how to get the Uri, you must NEVER try to share the Bitmap itself.
About passing data between activities, you should use the Bundle so check this doc about Parcelables and Bundles which is the way to share data.
Regards!

Related

Android image editor intent: retrieving saved file location

I am using the ACTION_EDIT intent to launch the Android image editor in order to edit an image:
Intent editIntent = new Intent(Intent.ACTION_EDIT);
editIntent.setDataAndType(uri, "image/*");
When launching the intent with result, how do I retrieve the path of the edited / saved image?
I am using the ACTION_EDIT intent to launch the Android image editor in order to edit an image
Bear in mind that Android does not have an image editor. Some Android devices might ship with an image editor, and some users may have installed an image editor off of the Play Store or other distribution channels.
Also, since you know what the MIME type is of uri, you will have better results if you put the real MIME type into your Intent.
When launching the intent with result, how do I retrieve the path of the edited / saved image?
First, ACTION_EDIT does not support "launch the intent with result", which I am interpreting as meaning startActivityForResult(). As the ACTION_EDIT documentation states, there is no output. Just use startActivity().
Beyond that, you are the one who provided the Uri in the Intent for the editor, so at that point in time, you already knew what the Uri was that you handed to the editor. Hang onto that Uri in your saved instance state, so that you can ensure that you have the Uri even if Android terminates your process while the user is editing the image. While there is no requirement that an ACTION_EDIT Intent save the edited image back to the location specified by the Uri used to load the image, one hopes that most ACTION_EDIT activities do just that.

send raw image or binary data directly to the other application

I would like to ask if there is a possibility to send raw data to the other application? Example: taking pictures from a custom camera and send to other application. I observe that using intent is just only sending data which is already saved to storage, but i want that let the 2nd application decided either they save the image or discard. Just like in the tutorial of on how to make custom camera but in that instance, only one application running and only startActivityForResult and the listener which is onResultActivity. In my case, I want that from my custom camera, pass the raw data to the other application and let the other application manage the raw image receive. I call this raw image because this image is in the buffer and not on the storage. Is that possible to pass the raw image/data to other application?
You would need to save the file and pass the URI to the file via an Intent.
Uri image = Uri.parse(fileName);
Intent myIntent = new Intent(Intent.ACTION_VIEW, image);
myIntent.setType("image/*");
Intent chooserIntent = Intent.createChooser(myIntent, "Open Image");
startActivity(chooserIntent);

android - how to add to intent an internel stored image?

I have mange to store on my app imges files.
the directory path that contains the images is -
/data/data/yourapp/app_data/imageDir
now what im want to do is to add the relevent image to an intent -
so i have used the next code for the text parts -
Intent share = new Intent(android.content.Intent.ACTION_SEND);
share.setType("text/plain");
share.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
share.putExtra(Intent.EXTRA_SUBJECT, name);
share.putExtra(Intent.EXTRA_TEXT, body);
startActivity(Intent.createChooser(share, "Share with ?"));
But i don'tunderstand how can i add an image file-
by the way i have an option to get the full path of the image, but i sure don't understand how to add it to intent - so thanks for any kind of help
Let me try it clear it a bit -
if let's say i want to add the image to an e-mail - could it be enough to add to intent the image uri on the memory?!? and if so, what should i add to tne intent than, thanks
maybe to pass the whole image,you can do it just like passing objects of classes
then you will have to use parceable and all that which will be very tedious for you,but instead of sending the whole image(which may be very big sometimes)you can just pass the url of the image and fetch the url on the other activity and operate on it.It's a more efficient way to do it....
if you really want to pass the image you can convert the image into it's byte code using bitmapdecodefactory and send it,and then convert it back again....
Unless you plan on sending a very small image (kilobytes) I would actually recommend against sending images this way. It's usually better to simply send the URI or file system path, and allow the caller to retrieve the image that way.
Place it in the "extras" or similar within the intent you plan on sending.
If you plan on using internal storage (versus external images or media paths on the SDCard) you'll most likely want to create a ContentProvider / ContentResolver that other applications can use to get those image files from your sandboxed protected storage for your app.

Android:Problem in passing image and its URI between two activities

Can we pass image and an image URI to other activity in same application using bundle?suggest me some way to do that?
USAGE :actually i have made an activity that crop an image taken from camera or from image stored in SD card depends upon the user.
and another app that uses a background image and a border image both are overlay so as to see PHOTOFRAME.
So now I want to combine both app so that the cropped image come from first app should become the background image for the second app.ie comes in photoframe.How can i do that?
Once you save your image in SD card
use this to cal the other activity.
final Intent intent = new Intent(MyClass.this, TargetClass.class);
final String root = Environment.getExternalStorageDirectory().getAbsolutePath();
intent.setData(Uri.parse(root + "/my/image/path/image.png"));
startActivity(intent);
if you are the reciever then you can get the path by calling
Uri path = getIntent().getData();
in the onCreate of the recieving activity. this is the standard way of checking for path by other activities.
Yes you can pass URI object,see putParcelable method in http://developer.android.com/reference/android/os/Bundle.html, URI already implements Parcelable interface,you can use corresponding get methods to get it.If any object that implements Parcelable interface then we can pass it using Bundle.

How to save a picture to a file?

I'm trying to use a standard Intent that will take a picture, then allow approval or retake. Then I want to save the picture into a file.
Here's the Intent I am using:
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE );
startActivityForResult( intent, 22 );
The documentation says:
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. If
the EXTRA_OUTPUT is present, then the full-sized image will be
written to the Uri value of EXTRA_OUTPUT.
I don't pass extra output, I hope to get a Bitmap object in the extra field of the Intent passed into onActivityResult() (for this request). So where/how do you extract it? Intent has a getExtras(), but that returns a Bundle, and Bundle wants a key string to give you something back.
What do you invoke on the Intent to extract the bitmap?
Try calling getExtras().get("data") and casting the result to a Bitmap.
See here for an example.
On a related note, if you have the "crop" activity come up after taking the picture using intent.putExtra("crop", "true"), you'll get the cropped URI from getExtras().get("action").
I realize you've got this all fixed by now, just want to make sure no one tries to use this with crop and gets confused.
Reference: the apps-for-android LolCat activity.

Categories

Resources