I want to call the camera through intent, capture images, and save it locally in the gallery and in the app-specific storage how can I achieve that?
I went through this question also but the code is not working I don’t know why.
I want to call the camera through intent, capture images, and save it locally in the gallery and in the app-specific storage how can I achieve that?
I went through this question also but the code is not working I don’t know why.
4
Answers
I’m assuming the app-specific storage is some sort of cache, in your
onActivityResult()
wouldn’t you save the picture first to app-specific storage and then copy it toEnvironment.DIRECTORY_PICTURES
?Whenever you take a photo from the device, you the image uri in the
onActivityResult
. Now to save it to another location, you can use the code given belowAnd in the
onActivityResult
, you can call it like thisI have done something like this Before, I Share my code with you and others that may help.
in this scenario if you want to save captured image, you have to check which api level that device is running with. for api level 28 and below you have to use specific method and for api level 28 above you have specific way.
so first step, start from:
AndroidManifest.xml
and add this line in to your AndroidManifest.xml application tag:
if your project targetSdkVersion is 28 and above, you have to use ActivityResultLauncher otherwise you have to override onRequestPermissionResult in your fragment or activity.
if you use fragment you have to register your ActivityResultLauncher in onAttach method, if not register it in onCreate of your Activity, so do this:
registerWriteExternalStoragePermission has this codes:
so next step is to get back result of captured image from intent and change it to Bitmap for this section we need another activityResultLauncher so do this in onAttach of your fragment or onCreate of your activity:
so next step is to check your app has storage permission or not:
ok as we near the end of this answer 🙂 you have to implement saveImageInAndroidApi28AndBelow method, se lets go to do it:
and the final step is to implement saveImageInAndroidApi29AndAbove method do it like this:
so you have to check which API Level that device is running and with with little code handle what you want:
Don’t FORGET TO call .launch ON YOUR ActivityResultLauncher
have a good time with the codes 🙂
Simple, make a Launcher attach the camera and gallery and you have everything in one place, here I send you 2 links to make a launcher
https://code.tutsplus.com/es/tutorials/build-a-custom-launcher-on-android–cms-21358
https://steemit.com/utopian-io/@ideba/how-to-build-a-custom-android-launcher-and-home-screen-application-part-1