Created
March 27, 2015 06:54
-
-
Save john990/739fa230bd309554c244 to your computer and use it in GitHub Desktop.
android select image from gallery or camera, and crop
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
private String cameraFileName; | |
@Override | |
public void choiceAvatarFromCamera() { | |
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); | |
cameraFileName = Constants.DOWNLOAD_IMAGE_PATH + System.currentTimeMillis(); | |
File file = new File(Constants.DOWNLOAD_IMAGE_PATH); | |
if(!file.exists()){ | |
file.mkdirs(); | |
} | |
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(cameraFileName))); | |
intent.putExtra("return-data", true); | |
startActivityForResult(intent, CHOICE_AVATAR_FROM_CAMERA_CROP); | |
} | |
@Override | |
public void choiceAvatarFromGallery() { | |
Intent intent = new Intent(Intent.ACTION_GET_CONTENT); | |
intent.addCategory(Intent.CATEGORY_OPENABLE); | |
intent.setType("image/*"); | |
startActivityForResult(getCropIntent(intent), CHOICE_AVATAR_FROM_GALLERY); | |
} | |
private Intent getCropIntent(Intent intent) { | |
intent.putExtra("crop", "true"); | |
intent.putExtra("aspectX", 1); | |
intent.putExtra("aspectY", 1); | |
intent.putExtra("outputX", 320); | |
intent.putExtra("outputY", 320); | |
intent.putExtra("return-data", true); | |
return intent; | |
} | |
@Override | |
public void onActivityResult(int requestCode, int resultCode, Intent data) { | |
if (resultCode == Activity.RESULT_OK) { | |
if (requestCode == CHOICE_AVATAR_FROM_CAMERA || requestCode == CHOICE_AVATAR_FROM_GALLERY) { | |
ToastUtils.toastType0(mActivity, "CHOICE_AVATAR_FROM_CAMERA", Toast.LENGTH_SHORT); | |
Bitmap avatar = getBitmapFromData(data); | |
// this bitmap is the finish image | |
} else if (requestCode == CHOICE_AVATAR_FROM_CAMERA_CROP) { | |
Intent intent = new Intent("com.android.camera.action.CROP"); | |
Uri uri = Uri.fromFile(new File(cameraFileName)); | |
intent.setDataAndType(uri, "image/*"); | |
startActivityForResult(getCropIntent(intent), CHOICE_AVATAR_FROM_CAMERA); | |
} | |
} | |
super.onActivityResult(requestCode, resultCode, data); | |
} | |
/** | |
* Use for decoding camera response data. | |
* | |
* @param data | |
* @return | |
*/ | |
public static Bitmap getBitmapFromData(Intent data) { | |
Bitmap photo = null; | |
Uri photoUri = data.getData(); | |
if (photoUri != null) { | |
photo = BitmapFactory.decodeFile(photoUri.getPath()); | |
} | |
if (photo == null) { | |
Bundle extra = data.getExtras(); | |
if (extra != null) { | |
photo = (Bitmap) extra.get("data"); | |
ByteArrayOutputStream stream = new ByteArrayOutputStream(); | |
photo.compress(Bitmap.CompressFormat.JPEG, 100, stream); | |
} | |
} | |
return photo; | |
} |
@koustuvsinha
You can use "Environment.getExternalStorageDirectory()"
Please take a look: https://github.com/jrvansuita/PickImage
No cropping facility for select image from gallery
case
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
what should be a sample value of
Constants.DOWNLOAD_IMAGE_PATH
?