Last active
April 5, 2019 02:42
-
-
Save TiagoGouvea/4739629db5f55d18cd72 to your computer and use it in GitHub Desktop.
Pick a image from all gallery and camera intents and return it in onActivityResult from caller PickImageActivity class.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_item_add); | |
// ImageButton | |
ImageButton imageButton = (ImageButton) findViewById(R.id.imageButton); | |
imageButton.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
ViewHelper.selectImage(PickImageActivity.this); | |
} | |
}); | |
} | |
@Override | |
protected void onActivityResult(int requestCode, int resultCode, Intent data) { | |
super.onActivityResult(requestCode, resultCode, data); | |
if (resultCode == RESULT_OK) { | |
Uri imageUri = ViewHelper.getPickImageResultUri(this,data); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static void selectImage(final Activity activity){ | |
activity.startActivityForResult(getPickImageChooserIntent(activity), 9162); | |
} | |
public static Intent getPickImageChooserIntent(final Activity activity) { | |
// Determine Uri of camera image to save. | |
Uri outputFileUri = getCaptureImageOutputUri(activity); | |
File file = new File(getRealPathFromURI(activity,outputFileUri)); | |
if (file.exists()) | |
file.delete(); | |
List<Intent> allIntents = new ArrayList<>(); | |
PackageManager packageManager = activity.getPackageManager(); | |
// collect all camera intents | |
Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); | |
List<ResolveInfo> listCam = packageManager.queryIntentActivities(captureIntent, 0); | |
for (ResolveInfo res : listCam) { | |
Intent intent = new Intent(captureIntent); | |
intent.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name)); | |
intent.setPackage(res.activityInfo.packageName); | |
if (outputFileUri != null) { | |
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri); | |
} | |
allIntents.add(intent); | |
} | |
// collect all gallery intents | |
Intent galleryIntent = new Intent(Intent.ACTION_GET_CONTENT); | |
galleryIntent.setType("image/*"); | |
List<ResolveInfo> listGallery = packageManager.queryIntentActivities(galleryIntent, 0); | |
for (ResolveInfo res : listGallery) { | |
Intent intent = new Intent(galleryIntent); | |
intent.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name)); | |
intent.setPackage(res.activityInfo.packageName); | |
allIntents.add(intent); | |
} | |
// the main intent is the last in the list (fucking android) so pickup the useless one | |
Intent mainIntent = allIntents.get(allIntents.size() - 1); | |
for (Intent intent : allIntents) { | |
if (intent.getComponent().getClassName().equals("com.android.documentsui.DocumentsActivity")) { | |
mainIntent = intent; | |
break; | |
} | |
} | |
allIntents.remove(mainIntent); | |
// Create a chooser from the main intent | |
Intent chooserIntent = Intent.createChooser(mainIntent, "Obter imagem"); | |
// Add all other intents | |
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, allIntents.toArray(new Parcelable[allIntents.size()])); | |
return chooserIntent; | |
} | |
/** | |
* Get URI to image received from capture by camera. | |
*/ | |
private static Uri getCaptureImageOutputUri(Activity activity) { | |
Uri outputFileUri = null; | |
File getImage = activity.getExternalCacheDir(); | |
if (getImage != null) { | |
outputFileUri = Uri.fromFile(new File(getImage.getPath(), "ImagePicked.jpeg")); | |
} | |
return outputFileUri; | |
} | |
public static Uri getPickImageResultUri(Activity activity, Intent data) { | |
boolean isCamera = true; | |
if (data != null) { | |
String action = data.getAction(); | |
isCamera = action != null && action.equals(MediaStore.ACTION_IMAGE_CAPTURE); | |
} | |
return isCamera ? getCaptureImageOutputUri(activity) : data.getData(); | |
} | |
public static String getRealPathFromURI(Activity activity,Uri contentUri) { | |
String result; | |
Cursor cursor = activity.getContentResolver().query(contentUri, null, null, null, null); | |
if (cursor == null) { | |
result = contentUri.getPath(); | |
} else { | |
cursor.moveToFirst(); | |
int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA); | |
result = cursor.getString(idx); | |
cursor.close(); | |
} | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment