【Android開發經驗】設定帳戶圖片並裁剪,僅僅是這麼簡單?

來源:互聯網
上載者:User

標籤:裁剪

    轉載請註明出處:http://blog.csdn.net/zhaokaiqiang1992

    在做APP的時候,如果有使用者系統功能,那麼一般都逃不了這個需求,就是給使用者佈建頭像,而設定頭像,又包括從拍照和從相簿選取兩個方式,而且選擇了之後,一般又都會要求對映像進行裁剪,讓使用者佈建頭像。今天這篇文章就是介紹如何完成這個需求的。


    我們首先分析一下需求。關於拍照和從相簿選取,都可以向系統發送特定的Intent,喚起對應的系統程式,然後在onActivityResult裡面,擷取我們的資料即可。關於映像裁剪,有兩種方式,一種是自己處理,比如利用第三方的開源項目,如Cropper(https://github.com/edmodo/cropper),來完成我們的需求,另外一種,我們可以直接利用系統提供的裁剪功能,實現映像的裁剪。


    在代碼實現之前,我們先理理思路。如果是從相簿擷取的照片,在onActivityResult裡面,我們可以擷取到兩種形式的資料,一種是Bitmap,一種是uri。如果Bitmap對象太大的話,可能就直接把我們的程式搞崩了,所以如果相簿中的圖片都是300px以下的圖片,使用bitmap的方式是允許的,也是安全的,但是這在我們的手機裡面也是基本不可能的。所以,我推薦無論大小都直接使用uri方式,因為擷取到uri之後,就相當於拿到了圖片的指標,想幹嘛就幹嘛~


    對於直接在相簿擷取圖片來說,並不會出現太多的問題,但是如果你想使用拍照的圖片進行處理的話,可能就麻煩一些。使用Intent調用起拍照APP之後,我們在onActivityResult裡面也可以擷取到bitmap或者是uri,這取決於我們在intent中設定了什麼標誌。但是,如果你直接擷取拍照返回的Bitmap的話,可能並不是你想得到的結果,有可能返回的不是原圖,而是模糊的縮圖,這是Android系統為了減少記憶體使用量所做的策略,但是我們拿著這張縮圖是沒法直接用的,所以,從拍照擷取圖片的時候,我們也應該使用uri的方式。


    好了,無論使用哪種方式,我們都擷取到了所要處理的圖片的uri,那麼之後呢?當前是把這個uri作為資料使用Intent發送到進行裁剪的Activity裡面,然後裁剪完成之後,在onActivityResult裡面,把裁剪之後的bitmap對象設定給ImageView,然後儲存起來,上傳到伺服器即可。


    瞭解了整個流程之後,我們就可以開始寫代碼了。

    如果想喚起相簿,有兩種方式,一種是Intent.ACTION_PICK,還有一種是Intent.ACTION_GET_CONTENT,代碼如下,這兩種方式都可以擷取到圖片的uri資料

//方式1,直接開啟圖庫,只能選擇圖庫的圖片Intent i = new Intent(Intent.ACTION_PICK,MediaStore.Images.Media.EXTERNAL_CONTENT_URI);//方式2,會先讓使用者選擇接收到該請求的APP,可以從檔案系統直接選取圖片Intent intent = new Intent(Intent.ACTION_GET_CONTENT,MediaStore.Images.Media.EXTERNAL_CONTENT_URI);intent.setType("image/*");startActivityForResult(intent, PICK_FROM_FILE);

    如果我們想從拍照擷取,那麼我們就可以使用下面的方式,先用一個file對象建立一個uri,然後幫頂起來,這樣拍照成功,返回之後,我們就可以根據uri擷取到照片了,而且在file的路徑儲存了拍照的結果,下面是代碼實現

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);imgUri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(), "avatar_"+ String.valueOf(System.currentTimeMillis())+ ".png"));intent.putExtra(MediaStore.EXTRA_OUTPUT, imgUri);startActivityForResult(intent, PICK_FROM_CAMERA);

    ok,擷取到uri之後,我們應該怎麼辦呢?當前是再開啟裁剪介面啦~

    開啟裁剪介面的Intent是Intent intent = new Intent("com.android.camera.action.CROP");

    intent中得這些參數含義請參考

