0

i just want to know is there any way to launch the backcamera using just intent. i don't want to use intent.putextra thing. so basically my code should look like this

final Intent cameraIntent = new Intent();
            cameraIntent.putExtra("android.intent.extras.CAMERA_FACING", 0);
            cameraIntent.setPackage(defaultCameraPackage);
            cameraIntent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
            cameraIntent.addFlags(Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION);
            cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, cameraUrl);
            ((Activity) mContext).startActivityForResult(cameraIntent, CHOOSE_PHOTO_INTENT);

But above code is sometimes launching the front camera. i want to launch the back camera directly. Help me with this if it is possible in Android.

and for opening default camera i use this code:

 public void defaultpackage() {
        PackageManager packageManager = mContext.getPackageManager();

        List<ApplicationInfo> list = packageManager
                .getInstalledApplications(PackageManager.GET_UNINSTALLED_PACKAGES);
        for (int n = 0; n < list.size(); n++) {
            if ((list.get(n).flags & ApplicationInfo.FLAG_SYSTEM) == 1) {
                if (list.get(n).loadLabel(packageManager).toString()
                        .equalsIgnoreCase("Camera")) {
                    defaultCameraPackage = list.get(n).packageName;
                    Log.e("package", "" + defaultCameraPackage);
                    break;
                }
            }
        }
    }
VC.One
  • 14,790
  • 4
  • 25
  • 57
aarti Ladva
  • 113
  • 9
  • For just open camera why this much code bro. Either from activity or fragment just you fire the intent when your condition are meet like which button are clicked. – Thirumalai May 27 '21 at 06:00
  • @Thirumalai i wanna open only device default camera soo. – aarti Ladva May 27 '21 at 06:05
  • Default front camera will open just if you use val intent : Intent = Intent(MediaStore.ACTION_IMAGE_CAPTURE) startActivity(intent). if you want specific front or back you have pass put extra accordingly – Thirumalai May 27 '21 at 06:08

2 Answers2

1

if you are looking for reliable way: no can do. note that Intent is launching 3rd-party app, which may read and respect Bundle params or may not... depends on device and default camera app set up. thats why your code may not work everywhere. but still you can try with some additional Intent params, like:

  • cameraIntent.putExtra("android.intent.extras.LENS_FACING_FRONT", 0)
  • cameraIntent.putExtra("android.intent.extra.USE_FRONT_CAMERA", false)

yours and two above params may cover most cases/apps, but still you won`t be 100% sure

snachmsm
  • 17,866
  • 3
  • 32
  • 74
  • just like in answer: it may not and you can't do anything with that (well, besides creating own `Activity` handling camera). have you tried to install some other camera app (and set as default) and test if setting back camera with all three `Intent` params works with it? – snachmsm May 27 '21 at 06:06
  • i can only use default camera not other camera apps – aarti Ladva May 27 '21 at 06:09
  • almost every device manufacturer makes own camera app and sets as default, so in fact there is already multiple "default camera" apps on market. still user may always install better equivalent and set it up as default (e.g. [Open Camera](https://play.google.com/store/apps/details?id=net.sourceforge.opencamera) installed 50M+ times). and any of them (default or installed) may not respect your `Intent` params and my experience is saying that this is pretty common... you won't get reliable way to set up camera back always on every device. nuff said, good luck – snachmsm May 27 '21 at 06:28
  • i've added all these extras to my bundle and no success :-( for some reason my app opens last used camera – Kirguduck Jul 20 '23 at 12:30
  • best way is to create own `Activity` handling camera, Camera2 API or CameraX. ofc you need permission for that in manifest. you can't be sure how behaves "default camera app", if you can't believe user that he/she sets up proper facing and took proper foto then you have to deliver own solution – snachmsm Jul 20 '23 at 19:50
0

Try this bro, it is working for me.

  val intent : Intent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
  intent.putExtra("android.intent.extras.CAMERA_FACING", 0)
Thirumalai
  • 176
  • 9
  • very strange. because public static final int LENS_FACING_FRONT = 0; but maybe you meant public static final int LENS_FACING_BACK = 1; – Kirguduck Jul 19 '23 at 13:29