Selection to set up the camera and select an image

I created a selector to select an image from a file or to create an image.

The code I use works fine on Nexus 5, however, when I try to use it on the Samsung S5, the selector does not display camera icons.

public Intent makePhotoIntent(String title, Context ctx, String uniqueImageId){ //Build galleryIntent Intent galleryIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); galleryIntent.setType("image/*"); //Create chooser Intent chooser = Intent.createChooser(galleryIntent,title); if (checkexCameraHardware()){ Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); mTempImage = null; try { mTempImage = createImageFile(uniqueImageId); } catch (IOException e) { e.printStackTrace(); } if (mTempImage != null){ cameraIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, Uri.fromFile(mTempImage)); //add file ure (photo is saved here) Intent[] extraIntents = {cameraIntent}; chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, extraIntents); } } return chooser; } 

SamsungNexus

When I change the order in which intentions are added to favorites, the Samsung device shows the camera, but only shows the android system as a file option.

 public Intent makePhotoIntent(String title, Context ctx, String uniqueImageId){ //Build galleryIntent Intent galleryIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); galleryIntent.setType("image/*"); //Create chooser Intent chooser = Intent.createChooser(galleryIntent,title); if (checkexCameraHardware()){ Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); mTempImage = null; try { mTempImage = createImageFile(uniqueImageId); } catch (IOException e) { e.printStackTrace(); } if (mTempImage != null){ cameraIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, Uri.fromFile(mTempImage)); //add file ure (photo is saved here) //I have to re-create the chooser here or the Samsung will not show the 'camera' icons. //I have to add the cameraIntent first. chooser = Intent.createChooser(cameraIntent,title); Intent[] extraIntents = {galleryIntent}; //Intent[] extraIntents = {cameraIntent}; chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, extraIntents); } } return chooser; } 

SamsungNexus

Ideally, I would like the Samsung device to show the same thing as the Nexus when I first add a gallery target. However, I cannot get this to work.

+7
android android-intent samsung-touchwiz camera
source share
3 answers
  • If for an additional intent ( Intent.EXTRA_INITIAL_INTENTS ) there are several matches for the intent, they will display all of them in the "Android system" section. When you click "Android system", it will open another choice with all these matches.

  • So, in your first screenshot for the samsung device, the camera really appears - itโ€™s just under โ€œAndroid Systeemโ€ (clicking on it displays all the correspondences of the cameraโ€™s intentions).

  • If you make galleryIntent incremental instead of camera intent, it will combine all gallery-related intentions in the Android Systeem section (as shown in the second screenshot for the samsung device).

I think the user can be confused if he does not know what Android Systeem is!

  • Using the solution below you can directly add them to your chooser

    In your second code snippet, replace

     Intent[] extraIntents = {galleryIntent}; chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, extraIntents); 

    from:

     List<Intent> galleryIntents = new ArrayList<Intent>(); PackageManager pm = getApplicationContext().getPackageManager(); for (ResolveInfo ri: pm.queryIntentActivities(galleryIntent, PackageManager.MATCH_DEFAULT_ONLY)) { Intent intent = pm.getLaunchIntentForPackage(ri.activityInfo.packageName); intent.setAction(Intent.ACTION_PICK); galleryIntents.add(intent); } chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, galleryIntents.toArray(new Parcelable[] {})); 

This will manually add each relevant intention directly to your chooser

+8
source share

Try this way, hope it helps you solve your problem

Please follow these steps and this is the best and easiest way to select an image from the gallery and from the camera,

Step 1. Declaring global variables

 public static final int rcCC = 33; boolean isCC = false; 

Step 2. Copy these methods and paste the code

 void picPhoto() { String str[] = new String[] { "Camera", "Gallery" }; new AlertDialog.Builder(getActivity()).setItems(str, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { performImgPicAction(which); } }).show(); } void performImgPicAction(int which) { Intent in; if (which == 1) { in = new Intent( Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); } else { in = new Intent(); in.setAction(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); } startActivityForResult( Intent.createChooser(in, "Select profile picture"), which); } @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); try { if (rcCC == requestCode) { getActivity(); if (resultCode == Activity.RESULT_OK) { isCC = true; } } else { getActivity(); if (resultCode == Activity.RESULT_OK) { BitmapFactory.Options option = new BitmapFactory.Options(); option.inDither = false; option.inPurgeable = true; option.inInputShareable = true; option.inTempStorage = new byte[32 * 1024]; option.inPreferredConfig = Bitmap.Config.RGB_565; if (Build.VERSION.SDK_INT < 19) { Uri selectedImageURI = data.getData(); uImage = BitmapFactory.decodeFile(Common.getPath( selectedImageURI, getActivity()), option); } else { ParcelFileDescriptor pfd; try { pfd = context .getContentResolver() .openFileDescriptor(data.getData(), "r"); FileDescriptor fileDescriptor = pfd .getFileDescriptor(); uImage = BitmapFactory.decodeFileDescriptor( fileDescriptor, null, option); pfd.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } try { ivProfile_image.setImageBitmap(uImage); } catch (Exception e) { e.printStackTrace(); } } } } catch (Exception e) { e.printStackTrace(); } } 

Step 3. Call the method in which you want to select pic and show the dialog

  picPhoto(); 

Step 4

Make the user permission in the manifest file,

  <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.CAMERA" /> 

thanks

+2
source share

Try this way, I hope it works for you, I check the galaxy galaxy 2 tab that it shows,

Declare your intention as follows

  Intent galleryIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); galleryIntent.setType("image/*"); //Create chooser Intent chooser = Intent.createChooser(galleryIntent,title); Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); fileUri = getOutputMediaFileUri(uniqueImageId); cameraIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, fileUri); //add file ure (photo is saved here) Intent[] extraIntents = {cameraIntent}; chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, extraIntents); startActivityForResult(chooser, Constants.SEND_IMAGE_FROM_CEMERA); 

and getOutputMediaFileUri (uniqueImageId), this returns you a Uri image

 getOutputMediaFileUri(String uniqueImageId){ File mediaStorageDir = new File( Environment .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "IMAGE_DIRECTORY_NAME"); // Create the storage directory if it does not exist if (!mediaStorageDir.exists()) { if (!mediaStorageDir.mkdirs()) { return null; } } File mediaFile; mediaFile = new File(mediaStorageDir.getPath() + File.separator + "IMG_" + uniqueImageId + ".jpg"); return Uri.fromFile(mediaFile); } 
+1
source share

All Articles