I'm facing a problem with parsing a uri of taken photo to crop activity. In my application, users can take a photo or select one from gallery and then crop it and upload it. Everything sounds straightforward.
When pick from Gallery, Gallery app return an uri for selected photo like this:
content://media/external/images/media/20
I start crop activity with this uri by following code, everything seems ok:
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setData(uri);
intent.putExtra("outputX", 96);
intent.putExtra("outputY", 96);
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("scale", true);
intent.putExtra("return-data", true);
startActivityForResult(intent, REQUEST_CODE_CROP);
But when I take a photo, I can only know the photo's path like this:
file:///mnt/sdcard/iBet88.us/IMAGE_20120517_151606.jpg
and Crop Activity will not accept this uri. I tried another way with Content Provider: add newly captured photo to ContentProvider, and then get new uri in sheme "content://..." from following code:
// TODO insert to Content Provider
ContentResolver cr = getContentResolver();
ContentValues contentValues = new ContentValues();
contentValues.put(MediaStore.Images.Media.DATA, avatarFilePathTmp.getPath());
contentValues.put(MediaStore.Images.Media.IS_PRIVATE, 0);
cr.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues);
// TODO get id from Content Provider
String[] projection = {
MediaStore.Images.Media._ID,
MediaStore.Images.Media.DATA
};
String selectionClause = "" + MediaStore.Images.Media.DATA + " = ?";
String[] selectionArgs = {avatarFilePathTmp.getPath()};
Cursor mCursor = getContentResolver().query(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
projection,
selectionClause,
selectionArgs,
"");
Uri uri = null;
if (null == mCursor) {
} else if (mCursor.getCount() < 1) {
} else {
mCursor.moveToFirst();
int id = mCursor.getInt(mCursor.getColumnIndex(MediaStore.Images.Media._ID));
String u = "content://media/external/images/media/" + id;
// create new Uri
uri = Uri.parse(u);
}
My new Uri is similar to uri from Gallery app, but when I start Crop Activity with new Uri, I get this Exception:
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=com.android.camera.action.CROP dat=content://media/external/images/media/20 (has extras) }
My questions:
1. Why Crop Activity refuses to work with my new Uri, despite my new Uri and uri from Gallery App are the same "content://..."
2. How can I call Crop Activity to crop a photo in sdcard which I know its path only?
I tried to google but still no luck.
Sorry for my bad English. Thank you.
I had intent.setType("image/*");
and it still didn't work...
What solved it for me was using:
intent.setDataAndType(tempImageURI, "image/*");
Good luck!