I am developing an application that should allow the user to select a profile picture, and I want to give them a simple option to either take a picture or select an existing one from the gallery.
My searches led me to https://stackoverflow.com/a/2129/2/2 , so I changed the code in one of the answers for my purposes. Here is what I have:
public static Uri openImageIntent(Activity context) {
Uri outputFileUri = null;
File cache_dir = context.getExternalFilesDir("photos");
cache_dir.mkdirs();
File image_file = null;
try {
image_file = File.createTempFile("profile", ".jpg", cache_dir);
} catch (IOException e) {
e.printStackTrace();
}
if (image_file != null) {
File sdImageMainDirectory = new File(cache_dir.getAbsolutePath(), image_file.getName());
outputFileUri = Uri.fromFile(sdImageMainDirectory);
List<Intent> cameraIntents = new ArrayList<Intent>();
Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
PackageManager packageManager = context.getPackageManager();
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_GET_CONTENT);
final Intent chooserIntent = Intent.createChooser(galleryIntent, "Select Source");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, cameraIntents.toArray(new Parcelable[]{}));
context.startActivityForResult(chooserIntent, Constants.RequestCodes.CHOOSE_PICTURE);
}
return outputFileUri;
}
, , ACTION_IMAGE_CAPTURE ACTION_GET_CONTENT, "image/*". - , "", , "" ( "" " " ), . onActivityResult(), , , .
4.3 . , :

, KitKat, :

, , , "image/*" "" .
, KitKat - , , , - , .
?