Android App Development
iOS App Development
Flutter App Development
Cross Platform App Development
Hire on-demand project developers and turn your idea into working reality.
Big thanks to Webkul and his team for helping get Opencart 3.0.3.7 release ready!
Deniel Kerr
Founder. Opencart
Top Partners
Taking photo is a very basic feature of the android. But if you want to add this feature to your app then?
So in this blog, we have shown you how to click the picture and save it to your custom directory.
There are some basic steps to add this functionality to your app,
Step 1: Add Request Camera Permission to your manifest.xml
<uses-feature android:name="android.hardware.camera" android:required="true" /> 12 <uses-feature android:name="android.hardware.camera" android:required="true" />
Step 2: Add the below code,
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); // ------- add below lines to store the photos to custom directory ------- File imagesFolder = new File(Environment.getExternalStorageDirectory(), "My Images"); imagesFolder.mkdirs(); mUri = Uri.fromFile(new File(imagesFolder, "IMG_" + String.valueOf(System.currentTimeMillis()) + ".jpg")); intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, mUri); startActivityForResult(intent, REQUEST_CAMERA); 1234567 Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);// ------- add below lines to store the photos to custom directory -------File imagesFolder = new File(Environment.getExternalStorageDirectory(), "My Images");imagesFolder.mkdirs();mUri = Uri.fromFile(new File(imagesFolder, "IMG_" + String.valueOf(System.currentTimeMillis()) + ".jpg"));intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, mUri);startActivityForResult(intent, REQUEST_CAMERA);
By above code, your all the photos will be stored in the My Images folder.
Note: Above Lollipop version you have to add the permission check here.
Step 3: Get the bitmap and set it to the ImageView,
@Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == REQUEST_CAMERA) { if (resultCode == Activity.RESULT_OK) { Bitmap clickedImage; String filename = ""; String path = mUri.getPath(); if (path.length() > 0) { filename = path.substring(path.lastIndexOf("/") + 1, path.length()); clickedImage = BitmapFactory.decodeFile(path); setImageAndName(clickedImage, filename); } } } } 12345678910111213141516 @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == REQUEST_CAMERA) { if (resultCode == Activity.RESULT_OK) { Bitmap clickedImage; String filename = ""; String path = mUri.getPath(); if (path.length() > 0) { filename = path.substring(path.lastIndexOf("/") + 1, path.length()); clickedImage = BitmapFactory.decodeFile(path); setImageAndName(clickedImage, filename); } } } } set the image, public void setImageAndName(Bitmap clickedImage, String displayName) { ImageView eachOptionImage = (ImageView) mMainView.findViewById(R.id.all_options).findViewWithTag(mClickedButtonTag).findViewById(R.id.each_option_image_preview); eachOptionImage.setImageBitmap(clickedImage); TextView eachOptionName = (TextView) mMainView.findViewById(R.id.all_options).findViewWithTag(mClickedButtonTag).findViewById(R.id.each_option_name_tv); eachOptionName.setText(displayName); } 1234567 public void setImageAndName(Bitmap clickedImage, String displayName) { ImageView eachOptionImage = (ImageView) mMainView.findViewById(R.id.all_options).findViewWithTag(mClickedButtonTag).findViewById(R.id.each_option_image_preview); eachOptionImage.setImageBitmap(clickedImage); TextView eachOptionName = (TextView) mMainView.findViewById(R.id.all_options).findViewWithTag(mClickedButtonTag).findViewById(R.id.each_option_name_tv); eachOptionName.setText(displayName); }
set the image,
Maybe this blog is helpable to you.
Thanks.
Your email address will not be published. Required fields are marked*
Name*
Email*
Save my name email and website in this browser for the next time I comment.
Be the first to comment.
We use cookies to personalize your experience. By continuing to visit this website you agree to our use of cookies. Learn more about privacy policy
The results provided were above our expectations both in deliverability timeline as well as operation. They are a top-notch company with many resources and a company we feel honoured to work with again!
Lee Seward
Founder, Gro-Connect
USA
India
Global
Name
Email
Enquiry or Requirement
If you have more details or questions, you can reply to the received confirmation email.