Android手機(jī)拍照或選取圖庫(kù)圖片作為頭像
package zhangpgil.photo; import java.io.File; import android.support.v7.app.ActionBarActivity; import android.view.View; import android.widget.Button; import android.widget.ImageView; import android.widget.Toast; import android.content.Intent; import android.graphics.Bitmap; import android.net.Uri; import android.os.Bundle; import android.os.Environment; import android.provider.MediaStore; public class MainActivity extends ActionBarActivity { /* 頭像文件 */ private static final String IMAGE_FILE_NAME = "temp_head_image.jpg"; /* 請(qǐng)求識(shí)別碼 */ private static final int CODE_GALLERY_REQUEST = 0xa0; private static final int CODE_CAMERA_REQUEST = 0xa1; private static final int CODE_RESULT_REQUEST = 0xa2; // 裁剪后圖片的寬(X)和高(Y),480 X 480的正方形。(生成bitmap貌似有時(shí)要報(bào)錯(cuò)?可試下把大小弄小點(diǎn)) private static int output_X = 480; private static int output_Y = 480; private ImageView headImage = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); headImage = (ImageView) findViewById(R.id.imageView); Button buttonLocal = (Button) findViewById(R.id.buttonLocal); buttonLocal.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { choseHeadImageFromGallery(); } }); Button buttonCamera = (Button) findViewById(R.id.buttonCamera); buttonCamera.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { choseHeadImageFromCameraCapture(); } }); } // 從本地相冊(cè)選取圖片作為頭像 private void choseHeadImageFromGallery() { Intent intentFromGallery = new Intent(); // 設(shè)置文件類型 intentFromGallery.setType("image/*"); intentFromGallery.setAction(Intent.ACTION_GET_CONTENT); startActivityForResult(intentFromGallery, CODE_GALLERY_REQUEST); } // 啟動(dòng)手機(jī)相機(jī)拍攝照片作為頭像 private void choseHeadImageFromCameraCapture() { Intent intentFromCapture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); // 判斷存儲(chǔ)卡是否可用,存儲(chǔ)照片文件 if (hasSdcard()) { intentFromCapture.putExtra(MediaStore.EXTRA_OUTPUT, Uri .fromFile(new File(Environment .getExternalStorageDirectory(), IMAGE_FILE_NAME))); } startActivityForResult(intentFromCapture, CODE_CAMERA_REQUEST); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent intent) { // 用戶沒(méi)有進(jìn)行有效的設(shè)置操作,返回 if (resultCode == RESULT_CANCELED) { Toast.makeText(getApplication(), "取消", Toast.LENGTH_LONG).show(); return; } switch (requestCode) { case CODE_GALLERY_REQUEST: cropRawPhoto(intent.getData()); break; case CODE_CAMERA_REQUEST: if (hasSdcard()) { File tempFile = new File( Environment.getExternalStorageDirectory(), IMAGE_FILE_NAME); cropRawPhoto(Uri.fromFile(tempFile)); } else { Toast.makeText(getApplication(), "沒(méi)有SDCard!", Toast.LENGTH_LONG) .show(); } break; case CODE_RESULT_REQUEST: if (intent != null) { setImageToHeadView(intent); } break; } super.onActivityResult(requestCode, resultCode, intent); } /** * 裁剪原始的圖片 */ public void cropRawPhoto(Uri uri) { Intent intent = new Intent("com.android.camera.action.CROP"); intent.setDataAndType(uri, "image/*"); // 設(shè)置裁剪 intent.putExtra("crop", "true"); // aspectX , aspectY :寬高的比例 intent.putExtra("aspectX", 1); intent.putExtra("aspectY", 1); // outputX , outputY : 裁剪圖片寬高 intent.putExtra("outputX", output_X); intent.putExtra("outputY", output_Y); intent.putExtra("return-data", true); startActivityForResult(intent, CODE_RESULT_REQUEST); } /** * 提取保存裁剪之后的圖片數(shù)據(jù),并設(shè)置頭像部分的View */ private void setImageToHeadView(Intent intent) { Bundle extras = intent.getExtras(); if (extras != null) { Bitmap photo = extras.getParcelable("data"); headImage.setImageBitmap(photo); } } /** * 檢查設(shè)備是否存在SDCard的工具方法 */ public static boolean hasSdcard() { String state = Environment.getExternalStorageState(); if (state.equals(Environment.MEDIA_MOUNTED)) { // 有存儲(chǔ)的SDCard return true; } else { return false; } } }
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <ImageView android:id="@+id/imageView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_launcher" /> <Button android:id="@+id/buttonLocal" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="本地相冊(cè)選取頭像" /> <Button android:id="@+id/buttonCamera" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="手機(jī)拍照選取頭像" /> </LinearLayout>
以上所述就是本文的全部?jī)?nèi)容了,希望大家能夠喜歡。
- Android實(shí)現(xiàn)本地上傳圖片并設(shè)置為圓形頭像
- Android使用CircleImageView實(shí)現(xiàn)圓形頭像的方法
- Android根據(jù)電話號(hào)碼獲得聯(lián)系人頭像實(shí)例代碼
- Android實(shí)現(xiàn)用戶頭像更換操作
- Android實(shí)現(xiàn)從本地圖庫(kù)/相機(jī)拍照后裁剪圖片并設(shè)置頭像
- Android一行代碼實(shí)現(xiàn)圓形頭像
- Android實(shí)現(xiàn)調(diào)用系統(tǒng)圖庫(kù)與相機(jī)設(shè)置頭像并保存在本地及服務(wù)器
- Android實(shí)現(xiàn)個(gè)人資料頁(yè)面頭像背景模糊顯示包(狀態(tài)欄)
- Android頭像上傳功能的實(shí)現(xiàn)代碼(獲取頭像加剪切)
- Android實(shí)現(xiàn)IM多人員組合的群組頭像
相關(guān)文章
Android通過(guò)overScrollBy實(shí)現(xiàn)下拉視差特效
這篇文章主要為大家詳細(xì)介紹了Android通過(guò)overScrollBy實(shí)現(xiàn)下拉視差特效,實(shí)現(xiàn)精彩的阻尼效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08ionic App 解決android端在真機(jī)上tab處于頂部的問(wèn)題
這篇文章主要介紹了ionic App 解決android端在真機(jī)上tab處于頂部的問(wèn)題的相關(guān)資料,需要的朋友可以參考下2017-06-06詳解flutter之網(wǎng)絡(luò)請(qǐng)求dio,請(qǐng)求,攔截器簡(jiǎn)單示例
這篇文章主要介紹了詳解flutter之網(wǎng)絡(luò)請(qǐng)求dio,請(qǐng)求,攔截器簡(jiǎn)單示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06android加密參數(shù)定位實(shí)現(xiàn)方法
這篇文章主要介紹了android加密參數(shù)定位方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-04-04Google 開(kāi)發(fā)Android MVP架構(gòu)Demo深入解析
這篇文章主要為大家介紹了Google 開(kāi)發(fā)Android MVP架構(gòu)Demo深入解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11android app在后臺(tái)運(yùn)行彈出彈窗
這篇文章主要為大家介紹了android app在后臺(tái)運(yùn)行彈出彈窗,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11Android實(shí)現(xiàn)底部圖標(biāo)與Fragment的聯(lián)動(dòng)實(shí)例
本篇文章主要介紹了Android實(shí)現(xiàn)底部圖標(biāo)與Fragment的聯(lián)動(dòng)實(shí)例,具有一定的參考價(jià)值,有興趣的可以了解一下2017-07-07