一 点击用户logo,弹出对话框,进行选择,是使用相册图片还是使用照相机现拍的照片;
public int NUM_ONE = 1;// 请求本地照片
public int NUM_TWO = 2;//请求打开照相机
FileWay.Path=Environment.getExternalStorageDirectory() + “/iweather/”;
private String TemLocalName=”temp.jpg”;//照相机拍照的图片存取的位置;
private String SDLocationName=”user_load.jpg”;//用户登录的图像保存在SD卡中;
`public void showDialog(){
ModifyAvatarDialog modifyAvatarDialog=new ModifyAvatarDialog(PersionActivity.this){
// 选择相机拍照
@Override
public void doGoToCanera() {
this.dismiss();
String status= Environment.getExternalStorageState();
if(status.equals(Environment.MEDIA_MOUNTED)){
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
File filePath=new File(FileWay.Path);
if (!filePath.exists()) {
filePath.mkdirs();
}
File f=new File(filePath,TemLocalName);
Uri u = Uri.fromFile(f);
intent.putExtra(MediaStore.Images.Media.ORIENTATION, 0);
intent.putExtra(MediaStore.EXTRA_OUTPUT, u);
startActivityForResult(intent, NUM_TWO);
}else{
Toast.makeText(getApplicationContext(), “您的SD卡暂时未能使用,请检查”, 0).show();
}
super.doGoToCanera();
}
// 选择本地相册
@Override
public void doGoToSelect() {
this.dismiss();
Intent intent=new Intent();
intent.setAction(Intent.ACTION_PICK);
intent.setType(“image/*”);
startActivityForResult(intent, NUM_ONE);
super.doGoToSelect();
}
};
Window window = modifyAvatarDialog.getWindow();
// // 设置显示动画
window.setWindowAnimations(R.style.main_menu_animstyle);
window.setBackgroundDrawableResource(R.color.android_transparent);
WindowManager.LayoutParams wl = window.getAttributes();
// // 设置显示位置
modifyAvatarDialog.onWindowAttributesChanged(wl);
// 设置点击外围解散
modifyAvatarDialog.setCanceledOnTouchOutside(true);
modifyAvatarDialog.setCancelable(true);
modifyAvatarDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
modifyAvatarDialog.show();
}`
二 在onActivityForResult函数中进行处理;
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch(requestCode){
case 1://点击相册的回调;
if (data != null) {
Uri uri = data.getData();
if (!TextUtils.isEmpty(uri.getAuthority())) {
Cursor cursor = this.getContentResolver().query(uri,
new String[] { MediaStore.Images.Media.DATA },
null, null, null);
if (null == cursor) {
Toast.makeText(this, "图片没找到", 0).show();
return;
}
cursor.moveToFirst();
String path = cursor.getString(cursor
.getColumnIndex(MediaStore.Images.Media.DATA));
cursor.close();
Log.e("TAG", "本地图片 path=" + path);
Bitmap b = BitmapFactory.decodeFile(path);
if(b!=null){
user_icon.setImageBitmap(b);
try {
//保存如本地,便于下次登录时直接加载本地图片;
saveImgToSD(b, FileWay.Path+SDLocationName);
}
catch (IOException e) {
e.printStackTrace();
}
}
// if(false){//判断用户字段是否已上传图像,若false
// //给用户添加积分;
// //设置上传图像字段为true;
// }
}
}
break;
case 2://点击相机的回调;
Bitmap bitmap=BitmapFactory.decodeFile(FileWay.Path+TemLocalName);
if(bitmap!=null){
user_icon.setImageBitmap(bitmap);
try {
//保存如本地,便于下次登录时直接加载本地图片;
saveImgToSD(bitmap, FileWay.Path+SDLocationName);
}
catch (IOException e) {
e.printStackTrace();
}
}
break;
}
super.onActivityResult(requestCode, resultCode, data);
}
//将登陆图片保存到SD卡中,便于,登录时直接加载出来;
public void saveImgToSD(Bitmap bitmap,String loadImgePath) throws IOException{
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream(new File(loadImgePath)));
bitmap.compress(CompressFormat.JPEG, 100, bos);
bos.flush();
bos.close();
}