附加選項資料類型描述cropString發送裁剪訊號aspectXintX方向上的比例aspectYintY方向上的比例outputXint裁剪區的寬outputYint裁剪區的高scaleboolean是否保留比例return-databoolean是否將資料保留在Bitmap中返回dataParcelable相應的Bitmap資料circleCropString圓形裁剪地區?MediaStore.EXTRA_OUTPUT ("output")URI將URI指向相應的file:///...,詳見程式碼範例

    比較重要的是MediaStore.EXTRA_OUTPUT和return-data這兩個選項。
    如果你將return-data設定為“true”,你將會獲得一個與內部資料關聯的Action,並且bitmap以此方式返回:(Bitmap)extras.getParcelable("data")。注意:如果你最終要擷取的圖片非常大,那麼此方法會給你帶來麻煩,所以你要控制outputX和outputY保持在較小的尺寸。 

    如果你將return-data設定為“false”,那麼在onActivityResult的Intent資料中你將不會接收到任何Bitmap,相反,你需要將MediaStore.EXTRA_OUTPUT關聯到一個Uri,此Uri是用來存放Bitmap的。 

    瞭解了這些之後,我們再看下面的代碼應該就很清楚了。

private void doCrop() {final ArrayList<CropOption> cropOptions = new ArrayList<CropOption>();Intent intent = new Intent("com.android.camera.action.CROP");intent.setType("image/*");List<ResolveInfo> list = getPackageManager().queryIntentActivities(intent, 0);int size = list.size();if (size == 0) {Toast.makeText(this, "can‘t find crop app", Toast.LENGTH_SHORT).show();return;} else {intent.setData(imgUri);intent.putExtra("outputX", 300);intent.putExtra("outputY", 300);intent.putExtra("aspectX", 1);intent.putExtra("aspectY", 1);intent.putExtra("scale", true);intent.putExtra("return-data", true);// only oneif (size == 1) {Intent i = new Intent(intent);ResolveInfo res = list.get(0);i.setComponent(new ComponentName(res.activityInfo.packageName,res.activityInfo.name));startActivityForResult(i, CROP_FROM_CAMERA);} else {// many crop appfor (ResolveInfo res : list) {final CropOption co = new CropOption();co.title = getPackageManager().getApplicationLabel(res.activityInfo.applicationInfo);co.icon = getPackageManager().getApplicationIcon(res.activityInfo.applicationInfo);co.appIntent = new Intent(intent);co.appIntent.setComponent(new ComponentName(res.activityInfo.packageName,res.activityInfo.name));cropOptions.add(co);}CropOptionAdapter adapter = new CropOptionAdapter(getApplicationContext(), cropOptions);AlertDialog.Builder builder = new AlertDialog.Builder(this);builder.setTitle("choose a app");builder.setAdapter(adapter,new DialogInterface.OnClickListener() {public void onClick(DialogInterface dialog, int item) {startActivityForResult(cropOptions.get(item).appIntent,CROP_FROM_CAMERA);}});builder.setOnCancelListener(new DialogInterface.OnCancelListener() {@Overridepublic void onCancel(DialogInterface dialog) {if (imgUri != null) {getContentResolver().delete(imgUri, null, null);imgUri = null;}}});AlertDialog alert = builder.create();alert.show();}}}

    上面的這些代碼,很大的一部分在處理有多個可以接受裁剪intent請求的APP上面,如果你只想用預設的第一個APP,那麼這些邏輯你可以刪除。

    在說完這些東西之後,我們在onActivityResult裡面可以這樣處理:

@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {if (resultCode != RESULT_OK) {return;}switch (requestCode) {case PICK_FROM_CAMERA:doCrop();break;case PICK_FROM_FILE:imgUri = data.getData();doCrop();break;case CROP_FROM_CAMERA:if (null != data) {setCropImg(data);}break;}}

    不管從哪裡選,都需要進入裁剪,然後裁剪結束之後,我們調用setCropImg(),把裁剪之後的結果設定就可以了,如下

private void setCropImg(Intent picdata) {Bundle bundle = picdata.getExtras();if (null != bundle) {Bitmap mBitmap = bundle.getParcelable("data");mImageView.setImageBitmap(mBitmap);saveBitmap(Environment.getExternalStorageDirectory() + "/crop_"+ System.currentTimeMillis() + ".png", mBitmap);}}

    saveBitmap是幹嘛的?當然是把裁剪之後的Bitmap存起來了!就像下面這樣

public void saveBitmap(String fileName, Bitmap mBitmap) {File f = new File(fileName);FileOutputStream fOut = null;try {f.createNewFile();fOut = new FileOutputStream(f);mBitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);fOut.flush();} catch (Exception e) {e.printStackTrace();} finally {try {fOut.close();Toast.makeText(this, "save success", Toast.LENGTH_SHORT).show();} catch (IOException e) {e.printStackTrace();}}}

    好了,現在存起來了,就可以上傳到伺服器,完工~

    記得加許可權

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<uses-permission android:name="android.permission.WRITE_SETTINGS" /> 

代碼下載:https://github.com/ZhaoKaiQiang/CropImageDemo


參考文章:

如何使用Android MediaStore裁剪大圖片

Android大圖片裁剪終極解決方案(上:原理分析) 

Android大圖片裁剪終極解決方案(中:從相簿) 

Android大圖片裁剪終極解決方案(下:拍照)

【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.