关于android端的对选择相册和照相机的处理

一 点击用户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();  

        }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值