Android圖片裁剪解決方案 -- 從相簿截圖

來源:互聯網
上載者:User

標籤:

在Android開發中,可以輕鬆調用一個Intent完成從相簿中的工作:

Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);intent.setType("image/*");intent.putExtra("crop", "true");

附加選項如下:

選項 資料類型 描述
crop String 發送裁剪訊號
aspectX int X方向上的比例
aspectY int Y方向上的比例
outputX int 裁剪區的寬
outputY int 裁剪區的高
scale boolean 是否保留比例
return-data boolean 是否將資料保留在Bitmap中返回
data Parcelable 相應的Bitmap資料
circleCrop String 圓形裁剪地區?
MediaStore.EXTRA_OUTPUT ("output") URI 將URI指向相應的 file:///...

 

這個return-data是比較令人困惑的。

有兩種方法接收資料:

方法一:把return-data設為true,讓資料直接通過Intent返回到onActivityResult中,如下:

public void SelectSmallImg(View view){        Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);        intent.setType("image/*");        intent.putExtra("crop", "true");        intent.putExtra("aspectX", 1);        intent.putExtra("aspectY", 1);        intent.putExtra("outputX", 800);        intent.putExtra("outputY", 800);        intent.putExtra("scale", true);        intent.putExtra("return-data", true);        intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());        intent.putExtra("noFaceDetection", true); // no face detection        startActivityForResult(intent, CHOOSE_SMALL_PICTURE);    }

方法二:return-data設為false,利用MediaStore.EXTRA_OUTPUT標籤,把資料存放區在外部的臨時URI,再由onActivityResult讀取。當然了,要事先準備好一個指向檔案的URI,如果有sdcard則問題不大,沒有的話會怎樣?????

public void SelectLargeImg(View view){        // this is for android 4.4 ??        //Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);        Intent intent = new Intent(Intent.ACTION_GET_CONTENT,null);        intent.setType("image/*");        intent.putExtra("crop", true);        intent.putExtra("aspectX", 1);        intent.putExtra("aspectY", 1);        //intent.putExtra("outputX", 400);  //返回資料的時候的 X 像素大小。        //intent.putExtra("outputY", 400);  //返回的時候 Y 的像素大小。        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);        startActivityForResult(intent,CHOOSE_BIG_PICTURE);    }

在onActivityResult裡可以這樣對付:

    @Override    public void onActivityResult(int requestCode, int resultCode, Intent resultData){        switch(requestCode){            case CHOOSE_BIG_PICTURE:                if(resultCode == Activity.RESULT_OK && resultData != null){                    Log.i(DEBUG_FLAG,imageUri.toString());                    Bitmap bitmap = decodeUriAsBitmap(imageUri);//decode bitmap (自己寫一個把URI轉換為bitmap的函數)                    if(bitmap == null){                        Log.i(DEBUG_FLAG,"BIG_PICTURE NULL");                    }else{                        imageView.setImageBitmap(bitmap);                        Log.i(DEBUG_FLAG , "Size:"+bitmap.getWidth()+" X "+bitmap.getHeight());                    }                                    }                break;            case CHOOSE_SMALL_PICTURE:                if(resultCode == Activity.RESULT_OK && resultData != null){                    Bitmap bitmap = resultData.getParcelableExtra("data");                    if(bitmap == null){                        Log.i(DEBUG_FLAG,"SMALL_PICTURE NULL");                    }else{                        imageView.setImageBitmap(bitmap);                        Log.i(DEBUG_FLAG , "Size:"+bitmap.getWidth()+" X "+bitmap.getHeight());                    }                }                break;            default :                break;        }    }

自己寫一個把URI轉換為bitmap的函數:

Bitmap decodeUriAsBitmap(Uri uri){            Bitmap bitmap = null;            try {                bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(uri));            } catch (FileNotFoundException e) {                e.printStackTrace();                return null;            }            return bitmap;    }

============================

總結一下:基於記憶體的考慮,在圖片較大時,android的功能會返回一個160*160的縮圖,這樣就只能通過外部URI來接收資料,如果是小圖,直接用Intent傳遞資料是沒問題的。

 

 

 

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.