安卓中上传头像的应用

来源:互联网 发布:限电插座淘宝 编辑:程序博客网 时间:2024/04/28 17:19

在最近的项目中,要应用到上传头像。为了图省事在网上找了许多代码,结果发现并不适用与自己。干脆就自己写了一下。


private void showChoosePicDialog() {    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());    builder.setTitle("设置头像");    String[] items = {"选择本地照片", "拍照"};    builder.setNegativeButton("取消", null);    builder.setItems(items, new DialogInterface.OnClickListener() {        @Override        public void onClick(DialogInterface dialog, int which) {            switch (which) {                case CHOOSE_PICTURE: // 选择本地照片                    Intent openAlbumIntent = new Intent(                            Intent.ACTION_GET_CONTENT);                    openAlbumIntent.setType("image/*");                    startActivityForResult(openAlbumIntent, CHOOSE_PICTURE);                    break;                case TAKE_PICTURE: // 拍照                    Intent openCameraIntent = new Intent(                            MediaStore.ACTION_IMAGE_CAPTURE);                    tempUri = Uri.fromFile(new File(Environment                            .getExternalStorageDirectory(), "image.jpg"));                    // 指定照片保存路径(SD卡),image.jpg为一个临时文件,每次拍照后这个图片都会被替换                    openCameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, tempUri);                    startActivityForResult(openCameraIntent, TAKE_PICTURE);                    break;            }        }    });    builder.create().show();}

弹出对话框,选择相册中获取还是拍照处理

protected static final int CHOOSE_PICTURE = 0;protected static final int TAKE_PICTURE = 1;private static final int CROP_SMALL_PICTURE = 2;

@Overridepublic void onActivityResult(int requestCode, int resultCode, Intent data) {    super.onActivityResult(requestCode, resultCode, data);    if (resultCode == -1) { // 如果返回码是可以用的        switch (requestCode) {            case TAKE_PICTURE:                startPhotoZoom(tempUri); // 开始对图片进行裁剪处理                break;            case CHOOSE_PICTURE:                startPhotoZoom(data.getData()); // 开始对图片进行裁剪处理                break;            case CROP_SMALL_PICTURE:                if (data != null) {                    setImageToView(data); // 让刚才选择裁剪得到的图片显示在界面上                }                break;        }    }}
protected void startPhotoZoom(Uri uri) {    if (uri == null) {        Log.i("tag", "The uri is not exist.");    }    tempUri = 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", 150);    intent.putExtra("outputY", 150);    intent.putExtra("return-data", true);    startActivityForResult(intent, CROP_SMALL_PICTURE);}
protected void setImageToView(Intent data) {    Bundle extras = data.getExtras();    if (extras != null) {        photo = extras.getParcelable("data");        photo = Utils.toRoundBitmap(photo, tempUri); // 这个时候的图片已经被处理成圆形的了        mycount_logo.setImageBitmap(photo);        uploadPic(photo);    }}private void uploadPic(Bitmap bitmap) {    // 上传至服务器    // ... 可以在这里把Bitmap转换成file,然后得到fileurl,做文件上传操作    // 注意这里得到的图片已经是圆形图片了    // bitmap是没有做个圆形处理的,但已经被裁剪了    final String imagePath = Utils.savePhoto(bitmap, Environment            .getExternalStorageDirectory().getAbsolutePath(), String            .valueOf(System.currentTimeMillis()));    Log.e("imagePath", imagePath + "");    if (imagePath != null) {        // 拿着imagePath上传了        // ...        final File file = new File(imagePath);        SPUtils.setImagePath(getActivity(), imagePath);        new Thread() {            public void run() {                //   UploadUtil.uploadFile(file, "http://172.20.16.168:8080/uploadHead");                final String tokenId = MyApplication.getInstance().getTokenId();                HttpUploadFile.testUploadImage(imagePath, tokenId);                getActivity().runOnUiThread(new Runnable() {                    @Override                    public void run() {                        Picasso.with(getActivity()).load(new File(SPUtils.getImagePath(getActivity()))).placeholder(R.drawable.zhanghulogo).into(mycount_logo);                    }                });            }        }.start();    }
里面的一些工具类,会在下一篇中分享

2 0
原创粉丝点击