有時候咱們須要的圖片並不適合咱們想要的大小, 那麼咱們就能夠用到系統自帶的圖片裁剪功能, 把規定範圍的圖像給剪出來。javascript
貼上部分代碼:java
-
- Intent intent = new Intent();
- intent.setType("image/*");
- intent.putExtra("crop", "true");
- intent.putExtra("aspectX", 5);
- intent.putExtra("aspectY", 4);
-
- intent.putExtra("output", Uri.fromFile(new File("SDCard/1.jpg")
- intent.putExtra("outputFormat", "JPEG");
- startActivityForResult(Intent.createChooser(intent, "選擇圖片"), 1);
-
- Intent intent = new Intent(
- MediaStore.ACTION_IMAGE_CAPTURE, null);
- intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(
- "SDCard/1.jpg")));
- startActivityForResult(intent, 2);
在調用了以上任意一種方法後, 系統會返回onActivityResult, 咱們在這個方法中處理就能夠了android
-
-
-
- @Override
- protected void onActivityResult(int requestCode, int resultCode, Intent data)
- {
- if (resultCode == 0)
- return;
-
- if (requestCode == 2)
- {
- File picture = new File("SDCard/1.jpg");
- startPhotoZoom(Uri.fromFile(picture));
- } else if (requestCode == PHOTO_CODE)
- {
- try
- {
- BitmapFactory.Options options = new BitmapFactory.Options();
- options.inSampleSize = 2;
- Bitmap bitmap = BitmapFactory.decodeFile("SDCard/1.jpg", options);
-
- if (bitmap != null)
- {
- mCacheBitmap = bitmap;
-
- FileOutputStream fos = null;
- fos = new FileOutputStream("SDCard/1.jpg");
- mCacheBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
- }
-
-
- } catch (Exception e)
- {
-
- }
- }
-
- super.onActivityResult(requestCode, resultCode, data);
- }
-
-
-
-
-
- public void startPhotoZoom(Uri uri) {
- Intent intent = new Intent("com.android.camera.action.CROP");
- intent.setDataAndType(uri, "image/*");
- intent.putExtra("crop", "true");
- intent.putExtra("aspectX", 5);
- intent.putExtra("aspectY", 4);
- intent.putExtra("output", Uri.fromFile(new File("SDCard/1.jpg")));
- intent.putExtra("outputFormat", "JPEG");
- startActivityForResult(intent, PHOTO_CODE);