Skip to content

Instantly share code, notes, and snippets.

@niftynei
Created September 9, 2015 18:41
Show Gist options
  • Save niftynei/963507de5f514ec15e01 to your computer and use it in GitHub Desktop.
Save niftynei/963507de5f514ec15e01 to your computer and use it in GitHub Desktop.
adding a camera thing
/**
* Utility to start an image picker with request code
*
* @param activity Activity that will receive result
* @param requestCode requestCode for result
*/
public static void pickImage(Activity activity, int requestCode, Uri cameraPhotoUri) {
if (activity.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_ANY)) {
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
Intent imageOnDiskIntent = new Intent(Intent.ACTION_GET_CONTENT).setType("image/*");
Intent openInChooser = Intent.createChooser(imageOnDiskIntent, null);
if (cameraPhotoUri != null) {
List<ResolveInfo> resInfo = activity.getPackageManager().queryIntentActivities(cameraIntent, 0);
Intent[] extraIntents = new Intent[resInfo.size()];
for (int i = 0; i < resInfo.size(); i++) {
ResolveInfo resolveInfo = resInfo.get(i);
String packageName = resolveInfo.activityInfo.packageName;
Intent intent = new Intent();
intent.setComponent(new ComponentName(packageName, resolveInfo.activityInfo.name));
intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, cameraPhotoUri);
extraIntents[i] = intent;
}
openInChooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, extraIntents);
}
try {
activity.startActivityForResult(openInChooser, requestCode);
} catch (ActivityNotFoundException e) {
Toast.makeText(activity, R.string.crop__pick_error, Toast.LENGTH_SHORT).show();
}
} else {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT).setType("image/*");
try {
activity.startActivityForResult(intent, requestCode);
} catch (ActivityNotFoundException e) {
Toast.makeText(activity, R.string.crop__pick_error, Toast.LENGTH_SHORT).show();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment