1

I am using an intent chooser to let users decide what app to use in order to pick an image from camera or gallery. Problem is that my code bellow also show file explorer applications like ES File Manager or the stock file manager.

Screenshot for demonstration : http://image.noelshack.com/fichiers/2015/22/1432565769-xmeedd.png

Also, i dont know which part of my code list those two file explorers.

So is there a way to only keep "Camera" which is the stock camera app, and "Galerie", which is the stock Gallery app ?

Sorry for bad english !

 private void openImageIntent() {

      // Determine Uri of camera image to save.
    final File root = new File(Environment.getExternalStorageDirectory() + File.separator + "TempDirectory" + File.separator);
    root.mkdirs();
    final String fname = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date())+".jpg";
    final File sdImageMainDirectory = new File(root, fname);
    outputFileUri = Uri.fromFile(sdImageMainDirectory);

    // Camera.
    // Is it this part that list the two unwanted file explorers ?
    final List<Intent> cameraIntents = new ArrayList<Intent>();
    final Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    final PackageManager packageManager = getPackageManager();
    final List<ResolveInfo> listCam = packageManager.queryIntentActivities(captureIntent, 0);
    for(ResolveInfo res : listCam) {
        final String packageName = res.activityInfo.packageName;
        final Intent intent = new Intent(captureIntent);
        intent.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
        intent.setPackage(packageName);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
        cameraIntents.add(intent);
    }

    // Filesystem.
    // Is it this part that list the two unwanted file explorers ?
    final Intent galleryIntent = new Intent();
    galleryIntent.setType("image/*");
    galleryIntent.setAction(Intent.ACTION_GET_CONTENT);
    galleryIntent.putExtra(Intent.EXTRA_LOCAL_ONLY, true);

    // Chooser of filesystem options.
    final Intent chooserIntent = Intent.createChooser(galleryIntent, "Select Source");

    // Add the camera options.
    chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, cameraIntents.toArray(new Parcelable[cameraIntents.size()]));

    startActivityForResult(chooserIntent, CAMERA_PIC_REQUEST);
}
Nuzzob
  • 371
  • 1
  • 4
  • 23
  • "Problem is that my code bellow also show file explorer applications like ES File Manager or the stock file manager" -- so? Perhaps the user wants to use those apps, or other apps. "So is there a way to only keep "Camera" which is the stock camera app, and "Galerie", which is the stock Gallery app ?" -- there is no single stock camera app, and there is no single stock gallery app. There are ~1.5 billion Android devices, spanning thousands of models, each of which can have their own stock apps. Plus, users may wish to use third-party camera, gallery, or other apps. – CommonsWare May 25 '15 at 14:58
  • http://stackoverflow.com/a/8550043/4069913 – Orest Savchak May 25 '15 at 14:58
  • @CommonsWare Yes, I know that's not a user-friendly solution, but I want to ensure that the chosen file is an image. Which is not possible to do by using third-party file chooser like ES which allow to choose any kind of files. In this case i will have to verify the file type in the onActivityResult method, is this a good way to achieve it ? – Nuzzob May 25 '15 at 15:28
  • "but I want to ensure that the chosen file is an image" -- there is no requirement for any app, including "stock camera app" and "stock gallery app" to always return an image. Those apps are written by programmers, who are welcome to do whatever they want. "In this case i will have to verify the file type in the onActivityResult method, is this a good way to achieve it ?" -- yes (e.g., use `BitmapFactory`, `BitmapFactory.Options`, and `inJustDecodeBounds` to see if the result decodes as a bitmap, or call `getType()` on `ContentResolver` to just check the MIME type, if that is suffficient). – CommonsWare May 25 '15 at 15:31
  • @CommonsWare Thank you for your help and your advices (and your book), I really appreciate it. – Nuzzob May 25 '15 at 15:34

0 Answers0