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

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.

Related

Use URI to get Bitmap in Another Activity

I have got the URI of an Image in an Activity for example: ImageActivity and set the URI to an object named Item.
val item = Item()
item.imageURI = uri
Now, I have got the returned result from ImageActivity to for example: UploadActivity.
Now, I want to upload the Item object that I got from ImageActivity from UploadActivity. I am uploading the image by encoding the Bitmap of the image to Base64. So, I need the Bitmap of the image.
I got that by this:
val bitmap = MediaStore.Images.Media.getBitmap(contentResolver, item.imageURI)
item.itemImage = imageToString(bitmap)
But it is showing error:
java.lang.SecurityException: Permission Denial: opening provider com.miui.gallery.provider.GalleryOpenProvider from ProcessRecord
How to solve this? Please elaborate on your answer as much as possible.
I am getting this image from Gallery
I am going to interpret this as meaning that you are using an Intent like ACTION_GET_CONTENT or ACTION_PICK.
If so, then by default, the only place that Uri can be used is in the activity instance that receives it (ImageActivity in your case).
Your options include:
Get rid of UploadActivity and do something else. For example, you could have the actual uploading be performed by a repository, with the UI handled by ImageActivity (directly, via fragments, etc.).
Pass the Uri to UploadActivity via the "data" facet of an Intent, plus include FLAG_GRANT_READ_URI_PERMISSION on that Intent
That second option would look something like:
startActivity(Intent(this, OtherActivity::class.java).setData(yourUri).addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION))
(where yourUri is your Uri)

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

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!

Can't launch intent to show local image

Within my fragment, I am trying to start an intent to display a local image with a gallery app on my phone.
The three lines in question are
string path = String.Format ("content:/{0}.jpg", CacheController.Static.GetPath (m));
Android.Net.Uri uri = Android.Net.Uri.Parse(path);
StartActivity (new Intent (Intent.ActionView, uri));
the value of path is content://data/data/Appname.subname/files/cache/107.jpg.
I tried using file:/ at the beginning of the Uri but that didn't help.
You are trying to share an image that is in a folder private to your app. You first need to copy the image to a public folder and make an intent pointing to that image. Have a look here and here

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);

store,then send the image from camera to other activity

Hi in my page there are three option to make the picture to display in the another activity. option one memory card,camera,horizontal list view of images. From this what ever the user may choose but the image should be displayed in the next activity image view. Please guide me to do further !! am just open the camera and the memory card and after that i dont know how to send it to next activity.
Basically, you need to pass the information of the image to the next activity using Intent.
You should use:
Intent intent = new Intent(firstActivity.this, secondActivity.class);
intent.putExtra("fileName","SEE_BELOW");
startActivity(intent);
Option A:
if you are using an image from SDcard - replace SEE_BELOW with the actual String of the image Path.
Option B:
if you are getting the image from a ListView - use the arg2 in the onItemSelected method to get the position selected in the list and pull the image from there, save it to SD, and sending the String path like in Option A.
Option C:
if you are getting the image from the camera - I would save the image to SD, and then send the String path via Intent like in Option A.
Now, in your SecondActivity, in the onCreate method you call:
Intent intent = getIntent();
String image = intent.getStringExtra("fileName");
Now in your image variable you have the address of the image - just set it to your imageView and you are done!

Categories

Resources