Saving Camera Bitmap to Storage, and Setting Image with Bitmap - android

I know the basics on how to take a picture and set it to ImageView.
photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
I want to do a little more than that.
I am saving it to a folder on to an SD card. That I have done successfully with this:
// intent
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
startActivityForResult(intent, CAMERA_REQUEST);
Here is my next question:
Not sure how to do this: What I'd like to do next: The next time I come to this Activity, I'd like to check if that image exists and assign it to that imageView.

Last days i faced this issue in one of my applications.
I'll try here to explain a little bitte what i have done.
Try to save the picture full path to a storage area or to your sharedpreferences.
Next time if you call your activity then check if a picture already exists and if you can use it.
Prepare in your xml layout an ImageView with visibility="gone" and if the point (2.) is true then you can change the visibility to visible and set the image in the view.
If the point (2.) is false then switch to camera view (SurfaceView) in order to take a new picture

You would have to use some sort of persistent storage to reference the file to check if it exists. I would just store it as a string in a preference and read it, then check if it exists and so on. Easy enough to do from the onCreate().

Related

MediaStore.Media.Images Android/ Kotlin: How to hide photos saved in gallery from user?

I am using the following program to capture an image using the MediaStore.ACTION_IMAGE_CAPTURE cameraIntent.
Code:
val values = ContentValues()
values.put(MediaStore.Images.Media.TITLE, "New Picture")
values.put(MediaStore.Images.Media.DESCRIPTION, "From the Camera")
imageUri = contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values)
//camera intent
val cameraIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri)
startActivityForResult(cameraIntent, IMAGE_CAPTURE_CODE)
I have the following questions:
The image that has been captured is being shown in the gallery. How do I hide that from the user?
After taking the image, I am setting the photo in a ImageView that is lagging the activity. How can I compress the image when I am setting it to the ImageView?
Is there a way to click photos only in potrait mode?
I tried changing the EXTERNAL_CONTENT_URI to INTERNAL_CONTENT_URIbut that gives me the following warning: Writing to internal storage is not supported android
As soon as you use the media store they are in 'the gallery'.
So you have to use getFilesDir() and classic file methods to save them for your eyes only.
The image that has been captured is being shown in the gallery. How do
I hide that from the user?
As blackapps have said, and in accordance to this documentation, which states that MediaStore API uses shared storage, you can't use MediaStore API to store in app-specific storage (Not the Gallery).
This documentation provides an example of storing photos to app specific storage using getExternalFileDir().
After taking the image, I am setting the photo in a ImageView that is
lagging the activity. How can I compress the image when I am setting
it to the ImageView?
Try Glide, a fast and efficient image loading library for Android. In addition, as I'm typing this, CommonsWare has also recommended other Image loading libraries such as Picasso and Coil.
Is there a way to click photos only in portrait mode?
Perhaps this SO Q&A might help. You can set theclickable attribute of the ImageView programmatically or declaratively.

How to draw on a photo IMMEDIATELY after taking it, THEN save to storage

I have searched around and found a few different solutions to this problem but none of them are really what I am looking for.
I am developing an app that lets the user take an image by opening the camera via a MediaStore.ACTION_IMAGE_CAPTURE Intent as so...
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
And then saves the image with
File photo = new File(Environment.getExternalStorageDirectory(), titleOfPhoto + ".jpg");
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
I even get the thumbnail back and display it for the user in the onActivityResult() Override.
As the title suggests, what I would like to do is allow the user to draw on the captured image immediately after taking it. But I have no control over what happens between opening the camera and returning to my app with the captured image already saved. Apps like SnapChat and WhatsApp are doing exactly this and so much more, so I know it is possible, but Android does not appear to have this capability built in.
To be clear: The sequence of events needs to be
1) Open the camera
2) Take a picture
3) Draw on the picture
4) Press 'Ok'/'Done' or something
5) Save the edited photo
6) Return thumbnail
Is there anyway to add an event listener to the camera, or open the camera in a different manner that gives more control? Any help is appreciated! Thanks!
In order to do this, you will have to use the Camera Framwork API.
The basic of this is to render the camera output on a Surface View in your app. Then when the user takes a picture, capture the state of the surface view. From there you can allow your users to draw on the picture. When they are finished, THEN save the picture and display its thumbnail.

