头像的更改

开发板推荐:天空星STM32F407VET6开发板

超高性价比 STM32主控 | 超高主频 | 一板兼容百芯 | 比赛神器 | 沉金彩色丝印

private void getPhoto(int i) {
        if(i==TAKEPHOTO){
            Intent ti = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
             // 判断存储卡是否可以用,可用进行存储
            if (ImageUtils.hasSdcard()) {
                ti.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(new File(Constants.picRootPath,"head_icon.png")));
            }
           startActivityForResult(ti,TAKEPHOTO_CODE);
        }
        if(i==CHOOSEIMAGE){
            Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
            intent.setType("image/*");
            startActivityForResult(Intent.createChooser(intent, getResources().getString(R.string.choose_pic)), CHOOSEIMAGE_CODE);
        }

    }

对于通过照相机,取得图片,先指定图片保存位置,然后取图片。原因是:android 不同手机照相机返回data数据不一样,有的是可以通过data.getData()获取,有的则需要通过,

data.getIntent("data")获取map集合。

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode==Activity.RESULT_OK){
            switch (requestCode) {
            case TAKEPHOTO_CODE:
                if (ImageUtils.hasSdcard()) {
                    File tempFile = new File(Constants.picRootPath+ "head_icon.png");
                    startPhotoZoom(Uri.fromFile(tempFile));
                }
                break;
            case CHOOSEIMAGE_CODE:
                Uri tu = data.getData();
                startPhotoZoom(tu);
                break;

        case SAVEIMAGE_CODE:
                if(data != null){
                    getImageToView(data);
                 }
                break;
            default:
                break;
            }
        }
    }

 /**
     * 裁剪图片方法实现
     * @param uri
     */
    public void startPhotoZoom(Uri uri) {
           Intent intent = new Intent("com.android.camera.action.CROP");
           intent.setDataAndType(uri, "image/*");
            // 设置裁剪
           intent.putExtra("crop", "true");
            // aspectX aspectY 是宽高的比例
           intent.putExtra("aspectX", 1);
           intent.putExtra("aspectY", 1);
            // outputX outputY 是裁剪图片宽高
           intent.putExtra("outputX", 100);
           intent.putExtra("outputY", 100);
           intent.putExtra("return-data", true);
           startActivityForResult(intent, SAVEIMAGE_CODE);
    }

/**
     * 保存裁剪之后的图片数据
    *
     * @param picdata
     */
    private void getImageToView(Intent data) {
            Bundle extras = data.getExtras();
            if (extras != null) {
                    Bitmap photo = extras.getParcelable("data");
                    iconUrl = ImageUtils.saveBitmap(photo);
                    Drawable drawable = new BitmapDrawable(photo);
                    icon.setImageDrawable(drawable);
            }
    }

public static String saveBitmap(Bitmap photo) {
                //保存入sdCard  
                File file= new File(Constants.picRootPath+ "icon.png");  
                try {  
                  FileOutputStream out = new FileOutputStream(file);  
                  if(photo.compress(Bitmap.CompressFormat.PNG, 100, out)){  
                    out.flush();  
                    out.close();  
                  }  
                } catch (Exception e) {  
                  e.printStackTrace();
                 }  
                return file.getPath();
            }

开发板推荐:天空星STM32F407VET6开发板

超高性价比 STM32主控 | 超高主频 | 一板兼容百芯 | 比赛神器 | 沉金彩色丝印

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值