android 选择头像上传

记录一下android 选择头像上传的实现。

简述一下:从图库获取的图片再经过裁剪后得到的图片质量很高,完全可以满足我们的需求。但是从使用一下这种方式通过相机拍照再裁剪后获得图片质量很低,不能满足使用。

Intent intent2 = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);// 请求拍照的Action
startActivityForResult(intent2, CAMERA_REQUEST_CODE);
所以我们需要另一种方法来获取质量比较高得图片。



简单布局一下,使用线性布局,

一个ImageView,然后一个选择图库照片按钮,一个拍照获取图片按钮。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:fitsSystemWindows="true"
tools:context="dao.d.photopickerdemo.MainActivity">

<ImageView
    android:id="@+id/iv_image"
    android:layout_width="96dp"
    android:layout_height="96dp"
    android:src="@mipmap/ic_launcher" />
<Button
    android:id="@+id/btn_gallery"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="获取图库图片" />
<Button
    android:id="@+id/btn_camera"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="拍照获取图片" />
</LinearLayout>

然后就是代码实现。

1.定义

private static int CAMERA_REQUEST_CODE = 1;
private static int GALLERY_REQUEST_CODE = 2;
private static int CROP_REQUEST_CODE = 3;

private String capturePath = "";//记录每一次文件路径

/**
 * 上传图片的宽度和高度
 */
private int mWH = 96;// 单位dp
另外,需要把dp转化为px

mWH = (int) (getResources().getDisplayMetrics().density * mWH);

2.从图库选择:

Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(intent, GALLERY_REQUEST_CODE);

3.拍照选择,这种方式果断被抛弃

Intent intent2 = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);// 请求拍照的Action
startActivityForResult(intent2, CAMERA_REQUEST_CODE);

  我们要使用这一种方法:

 

Intent intent2 = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);// 请求拍照的Action
String out_file_path = "/sdcard/image/";
File dir = new File(out_file_path);
if (!dir.exists()) {
    dir.mkdirs();
}
capturePath = out_file_path + System.currentTimeMillis() + ".jpg";
intent2.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(capturePath)));
intent2.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
startActivityForResult(intent2, CAMERA_REQUEST_CODE);


4.onActivityResult方法实现

 @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == CAMERA_REQUEST_CODE) {
                File file = new File(capturePath);
                Uri uri = Uri.fromFile(file);
                startImageZoom(uri);
            } else if (requestCode == GALLERY_REQUEST_CODE) {
                if (data == null) {//相册
                    return;
                }
                Uri uri;
                uri = data.getData();
                startImageZoom(uri);
            } else if (requestCode == CROP_REQUEST_CODE) {
                if (data == null) {
                    return;
                }//剪裁后的图片
                Bundle extras = data.getExtras();
                if (extras == null) {
                    return;
                }
                Bitmap bm = extras.getParcelable("data");
                Log.e("bm.getWidth", "" + bm.getWidth());
                Log.e("bm.getHeight", "" + bm.getHeight());
                Bitmap finalBm = toRoundCorner(bm, 16);
                iv_image.setImageBitmap(finalBm);
      //          upLoadFile(finalBm);//关于上传在之前写过,不再多说
            }
        }
    }


/**
 * 剪裁图片
 *
 * @param uri
 */
private void startImageZoom(Uri uri) {
    Intent intent = new Intent("com.android.camera.action.CROP");
    intent.setDataAndType(uri, "image/*");
    intent.putExtra("crop", "true");
    // 裁剪框的比例,11
    intent.putExtra("aspectX", 1);
    intent.putExtra("aspectY", 1);
    // 裁剪后输出图片的尺寸大小
    intent.putExtra("outputX", mWH);
    intent.putExtra("outputY", mWH);
    Log.e("mWH", "" + mWH);
    Log.e("mWH", "" + mWH);
    // 图片格式
    intent.putExtra("outputFormat", "png");
    intent.putExtra("noFaceDetection", false);// 取消人脸识别
    intent.putExtra("return-data", true);// true:不返回urifalse:返回uri
    startActivityForResult(intent, CROP_REQUEST_CODE);
}



/**
 * 将图片变为圆角
 *
 * @param bitmap Bitmap图片
 * @param pixels 图片圆角的弧度(单位:像素(px))
 * @return 带有圆角的图片(Bitmap 类型)
 */
public static Bitmap toRoundCorner(Bitmap bitmap, int pixels) {
    Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
            bitmap.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(output);
    final int color = 0xff424242;
    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
    final RectF rectF = new RectF(rect);
    final float roundPx = pixels;

    paint.setAntiAlias(true);
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, rect, paint);
    return output;
}

写的过于匆忙,很多没有细讲。建议直接查看此贴,我也是主要参考了这个帖子。



http://blog.csdn.net/beyond0525/article/details/8940840




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值