Bitmap not setting to ImageView [duplicate]

I know the basics on how to take a picture and set it to ImageView.
photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
I want to do a little more than that.
I am saving it to a folder on to an SD card. That I have done successfully with this:
// intent
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
startActivityForResult(intent, CAMERA_REQUEST);
Here is my next question:
Not sure how to do this: What I'd like to do next: The next time I come to this Activity, I'd like to check if that image exists and assign it to that imageView.
Last days i faced this issue in one of my applications.
I'll try here to explain a little bitte what i have done.
Try to save the picture full path to a storage area or to your sharedpreferences.
Next time if you call your activity then check if a picture already exists and if you can use it.
Prepare in your xml layout an ImageView with visibility="gone" and if the point (2.) is true then you can change the visibility to visible and set the image in the view.
If the point (2.) is false then switch to camera view (SurfaceView) in order to take a new picture
You would have to use some sort of persistent storage to reference the file to check if it exists. I would just store it as a string in a preference and read it, then check if it exists and so on. Easy enough to do from the onCreate().

cant take picture with android intent when rotating the screen in android

i have a android application where i tae a picture in a intend like this:
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
startActivityForResult(takePictureIntent, actionCode);
then i fill a ImageView with the result.
now i found a bug, then i want to take a picture and during the camera is open i rotate the screen and take the picture, i dont get a result in my ImageView.
Only when not rotating the camera, while taking a picture i see it in the ImageView.
how to solve this?
I assume you have a get results method.
Make sure your file location hasnt been cleaned because of rotate(aka you are looking in the wrong place).
I had this problem. I was setting a global variable of where to save the photo to.
I passed this to the intent to take photo, and on return looked at that location to display photo.
However, if you rotate the screen, the global variable is destroyed, and so on result, it no longer knows where to find the photo.
The best thing to do is to save it to the saved instance, and reinitialse location onResume from the savedInstance.
See here on how to do it.

Android camera (getting imageView from a different class)

I'm currently working on a app which the user can answer a question through images by taking photos or uploading photos from the album.
I've separated into two classes which are FulfillPhotoTaskActivity, AddPhotoActivity.
FulfillPhotoTaskActivity have imageView, addphoto button and save button.
when I press addphoto button, it goes to AddPhotoActivity which we can select two options(taking photos or uploading photos). I finished the part where if the user press take photo button then it opens the camera and take the photo. I created onActivityResult which get the image data. but in many example, inside the onActivityResult they also have imageView, like setImageBitmap.
My problem is, how can I get the imageView from the AddPhotoActivity and shows it in FulfillPhotoTaskActivity which I already made for XML.
When you take a photo it saves the image to a file. You can retrieve this image file and then you can call your imageView in FulfillPhotoTaskActivity and set the imageView to the image that is in the file.
The following code will start the camera intent and the file to a given location:
File root = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Folder/SubFolder");
String folder = root.toString();
File file = new File(folder, "fileName" + ".jpg");
Uri outputFileUri = Uri.fromFile(file);
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(cameraIntent, 0);
Now when the user takes a picture you know where it will be saved. In your FulfillPhotoTaskActivity you can call your imageview:
ImageView imageView = (ImageView)findViewById(R.id.YOUR_IMAGE_VIEW_ID);
Finally, you can just set the image to the imageview:
imageView.setImageBitmap(BitmapFactory.decodeFile(file));
So, you don't need to get the imageview from AddPhotoActivity. You only need the file name. Then, you can go back to FulfillPhotoTaskActivity and set the imageview to the bitmap file.
I hope this helps!

Categories

Resources