This code used to work on previous versions of Android, because the data from the intent in onActivityResult, would return null if the image came from the camera, and it would hold a Uri if the image was selected from the file system. However, on Lollipop, the camera intent is also returning a Uri. How do I know whether the image was taken with the camera or was selected from the file storage/gallery?
Here is my code:
private void openImageIntent() {
// Determine Uri of camera image to save.
final File root = new File(Environment.getExternalStorageDirectory()
+ File.separator + "Klea" + File.separator);
root.mkdirs();
final String fname = getUniqueImageFilename();
final File sdImageMainDirectory = new File(root, fname);
outputFileUri = Uri.fromFile(sdImageMainDirectory);
// Camera.
final List<Intent> cameraIntents = new ArrayList<Intent>();
final Intent captureIntent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
final PackageManager packageManager = getActivity().getPackageManager();
final 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);
}
// Filesystem.
final Intent galleryIntent = new Intent();
galleryIntent.setType("image/*");
galleryIntent.setAction(Intent.ACTION_GET_CONTENT);
// Chooser of filesystem options.
final Intent chooserIntent = Intent.createChooser(galleryIntent,
getString(R.string.chose_source));
// Add the camera options.
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS,
cameraIntents.toArray(new Parcelable[] {}));
startActivityForResult(chooserIntent, REQUEST_IMAGE_CAPTURE);
}
private static String getUniqueImageFilename() {
// TODO Auto-generated method stub
String fileName = "img_" + System.currentTimeMillis() + ".jpg";
return fileName;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.d("reqCode", Integer.toString(requestCode));
Log.d("resCode", Integer.toString(resultCode));
Log.d("data", data.toString());
getActivity();
if (resultCode == Activity.RESULT_OK) {
if (requestCode == REQUEST_IMAGE_CAPTURE) {
final boolean isCamera;
if (data == null) {
isCamera = true;
} else {
final String action = data.getAction();
if (action == null) {
isCamera = false;
} else {
isCamera = action
.equals(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
}
}
Uri selectedImageUri;
if (isCamera) {
File file = new File(outputFileUri.toString());
Log.d("old file", file.toString());
String temp = file.toString().replace("file:", "");
Log.d("new file", temp);
selectedImageUri = Uri.parse(temp);
} else {
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getActivity().getContentResolver().query(
selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
selectedImageUri = Uri.parse(cursor.getString(columnIndex));
cursor.close();
}
}
}
}