Android 編輯頭像功能簡單實(shí)現(xiàn)實(shí)例(圖片選取,裁剪)
本文介紹了Android 編輯頭像功能的簡單實(shí)例,可以實(shí)現(xiàn)拍照,圖片選取,裁剪。
拍照
public static void startCamera(Fragment fragment){ File file=new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM)+ File.separator+"user_icon.jpg"); Intent intent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);//intent隱式調(diào)用啟動(dòng)拍照界面 intent.putExtra("return-data",false);//該屬性設(shè)置為false表示拍照后不會(huì)將數(shù)據(jù)返回到onResluet方法中(建議設(shè)置為false,這樣獲取的圖片會(huì)比較清晰) ComponentName componentName=intent.resolveActivity(fragment.getContext().getPackageManager()); intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));//該屬性設(shè)置的是拍照后圖片保存的位置 //防止app啟動(dòng)意圖時(shí)崩潰 if (componentName!=null){ fragment.startActivityForResult(intent,Variable.request_camera_code); } }
從相冊中選取
public static void startPhoto(Fragment fragment){ Intent intent = new Intent(Intent.ACTION_GET_CONTENT);//intent隱式調(diào)用啟動(dòng)相冊界面 intent.setType("image/*");//設(shè)置數(shù)據(jù)類型 ComponentName componentName = intent.resolveActivity(fragment.getContext().getPackageManager()); Log.d("tag", "startPhoto: "+componentName); if (componentName != null) {//防止啟動(dòng)意圖時(shí)app崩潰 fragment.startActivityForResult(intent, Variable.request_photo); } }
裁剪選取或拍攝的圖片
public static void cropphoto(Fragment fragment, Uri uri){ //設(shè)置裁剪圖片保存位置 File bomb=new File(fragment.getContext().getExternalCacheDir(),"bmob"); Log.d("tag", "cropphoto: "+bomb); if (!bomb.exists()){ bomb.mkdir(); } File file=new File(bomb,"user_icon.jpg"); if (!file.exists()){ try { file.createNewFile(); } catch (IOException e) { e.printStackTrace(); } } Intent intent=new Intent("com.android.camera.action.CROP");//intent隱式調(diào)用啟動(dòng)拍照界面 intent.setDataAndType(uri,"image/*");//設(shè)置需要裁剪的圖片地址 intent.putExtra("crop", "true");//通過put(key,value)方法設(shè)置相關(guān)屬相 intent.putExtra("aspectX", 1);//設(shè)置圖片寬高比例 intent.putExtra("aspectY", 1); intent.putExtra("outputX", 240);//設(shè)置圖片寬高 intent.putExtra("outputY", 240); intent.putExtra("return-data", false);//該屬性設(shè)置為false表示拍照后不會(huì)將數(shù)據(jù)返回到onResluet方法中(建議設(shè)置為false,這樣獲取的圖片會(huì)比較清晰) intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));//該屬性設(shè)置的是拍照后圖片保存的位置 intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());//設(shè)置輸出格式 intent.putExtra("noFaceDetection", true);//是否取消人臉識(shí)別 /*ComponentName componentName = intent.resolveActivity(context.getPackageManager()); Log.d("TAG", "cropphoto: "+componentName); if (componentName!=null){ fragment.startActivityForResult(intent,Variable.request_crop); }*/ fragment.startActivityForResult(intent,Variable.request_crop); }
重寫OnActivityResult方法獲取并設(shè)置對應(yīng)數(shù)據(jù)
@Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); switch (requestCode){ case Variable.request_camera_code://當(dāng)返回的請求碼是啟動(dòng)拍照時(shí)設(shè)置的,此時(shí)調(diào)用裁剪方法 Assist.cropphoto(RegisterFragment.this, Uri.fromFile(new File(Environment .getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM),"user_icon.jpg"))); break; case Variable.request_photo://當(dāng)返回的請求碼是啟動(dòng)相冊時(shí)設(shè)置的,此時(shí)獲取圖片uri并調(diào)用裁剪方法 Assist.cropphoto(RegisterFragment.this,data.getData()); break; case Variable.request_crop://當(dāng)返回的請求碼是啟動(dòng)裁剪時(shí)設(shè)置的,此時(shí)便可獲取最終裁剪好的圖片 Operation op=new Operation(context); String path=context.getExternalCacheDir()+ File.separator+"bmob"+File.separator+"user_icon.jpg";//裁剪好的圖片保存位置 Bitmap icon=op.decodeBitmap(path);//加載本地圖片,并獲取大小合適的bitmap if (icon!=null){ user_icon.setImageBitmap(icon);//將獲取的圖片設(shè)置到imagerview bmobfile=new BmobFile(new File(path)); bmobfile.uploadblock(new UploadFileListener() { @Override public void done(BmobException e) { if (e==null){ //bmob上傳圖片成功 } } }); } break; } }
最終效果展示
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android自定義控件ViewGroup實(shí)現(xiàn)標(biāo)簽云
這篇文章主要為大家詳細(xì)介紹了Android自定義控件ViewGroup實(shí)現(xiàn)標(biāo)簽云,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05Android實(shí)現(xiàn)游戲中的漸隱和漸現(xiàn)動(dòng)畫效果
本文給大家分享android中實(shí)現(xiàn)游戲中的漸隱漸現(xiàn)的動(dòng)畫效果,在游戲開發(fā)中經(jīng)常會(huì)遇到,對android漸隱漸現(xiàn)效果感興趣的朋友可以參考下本教程2016-09-09Android 詳解自定義圓角輸入框和按鈕的實(shí)現(xiàn)流程
對于安卓程序員來說,自定義view簡直不要太重要,畢竟有很多功能,譬如圓形頭像這些,用單純的原生非常難以實(shí)現(xiàn),而用自定義view,簡直分分鐘,今天我們來實(shí)現(xiàn)自定義圓角輸入框和按鈕,大家可以跟著練習(xí),掌握技巧2021-11-11Android懸浮窗的實(shí)現(xiàn)(易錯(cuò)點(diǎn))
現(xiàn)在很多應(yīng)用都使用到懸浮窗,例如微信在視頻的時(shí)候,點(diǎn)擊Home鍵,視頻小窗口仍然會(huì)在屏幕上顯示。下面小編來實(shí)現(xiàn)一下android 懸浮窗,感興趣的朋友跟隨小編一起看看吧2019-10-10android仿微信表情雨下落效果的實(shí)現(xiàn)方法
這篇文章主要給大家介紹了關(guān)于android仿微信表情雨下落效果的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-09-09Android Native庫的加載及動(dòng)態(tài)鏈接的過程
這篇文章主要介紹了Android Native庫的加載及動(dòng)態(tài)鏈接的加載過程,需要的朋友可以參考下2018-01-01Android自定義View實(shí)現(xiàn)雪花特效
這篇文章主要為大家詳細(xì)介紹了Android自定義View實(shí)現(xiàn)雪花特效,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-02-02