安卓裁剪上传保存头像
privateString[]items=newString[]{"图库","拍照"};privatestaticfinalintIMAGE_REQUEST_CODE=0;privatestaticfinalintCAMERA_REQUEST_CODE=1;privatestaticfinalintRESULT_REQUEST_CODE=2;publicvoidheadphoto(Viewview){newAlertDialog.Builder(this).setTitle("设置头像").setItems(items,newDialogInterface.OnClickListener(){@OverridepublicvoidonClick(DialogInterfacedialog,intwhich){switch(which){case0:IntentintentFromGallery=newIntent();intentFromGallery.setType("p_w_picpath/*");//设置文件类型intentFromGallery.setAction(Intent.ACTION_GET_CONTENT);startActivityForResult(intentFromGallery,IMAGE_REQUEST_CODE);break;case1:Intentintent=newIntent(MediaStore.ACTION_IMAGE_CAPTURE);intent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(newFile(Environment.getExternalStorageDirectory()+"/head/","temp.jpg")));startActivityForResult(intent,CAMERA_REQUEST_CODE);break;}}}).setNegativeButton("取消",newDialogInterface.OnClickListener(){@OverridepublicvoidonClick(DialogInterfacedialog,intwhich){dialog.dismiss();}}).show();}@OverrideprotectedvoidonActivityResult(intrequestCode,intresultCode,Intentdata){//结果码不等于取消时候if(resultCode!=RESULT_CANCELED){switch(requestCode){caseIMAGE_REQUEST_CODE:startPhotoZoom(data.getData());break;caseCAMERA_REQUEST_CODE:Filepicture=newFile(Environment.getExternalStorageDirectory()+"/head/temp.jpg");startPhotoZoom(Uri.fromFile(picture));break;caseRESULT_REQUEST_CODE:if(data!=null){getImageToView(data);}break;}}super.onActivityResult(requestCode,resultCode,data);}/***裁剪图片方法实现**@paramuri*/publicvoidstartPhotoZoom(Uriuri){Intentintent=newIntent("com.android.camera.action.CROP");intent.setDataAndType(uri,"p_w_picpath/*");//设置裁剪intent.putExtra("crop","true");//aspectXaspectY是宽高的比例intent.putExtra("aspectX",1);intent.putExtra("aspectY",1);//outputXoutputY是裁剪图片宽高intent.putExtra("outputX",320);intent.putExtra("outputY",320);intent.putExtra("return-data",true);startActivityForResult(intent,2);}/***保存裁剪之后的图片数据**@parampicdata*/privatevoidgetImageToView(Intentdata){Bundleextras=data.getExtras();if(extras!=null){Bitmapphoto=extras.getParcelable("data");Drawabledrawable=newBitmapDrawable(photo);head.setImageDrawable(drawable);savePic(photo);}}/***@parambitmap*@throws保存图片到本地*/publicvoidsavePic(Bitmapb){FileOutputStreamfos=null;try{Stringpath=(Environment.getExternalStorageDirectory().getAbsolutePath()+"/head/");Filefile=newFile(path);if(!file.exists()){file.mkdirs();}Filef=newFile(path,"head.png");if(f.exists()){f.delete();}fos=newFileOutputStream(f);if(null!=fos){b.compress(Bitmap.CompressFormat.PNG,90,fos);fos.flush();fos.close();}}catch(FileNotFoundExceptione){e.printStackTrace();}catch(IOExceptione){e.printStackTrace();}}/***@param读取照片*@return*/publicstaticBitmapgetDiskBitmap(StringpathString){Bitmapbitmap=null;try{Filefile=newFile(pathString);if(file.exists()){bitmap=BitmapFactory.decodeFile(pathString);}}catch(Exceptione){}returnbitmap;}Stringpath=(Environment.getExternalStorageDirectory().getAbsolutePath()+"/bbpet/head/head.png");Bitmapbitmap=MyDataActivity.getDiskBitmap(path);if(bitmap!=null){headimg.setImageBitmap(bitmap);}else{headimg.setBackgroundResource(R.drawable.ic_launcher);}
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。