朝花夕拾-android 從手機選擇圖片或拍照設定頭像

來源:互聯網
上載者:User

標籤:

Demo源碼位置:http://git.oschina.net/zj2012zy/Android-Demo/tree/master/AndroidDemo/headset

一般需要使用者資訊的好多的也需要設定帳戶圖片,通常設定帳戶圖片的操縱要麼從手機選擇一張圖片,要麼直接通過手機拍照進行設定。如下:

另外:製作手機的動態截屏,可以使用應用寶的動態截屏功能,非常的方便。

布局檔案很簡單:就是兩個按鈕加一個imageview用來顯示設定好的頭像,就不說了。

核心代碼為如下四個函數:

1.選擇一張圖片

1     public static void selectPhoto(Activity activity) {2         Intent intent = new Intent(Intent.ACTION_GET_CONTENT);3         intent.setType("image/jpeg");4         intent.putExtra("return-data", true);5         activity.startActivityForResult(intent, PHOTO_PICKED_WITH_DATA);6     }

2、拍照並儲存

 1     public static String takePicture(Activity activity) { 2         createDirFile(Environment.getExternalStorageDirectory().getPath() + "/zhaojie/images/"); 3         Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 4         String timeStamp = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()); 5         String path = Environment.getExternalStorageDirectory().getPath() + "/zhaojie/images/" + timeStamp + ".png"; 6         File file = createNewFile(path); 7         if (file != null) { 8             intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file)); 9         }10         activity.startActivityForResult(intent, INTENT_REQUEST_CODE_CAMERA);11         return path;12     }

3、處理拍照或者是選擇的照片,截取頭像,其中可指定截取頭像的大小。

 1     private void cropImageUri(Uri uri, int requestCode) { 2         Intent intent = new Intent("com.android.camera.action.CROP"); 3         intent.setDataAndType(uri, "image/*"); 4         intent.putExtra("crop", "true"); 5         intent.putExtra("aspectX", 1); 6         intent.putExtra("aspectY", 1); 7         intent.putExtra("outputX", 320); 8         intent.putExtra("outputY", 320); 9         intent.putExtra("scale", true);10         intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);11         intent.putExtra("return-data", true);12         intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());13         intent.putExtra("noFaceDetection", true); // no face detection14         startActivityForResult(intent, requestCode);15     }

4、整體流程基本都是通過調用系統activity實現,代碼如下

 1     protected void onActivityResult(int requestCode, int resultCode, Intent data) { 2         super.onActivityResult(requestCode, resultCode, data); 3         if (resultCode != RESULT_OK) 4             return; 5         Bitmap tempPhoto = null; 6         switch (requestCode) { 7             case PHOTO_PICKED_WITH_DATA: // ???????? 8                 ContentResolver cr = this.getContentResolver(); 9                 try {10                     Uri uri = data.getData();11                     cropImageUri(uri, PHOTO_CROP_DATA);12                     // tempPhoto =13                     // BitmapFactory.decodeStream(cr.openInputStream(uri));14                     // mStepPhoto.setUserPhoto(tempPhoto);15                 } catch (Exception e) {16                     e.printStackTrace();17                 }18                 break;19             case INTENT_REQUEST_CODE_CAMERA:20                 tempPhoto = BitmapFactory.decodeFile(headPath);21                 cropImageUri(Uri.fromFile(new File(headPath)), PHOTO_CROP_DATA);22                 break;23             case PHOTO_CROP_DATA:24                 tempPhoto = data.getParcelableExtra("data");25                 mHead.setImageBitmap(tempPhoto);26                 break;27         }28     }

文章從簡,具體實現請參考demo,歡迎留言討論

 

朝花夕拾-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.