1

I am trying to create a dialog that shows all applications in a user phone that can be used to select a picture from the storage or take one using the camera.

This is a follow-up to my previous question.

The best way I have found to populate my listview in my customized dialog with applications that can perform the above actions is to use queryIntentActivityOptions() method but it's not working. my listview isn't been populated with apps that can be used to access an image or take one using the camera.

private void acquirePicture(){
    Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
    photoPickerIntent.setType("image/*");

    Intent takePicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(photoPickerIntent, 1);

    final Dialog dialog = new Dialog(this);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    WindowManager.LayoutParams WMLP = dialog.getWindow().getAttributes();
    WMLP.gravity = Gravity.CENTER;
    dialog.getWindow().setAttributes(WMLP);
    dialog.getWindow().setBackgroundDrawable(
            new ColorDrawable(android.graphics.Color.TRANSPARENT));
    dialog.setCanceledOnTouchOutside(true);
    dialog.setContentView(R.layout.about_dialog);
    dialog.setCancelable(true);
    ListView lv=(ListView)dialog.findViewById(R.id.listView1);
    PackageManager pm=getPackageManager();

    List<ResolveInfo> launchables=pm.queryIntentActivityOptions(
this.getComponentName(),new Intent[]{takePicture},
photoPickerIntent,0);

    Collections.sort(launchables,
            new ResolveInfo.DisplayNameComparator(pm));

    appAdapter=new AppAdapter(pm, launchables);

    lv.setAdapter(adapter);
    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                                long arg3) {
            // TODO Auto-generated method stub
            ResolveInfo launchable=appAdapter.getItem(position);
            ActivityInfo activity=launchable.activityInfo;
            ComponentName name=new ComponentName(activity.applicationInfo.packageName,
                    activity.name);
            //I DON'T KNOW WHAT TO DO NEXT OR WHETHER AM DOING IT
             THE CORRECT WAY
        }
    });

    dialog.show();
}

class AppAdapter extends ArrayAdapter<ResolveInfo> {
    private PackageManager pm=null;

    AppAdapter(PackageManager pm, List<ResolveInfo> apps) {
        super(Custom_chooser.this, R.layout.row, apps);
        this.pm=pm;
    }

    @Override
    public View getView(int position, View convertView,
                        ViewGroup parent) {
        if (convertView==null) {
            convertView=newView(parent);
        }

        bindView(position, convertView);

        return(convertView);
    }

    private View newView(ViewGroup parent) {
        return(getLayoutInflater().inflate(R.layout.row, parent, false));
    }

    private void bindView(int position, View row) {
        TextView label=(TextView)row.findViewById(R.id.label);

        label.setText(getItem(position).loadLabel(pm));

        ImageView icon=(ImageView)row.findViewById(R.id.icon);

        icon.setImageDrawable(getItem(position).loadIcon(pm));
    }
}

RESULT(empty dialog)

empty dialog

EDIT

 List<ResolveInfo> launchables=pm.queryIntentActivityOptions(
this.getComponentName(),new Intent[]{takePicture},
photoPickerIntent,0);
Community
  • 1
  • 1
Edijae Crusar
  • 3,473
  • 3
  • 37
  • 74
  • wondered, why you are using the custom dialog to show installed apps. It is default that they will all show in a dialog. – Swaminathan V Dec 15 '15 at 10:55
  • @RaguSwaminathan how then can i show a list of apps that can perform **the two actions in the same dialog ? i havent seen an intent that can accomodiate two actions in it's parameters** – Edijae Crusar Dec 15 '15 at 10:57
  • what are all the actions required .? 1. want to open camera or 2. pick from the local storage correct. – Swaminathan V Dec 15 '15 at 10:59
  • @RaguSwaminathan am creating an app where user with click of a button, can provide an image either from the storage or take one using camera – Edijae Crusar Dec 15 '15 at 11:05

2 Answers2

2

Hope if i am correct you are looking for something like below, refer screenshot. Opening Camera and Browse Files options in the single dialog.

 // Picks Camera first.
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);
}

final Intent galleryIntent = new Intent();
galleryIntent.setType("image/*");
galleryIntent.setAction(Intent.ACTION_PICK);

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

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

startActivityForResult(chooserIntent, TAKE_PHOTO_CODE);

This will help you.!!

Screenshot from lollipop and jelly bean[![][1]]

2

Tutorial for how to work with camera

Tutorial for how to browse and select file from the storage.

Selecting image from internal storage

Swaminathan V
  • 4,663
  • 2
  • 22
  • 32
  • brother, this is it !! :). however, because i know my boss will need a customized dialog, with your example, i was able to correct my problem. the issue was **i was passing the wrong adapter to my listview**. after passing the correct adapter, my custom dialog was populated. **however** i still have a question. have you ever used `packageManager.queryIntentActivities`? – Edijae Crusar Dec 15 '15 at 12:20
  • i have never used it and am douting my implementation. it's working. **but i haven't understood 2nd and 3rd parameters my implementation is guesswork** [CLICK HERE](http://developer.android.com/intl/zh-cn/reference/android/content/pm/PackageManager.html#queryIntentActivityOptions%28android.content.ComponentName,%20android.content.Intent[],%20android.content.Intent,%20int%29) – Edijae Crusar Dec 15 '15 at 12:30
  • please if possible tell me if i have implemented `packageManager.queryIntentActivities` the correct way – Edijae Crusar Dec 15 '15 at 12:32
  • i have edited my question. please see my edit which shows my implementation of `packageManager.queryIntentActivities` – Edijae Crusar Dec 15 '15 at 12:43
  • added some reference links to work further with what you have...!! – Swaminathan V Dec 15 '15 at 13:09
  • great tutorial. i highly appreciate your help my brother. – Edijae Crusar Dec 15 '15 at 14:01
  • Men, i don't know who downvoted your good answer and links. i tried to upvote it again but i cant since i had already upvoted it before – Edijae Crusar Feb 01 '16 at 05:14
  • cool no problem :) ..!! may be my answer was not be good enough to understand..!! @gikarasojokinene – Swaminathan V Feb 01 '16 at 05:26
  • yah to the one who downvoted. but to me everything was crystal clear. lets hope things will get better. sometimes i also find myself in same situation but hope for the best. – Edijae Crusar Feb 01 '16 at 05:35
0

You are not passing the correct adapter in your listview

Your code

 List<ResolveInfo> launchables=pm.queryIntentActivityOptions(
 this.getComponentName(),new Intent[]{takePicture},
 photoPickerIntent,0);

Collections.sort(launchables,
        new ResolveInfo.DisplayNameComparator(pm));

appAdapter=new AppAdapter(pm, launchables);

lv.setAdapter(adapter);

Correct way

List<ResolveInfo> launchables=pm.queryIntentActivityOptions(
 this.getComponentName(),new Intent[]{takePicture},
 photoPickerIntent,0);

Collections.sort(launchables,
        new ResolveInfo.DisplayNameComparator(pm));

appAdapter=new AppAdapter(pm, launchables);

lv.setAdapter(appAdapter);

Note the difference in

lv.setAdapter(appAdapter);

Result

after correcting error

Edijae Crusar
  • 3,473
  • 3
  • 37
  • 74