Android 上传头像(文件)到服务器

现在很多app都用到了头像的功能,我的项目中也用到了。
头像上传分几步:
1.获取头像
2.剪裁头像
3.文件上传
4.服务器的接受保存
首先第一步,无非就是两种方式
1,拍照
2,相册选择
之前的博客中有现到,不重复,链接:
http://blog.youkuaiyun.com/w18756901575/article/details/52087242
http://blog.youkuaiyun.com/w18756901575/article/details/52085157
第二步,裁剪头像
这一步我用的时鸿洋大神的东西,是个自定的控件,不过我对其中代码进行了一点点的修改,使之更适合自己的项目,里面原理就是监听onTouchEvent来对缩放,双击事件进行相应的处理,裁剪就是根据大小,创建一个新的bitmap,当然说起来挺简单,如果没有别人的代码可以借鉴参考,估计有的搞了,在此谢谢鸿洋大神,下面是鸿洋大神的博客链接:
http://blog.youkuaiyun.com/lmj623565791/article/details/39761281
裁剪图片好像还可以调用Android自带的裁剪功能,不过我没用过,
第三步,上传图片,
上传图片不过也就是上传文件,可以将文件转化为byte数组然后进行base64转码后进行上传,在服务器接收到后,将string解码成byte数组,然后在转化为文件。
当然上传的方法有很多,肯定不止这一种 ,因为项目的Android和web接口都是我写的所已我就这样写啦…谁让自己的web技术有限呢,原来写Android的时候一直说调用接口接口,原来自己写了才知道,其实就是一个配置好servlet的java文件.⊙﹏⊙||
写接口和写Android差不过嘛,都是用Java写的,本家嘛
对了文件上传的时候如果从本地读取,会需要读取sd卡的权限,我在6.0的模拟器上测试的时候,一直有问题,找了半天才发现是因为,没有申请权限,,,设立的权限不仅仅时AndroidManifest里面的权限声明,在6.0的系统中需要用代码弹出对话框向用户申请..
贴下请求代码

    /**
     * 请求权限
     */
    public void getUserPar() {
//        Log.d("测试--授权信息", ContextCompat.checkSelfPermission(this,
//                Manifest.permission.WRITE_EXTERNAL_STORAGE) + "");
//        Log.d("测试--已授权", PackageManager.PERMISSION_GRANTED + "");
//        Log.d("测试--未授权", PackageManager.PERMISSION_DENIED + "");
        if (ContextCompat.checkSelfPermission(this,
                Manifest.permission.WRITE_EXTERNAL_STORAGE)
                == PackageManager.PERMISSION_GRANTED && ContextCompat.checkSelfPermission(this,
                Manifest.permission.READ_EXTERNAL_STORAGE)
                == PackageManager.PERMISSION_GRANTED) {
        } else {
            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE},
                    1);
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
        switch (requestCode) {
            case 1: {
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    L.d("同意");
                } else {
                    L.d("拒绝");
                }
                return;
            }
        }
    }

还有文件和数组相互转化的代码

    /**
     * 将file文件转化为byte数组
     *
     * @param filePath
     * @return
     */
    public static byte[] getBytes(String filePath) {
        byte[] buffer = null;
        try {
            File file = new File(filePath);
            FileInputStream fis = new FileInputStream(file);
            ByteArrayOutputStream bos = new ByteArrayOutputStream(1000);
            byte[] b = new byte[1000];
            int n;
            while ((n = fis.read(b)) != -1) {
                bos.write(b, 0, n);
            }
            fis.close();
            bos.close();
            buffer = bos.toByteArray();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return buffer;
    }

    /**
     * 将byte数组转化为file文件
     *
     * @param bfile
     * @param filePath
     * @param fileName
     */
    public static void getFile(byte[] bfile, String filePath, String fileName) {
        BufferedOutputStream bos = null;
        FileOutputStream fos = null;
        File file = null;
        try {
            File dir = new File(filePath);
            if (!dir.exists() && dir.isDirectory()) {
  
  // 判断文件目录是否存在
                dir.mkdirs();
            }
            file = new File(filePath + "\\" + fileName);
            fos = new FileOutputStream(file);
            bos = new BufferedOutputStream(fos);
            bos.write(bfile);
            bos.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (bos != null) {
                try {
                    bos.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        }
    }

还有base64转码的:

http://blog.youkuaiyun.com/w18756901575/article/details/51073847

嗯,好像只有这些,,没了,,end

对了还有圆角,模糊,图片压缩,,,一起放上去吧

圆形图片效果:



import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.widget.ImageView;

import com.yesun.league.R;
import com.yesun.league.code.utils.DimensUtils;


/**
 * 自定义View,实现圆角,圆形等效果
 *
 * @author zhy
 */
public class CustomImageView extends ImageView {
    
    

    /**
     * TYPE_CIRCLE / TYPE_ROUND
     */
    private int type;
    private static final int TYPE_CIRCLE = 0;
    
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值