Android大圖片裁剪終極解決方案

來源:互聯網
上載者:User

標籤:

根據我們的分析與總結,圖片的來源有拍照和相簿,而可採取的操作有

  • 使用Bitmap並返回資料
  • 使用Uri不返回資料

    前面我們瞭解到,使用Bitmap有可能會導致圖片過大,而不能返回實際大小的圖片,我將採用大圖Uri,小圖Bitmap的資料存放區方式。

    我們將要使用到URI來儲存拍照後的圖片:

?
12 private static final String IMAGE_FILE_LOCATION = "file:///sdcard/temp.jpg";//temp fileUri imageUri = Uri.parse(IMAGE_FILE_LOCATION);//The Uri to store the big bitmap

    不難知道,我們從相簿選取圖片的Action為Intent.ACTION_GET_CONTENT。

    根據我們上一篇部落格的分析,我準備好了兩個執行個體的Intent。

    一、從相簿截大圖:

?
12345678910111213 Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);intent.setType("image/*");intent.putExtra("crop", "true");intent.putExtra("aspectX", 2);intent.putExtra("aspectY", 1);intent.putExtra("outputX", 600);intent.putExtra("outputY", 300);intent.putExtra("scale", true);intent.putExtra("return-data", false);intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());intent.putExtra("noFaceDetection", true); // no face detectionstartActivityForResult(intent, CHOOSE_BIG_PICTURE);

    二、從相簿截小圖

?
123456789101112 Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);intent.setType("image/*");intent.putExtra("crop", "true");intent.putExtra("aspectX", 2);intent.putExtra("aspectY", 1);intent.putExtra("outputX", 200);intent.putExtra("outputY", 100);intent.putExtra("scale", true);intent.putExtra("return-data", true);intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());intent.putExtra("noFaceDetection", true); // no face detectionstartActivityForResult(intent, CHOOSE_SMALL_PICTURE);

    三、對應的onActivityResult可以這樣處理返回的資料

?
12345678910111213141516171819 switch (requestCode) {case CHOOSE_BIG_PICTURE:    Log.d(TAG, "CHOOSE_BIG_PICTURE: data = " + data);//it seems to be null    if(imageUri != null){        Bitmap bitmap = decodeUriAsBitmap(imageUri);//decode bitmap        imageView.setImageBitmap(bitmap);    }    break;case CHOOSE_SMALL_PICTURE:    if(data != null){        Bitmap bitmap = data.getParcelableExtra("data");        imageView.setImageBitmap(bitmap);    }else{        Log.e(TAG, "CHOOSE_SMALL_PICTURE: data = " + data);    }    break;default:    break;}
?
12345678910 private Bitmap decodeUriAsBitmap(Uri uri){    Bitmap bitmap = null;    try {        bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(uri));    } catch (FileNotFoundException e) {        e.printStackTrace();        return null;    }    return bitmap;}

    :

大圖 小圖

轉載(http://my.oschina.net/ryanhoo/blog/86853)

Android大圖片裁剪終極解決方案

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.