Android camera2 api - android

I need to take a picture from camera where it goes to another activity where we have image view to show the picture taken through camera.My questions is how to pass image data through intents using camera2api?
In which method in camera2api i need to pass data for another activity?

You don't need Intent to pass image data from one Activity of your app to another. Actually, you cannot use Intent to pass a picture: there are restrictions on the size of the data bundled with Intent. So, you can use Intent extras to pass the path to an image, or some parameters extracted from the image. The Camera app uses intent extra("data") to return a thumbnail (very small) image.
Using the standard MediaStore.ACTION_IMAGE_CAPTURE Intent for latest devices which feature hardware.camera2 API is not different from the older devices that used the deprecated hardware.Camera.
You can employ some private Intent-based mechanism to communicate between activities of your app. In this case the consumer will probably pass necessary parameters to capture activity with startActivityForResult() and get the results in onActivityResult(). There are no methods in hardware.camera2 that can (or could be expected to) help you with these tasks.

Related

ActivityResultContracts.TakePicture with front camera

I'm using ActivityResultContracts to capture images, is there any way by which I can launch my front camera by default?
As per the documentation it is possible to pass extra data by overriding createIntent method, can I use that here?
It seems to be possible by using Intent but is there any way to achieve this using ActivityResultContracts?

what is the best way to pass image from one activity another in android

In my apps, i need to pass image of image view from one activity to another. I know there are different kinds of way to pass image from activity to activity. But i want to know the best approach to do that. Previously i tried to pass image by getting the Bitmap of imageview from first activity then put in as putextra to the intent then extract the bitmap by getPercible to second activity. This was working good in Lollipop but getting error in Nougat. Now, i am trying to pass the bitmap as byteArray, but this is more unreliable than previous one. I don't know how to overcome this situation. Please help me in this context.
Where do you get your image from?
I would advise you to store it locally and only pass the path to it to the other activity. In this activity just retrieve the Bitmap again from your local storage.

Getting images from Gallery/Camera using MVP

I am writing an app that allows user to either take a picture with the camera
or choose an image in the gallery. I have a Fragment whose the layout displays
two boutons, depending on the button pressed I use intent either to start the
camera or open the Gallery. It works fine.
My purpose now is to do that with MVP. As I see things
my fragment is the View
the place from where images come from is the Model so in this case the models
would be getting images from the gallery or with the camera
a Presenter asks models to give him a picture and forwarding the image
to the fragment
The problem is model objects are Pojo classes and to retrieve an image from the
Camera or Gallery the class needs to implement startActivityForResult to retrieve
the photo taken/choosed.
How can I get image from camera/gallery with a class that is not a Fragment or
an Activity ?
How can I move code for camera/gallery in POJO class ?
Is it overall a good idea ? I did not find any MVP examples where retrieving
images was done out of a Fragment or an Activity ?
Note: Maybe is such an architecture possible with RxAndroid but I wish I could do that first without third party librairies.
Thanks for all your suggestion
How can I get image from camera/gallery with a class that is not a Fragment or an Activity ?
You have to do all this stuff from your presenter, not from your POJO class. Here you want to access an android resource, so pass the call back to the 'view-layer' from 'presenter-layer' and get response from system. Once the response is received in the onActivityResult method of Fragment/Activity, pass it into the presenter. cheers :)

What's the difference between ACTION_EDIT and ACTION_VIEW?

They all transfer info in data and seem like when I start a new intent with them all start a new activity. So I don't really know the difference between them.
When starting a new activity using an intent, you could choose to view some data or to edit it depending on your needs by specifying one of ACTION_EDIT or ACTION_VIEW respectively. It may be the case that both of these intents start the same activity and so the result is the same whatever you choose or it might be that they are mapped to different activities and that ACTION_EDIT will start an activity where the data is editable and ACTION_VIEW will start an activity where it is not. As the name suggests, you should choose the action that best matches your intent. If you are specifying a filter for your activity, you should keep this in mind too and filter the actions that your activity is designed to fulfill.
EDIT: Data refers to what you intend to act on. Normally, an Intent contains two things: an action and the data you want to perform the action on. Here are the brief descriptions from the javadoc (which is very good, I suggest you read the section on Intent Resolution):
action -- The general action to be performed, such as ACTION_VIEW, ACTION_EDIT, ACTION_MAIN, etc.
data -- The data to operate on, such as a person record in the contacts database, expressed as a Uri.
Using that information, Android will attempt to find the most suitable activity to service your intent (one with a filter that matches your intent). Activities in your application take precedence over those outside it, but is is possible and common to invoke an activity in a 3rd party app.

Method to pass an image value?

How would i go about passing an image to the next page for android?
You have a few different choices here.
I assume by "next page" you mean the next Activity? I also assume you've created a Bitmap out of the image. Bitmap implements Parcelable so you could just put it directly into the extras of the intent. This also lets you can put it in the Bundle in your activity's onSaveInstanceState() method, so it is restored whenever that activity is rotated or is recreated for some other reason.
However, if you have more than one image, this is less than ideal, since you have to serialize and deserialize all your images each time you switch activities. If you obtained the image from the file system somewhere, or if you got it from a ContentProvider like the MediaStore, then you'll have a Uri for it. So, you could just put the Uri in the intent, and then recreate the bitmap each time you load the activity. This has the advantage of being a smaller amount of serialized data, but it's even worse in terms of processing because now you have to read from the filesystem and decompress the image every time.
Therefore, if you are concerned with performance, the only good method is to store the image(s) to a static variable so that it can be accessed by your other activities. This way, both activities actually use the same image instead of duplicating it, thereby saving memory. The only disadvantage with this approach is that you will not be able to start the activity in a new task. All activities that use the image must run in the same process. Also, if you are manually recycling the image when you're done (via the Bitmap.recycle() method), then you'll have to make sure no one else is using the image before you recycle it.
Personally, a lot of my apps download images from a server and I store all HTTP responses in a cache, so whenever I need one of these images I re-request it from the cache. The cache is a singleton so it can be accessed from any of my activities.
Do you have a sample code for passing an image value?
Will this work?
Intent i = new Intent(Moods.this, New_Entry.class);
Bundle f = new Bundle();
f.putString("image", img);

Categories

Resources