1

If you use whatsapp and you click the attach button, a menu/popup will be inflated showing you a list of appications you can use to select images and even take pictures from your camera using the photo app

whatapp popup

I making an app that have almost the same functionality as whatapp of accessing and taking pictures. the problem is, I can't take pictures using the photo app listed as one of apps in my popup dialog but i can take pictures when using whatsapp. When I click photo app from the dialog shown in my application, it shows me all my pictures. but when I click the photo app from whatsapp popup, it goes directly to the camera

public class ImagePickerActivity extends Activity {

private final int SELECT_PHOTO = 1;
private ImageView imageView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_image_picker);

    imageView = (ImageView)findViewById(R.id.imageView);

    Button pickImage = (Button) findViewById(R.id.btn_pick);
    pickImage.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View view) {                
            Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
            photoPickerIntent.setType("image/*");
            startActivityForResult(photoPickerIntent, SELECT_PHOTO);
        }
    });
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) { 
    super.onActivityResult(requestCode, resultCode, imageReturnedIntent); 

    switch(requestCode) { 
    case SELECT_PHOTO:
        if(resultCode == RESULT_OK){
            try {
                final Uri imageUri = imageReturnedIntent.getData();
                final InputStream imageStream = getContentResolver().openInputStream(imageUri);
                final Bitmap selectedImage = BitmapFactory.decodeStream(imageStream);
                imageView.setImageBitmap(selectedImage);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }

        }
    }
}

In my android manifest

<uses-feature android:name="android.hardware.camera"
              android:required="true" />

What do I need to change in my code to be able to take pictures from photo app?

Edijae Crusar
  • 3,473
  • 3
  • 37
  • 74
  • In whatsup ,can you go to your pictures when you are taking photo? – tiny sunlight Dec 14 '15 at 14:45
  • @tinysunlight in whatapp you **can take photo using photo app or select photo using galley app. for my case am not able to take photo using photo app shown in my popup. i don't know what i need to add in my intent** – Edijae Crusar Dec 14 '15 at 14:48
  • I think you should custom you popup youself. You can compare popup and your popup to see whether them has same style. – tiny sunlight Dec 14 '15 at 14:57
  • @tinysunlight mmm you are right. the issue is, how can i populate my custom dialog with apps that one can use to take photos or access photos and implement onclick listeners on this apps in my dialog? – Edijae Crusar Dec 14 '15 at 15:58
  • Create a dialog with 2 button. Add bind one button to take photo like those answer do and bind the other to open gallery like you did. – tiny sunlight Dec 14 '15 at 16:03
  • you see those are not apps , they are just mime types – karan Dec 15 '15 at 08:53
  • @KaranMer i think the best way i will solve this issue is using my own custom dialog. [please check this out](http://stackoverflow.com/q/34284223/3671509) – Edijae Crusar Dec 15 '15 at 09:02

4 Answers4

2

Below code will provide the showing of Camera and Browse storage options in a native 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.!!

And displaying the same in customized dialog as whatsapp refer the link below click here..

Community
  • 1
  • 1
Swaminathan V
  • 4,663
  • 2
  • 22
  • 32
1

Try to add android.permission.WRITE_EXTERNAL_STORAGE to your manifest.

camera:

Intent takePicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(takePicture, 0);//zero can be replaced with any action code

gallery:

Intent pickPhoto = new Intent(Intent.ACTION_PICK,
       android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(pickPhoto , 1);//one can be replaced with any action code

onactivity result code:

protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) { 
    super.onActivityResult(requestCode, resultCode, imageReturnedIntent); 
    switch(requestCode) {
    case 1:
        if(resultCode == RESULT_OK){  
            Uri selectedImage = imageReturnedIntent.getData();
            imageview.setImageURI(selectedImage);
        }
    break;
    case 0:
        if(resultCode == RESULT_OK){  
            Uri selectedImage = imageReturnedIntent.getData();
            imageview.setImageURI(selectedImage);
        }
    break; 
    }

}

OShiffer
  • 1,366
  • 12
  • 27
  • i want to implement your answer and i think i have to create my own popup which will have all apps that can one can take or access an app. the issue is, how can i populate my dialog with this apps and implement onclick listeners? – Edijae Crusar Dec 14 '15 at 15:56
  • When you start the camera/gallery, your device is automatically offer you the list of apps that can be used. I think that there can be 2 reasons: - your device has one app that can handle the intent. - you have a default app that handles that kind of intent (capture/gallery). – OShiffer Dec 15 '15 at 11:19
0

To access camera and photos using one intent

populate a custom dialog's listview with intents from apps that can either take pictures using camera or access photos in the storage like this

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

Intent captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

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);
lv=(ListView)dialog.findViewById(R.id.listView1);

populateDialogsListView();

dialog.show();
}

Poputate dialog's listview (lv) with apps that can perform the two actions

public void populateDialogListView(){

  PackageManager pm=getPackageManager();

 List<ResolveInfo> photoIntents = new ArrayList<>();

final List<ResolveInfo> listCam = packageManager.queryIntentActivities(
        captureIntent, PackageManager.GET_RESOLVED_FILTER);

final List<ResolveInfo> listGalley = packageManager.queryIntentActivities(photoPickerIntent, PackageManager.GET_RESOLVED_FILTER);

for (ResolveInfo activity : listCam) {
  photoIntents.add(activity);
}

for (ResolveInfo activity : listGalley) {
  photoIntents.add(activity);
}



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

AppAdapter appAdapter = new AppAdapter(pm, photoIntents);

lv.setAdapter(appAdapter);
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);

       IntentFilter filter = launchable.filter;

        int actioncode;
        Intent  intent = new Intent();
        Uri uri;
        if(filter.hasAction(Intent.ACTION_PICK)){
            actioncode = 1;
            uri = android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
            intent.setData(uri);
        }else{
            actioncode = 0;
        }
        intent.setComponent(name);
        startActivityForResult(intent,actioncode);
    }
});

}

Since you are creating a custom dialog with a listview, you have to create adapter for that listview(lv) which will fill the listview with app's name and app's icon

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));
}

Any problem, let me know

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

So to launch them to the native camera application or the camera application the user has selected as their default I believe you would use this.

Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
Intent chooser = Intent.createChooser(intent, title);
// Always use string resources for UI text.
// This says something like "Take a photo with"
String title = getResources().getString(R.string.chooser_title)

// Verify the intent will resolve to at least one activity
if (intent.resolveActivity(getPackageManager()) != null) {
    startActivity(chooser);
}
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
    startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}

Edited it with a sample of what should be required to do so. It may not be simply cut and paste but you should be able to figure it out from that. Sorry, short on time so i cant really elaborate at the moment.

Devsil
  • 598
  • 3
  • 16
  • i would like to show apps that u can use to select **and take pictures** just like the way whatsapp shows galley and photo app in the popup – Edijae Crusar Dec 14 '15 at 14:43