lundi 25 mai 2015

Camera intent doesn't show in intent chooser

String chooseTitle = activity.getString(R.string.select_or_take_picture);

Intent getIntent = new Intent();
getIntent.setType("image/*");
getIntent.setAction(Intent.ACTION_GET_CONTENT);

Intent galleryIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
galleryIntent.setType("image/*");
PackageManager pm = activity.getApplicationContext().getPackageManager();
for (ResolveInfo ri: pm.queryIntentActivities(galleryIntent, PackageManager.MATCH_DEFAULT_ONLY)) {
  Intent intent = pm.getLaunchIntentForPackage(ri.activityInfo.packageName);
  intent.setAction(Intent.ACTION_PICK);
  intents.add(intent);
}

Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE, photoUri);
for (ResolveInfo ri : pm.queryIntentActivities(cameraIntent, 0)) {
  Intent intent = pm.getLaunchIntentForPackage(ri.activityInfo.packageName);
  intents.add(intent);
}

Intent chooserIntent = Intent.createChooser(getIntent, chooseTitle);
chooserIntent.putExtra(
    Intent.EXTRA_INITIAL_INTENTS,
    intents.toArray(new Parcelable[] {})
);

By doing this way, the chooser shows:

Effect when I passed photoUri

The camera intent doesn't show at all.

But if I change the line

Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE, photoUri);

to

Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

It works just fine:

enter image description here

But the problem is, I really want to pass photoUri. How can I do with it?

I know a possible alternative is to write my own chooser dialog, but I do want to know if it's a bug in intent chooser, or if I don't use it correctly.

p.s. @dkarmazi, Here's how I generate Uri:

public Uri generatePhotoUri() {
  String timeStamp = new SimpleDateFormat("yyyyMMdd-HHmmss").format(new Date());
  String imageFileName = "XXX_" + timeStamp + ".jpg";
  File storageDir = Environment.getExternalStoragePublicDirectory(
      Environment.DIRECTORY_PICTURES);
  File imageFile = new File(storageDir, imageFileName);
  return Uri.fromFile(imageFile);
}

Here's my onActivityResult

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  switch (requestCode) {
    case PICK_PHOTO:
      if (resultCode != Activity.RESULT_OK) {
        break;
      }
      Uri source = data == null ?
          mPhotoUri : // take picture
          data.getData(); // choose from other app
      if (source == null) {
        break;
      }
      // TODO: do with source
      break;
  }
  //....
}

@dkarmazi, I've debugged it, and I make sure resultCode is RESULT_CANCELED.

Aucun commentaire:

Enregistrer un commentaire