Skip to content

Instantly share code, notes, and snippets.

@skylee91
Created October 22, 2015 04:55
Show Gist options
  • Select an option

  • Save skylee91/12a85aff7585fb78aedb to your computer and use it in GitHub Desktop.

Select an option

Save skylee91/12a85aff7585fb78aedb to your computer and use it in GitHub Desktop.
Camare Gallery in single Intent
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent)
{
super.onActivityResult(requestCode, resultCode, intent);
if (resultCode == Utils.CODE_ACTY_SCANQR)
{
String result = intent.getStringExtra("result");
Utils.DisplayToast(this, result);
}
if (requestCode == Utils.CODE_IMAGE_PICKER && resultCode == RESULT_OK)
{
try
{
if (intent != null)
{
//Gallery or FileSystem
InputStream inputStream = getContentResolver().openInputStream(intent.getData());
imgUploaded.setImageBitmap(Utils.DecodeImageStream(inputStream, imgUploaded.getWidth(), imgUploaded.getHeight()));
} else
{
//Camera
//imgReading.setImageBitmap((Bitmap) intent.getExtras().get("data")); //Get thumbnail only
imgUploaded.setImageBitmap(Utils.DecodeImageFile(photoFile, imgUploaded.getWidth(), imgUploaded.getHeight()));
}
}
catch (Exception ex)
{
Utils.DisplayToast(this, ex.toString());
}
}
if (photoFile != null && photoFile.exists()) photoFile.delete();
}
public static void ImagePicker(Context context, File tempFile)
{
Intent intentPick = new Intent();
intentPick.setType("image/*");
intentPick.setAction(Intent.ACTION_GET_CONTENT);
Intent intentCamera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intentCamera.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(tempFile)); //Tell Camera the location to save the captured photo
String pickTitle = context.getString(R.string.UploadPhoto);
if (Utils.isIntentInstalled(context, intentPick) && Utils.isIntentInstalled(context, intentCamera))
{
Intent chooserIntent = Intent.createChooser(intentPick, pickTitle);
chooserIntent.putExtra
(
Intent.EXTRA_INITIAL_INTENTS,
new Intent[]{intentCamera}
);
((Activity) context).startActivityForResult(chooserIntent, CODE_IMAGE_PICKER);
}
else if (Utils.isIntentInstalled(context, intentPick))
{
Intent chooserIntent = Intent.createChooser(intentPick, pickTitle);
((Activity) context).startActivityForResult(chooserIntent, CODE_IMAGE_PICKER);
}
else if (Utils.isIntentInstalled(context, intentCamera))
{
Intent chooserIntent = Intent.createChooser(intentCamera, pickTitle);
((Activity) context).startActivityForResult(chooserIntent, CODE_IMAGE_PICKER);
}
else
{
Utils.DisplayToast(context, "camera and gallery are not installed.");
}
}
public static boolean isIntentInstalled(Context context, Intent intent)
{
return (intent.resolveActivity(context.getPackageManager()) != null);
}
public static void DisplayToast(Context context, String msg)
{
if (toast == null) toast = new Toast(context);
toast.cancel();
toast = Toast.makeText(context, msg, Toast.LENGTH_SHORT);
toast.show();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment