项目中有一个功能:发帖。

帖子内容:文字,图片(可以是多张)。这就涉及到多张图片的上传问题。

后台接口文档如下:

使用Retrofit+Rxjava上传多张图片的代码如下:

/** * 上传多张图片(带参数) */public void uploadMoreImage2(RetrofitSubscriber> subscriber, List imageFileList){    List photos = new ArrayList<>(imageFileList.size());    //上传图片时携带参数,这里用于区分是帖子图片还是其他图片,比如头像等    RequestBody imageType = RequestBody.create(MediaType.parse("text/plain"), "post");    MultipartBody.Part body = MultipartBody.Part.createFormData("type",null, imageType);    photos.add(body);    //文件(注意与上面的不同)    if(imageFileList != null && !imageFileList.isEmpty()) {        for (File file:imageFileList){            RequestBody photoRequestBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);            MultipartBody.Part part= MultipartBody.Part.createFormData("files", file.getName(), photoRequestBody);            photos.add(part);        }    }    RetrofitUtil.toSubscribe(getServiceOutHead(UrlConfig.BASEURL).uploadMoreImage2(HeaderHelper.getFileUpdateHeader(),photos),mContext, subscriber);}

 

当然了,上传的图片需要经过压缩处理才能上传。(我们的图片是从本地相册选择的或者是拍照所得的)

压缩方法:

 

Bitmaputils.java/** * 图片压缩-质量压缩 * * @param filePath 源图片路径 * @return 压缩后的路径 */public static String compressImage(String filePath) {    //原文件    File oldFile = new File(filePath);    //压缩文件路径 照片路径/    String targetPath = oldFile.getPath();    int quality = 50;//压缩比例0-100    Bitmap bm = getSmallBitmap(filePath);//获取一定尺寸的图片    int degree = getRotateAngle(filePath);//获取相片拍摄角度    if (degree != 0) {//旋转照片角度,防止头像横着显示        bm = setRotateAngle(degree,bm);    }    File outputFile = new File(targetPath);    try {        if (!outputFile.exists()) {            outputFile.getParentFile().mkdirs();        }        FileOutputStream out = new FileOutputStream(outputFile);        bm.compress(Bitmap.CompressFormat.JPEG, quality, out);        out.close();    } catch (Exception e) {        e.printStackTrace();        return filePath;    }    return outputFile.getPath();}/** * 根据路径获得图片信息并按比例压缩,返回bitmap */public static Bitmap getSmallBitmap(String filePath) {    final BitmapFactory.Options options = new BitmapFactory.Options();    options.inJustDecodeBounds = true;//只解析图片边沿,获取宽高    BitmapFactory.decodeFile(filePath, options);    // 计算缩放比    options.inSampleSize = calculateInSampleSize(options, 480, 800);    // 完整解析图片返回bitmap    options.inJustDecodeBounds = false;    return BitmapFactory.decodeFile(filePath, options);}public static int calculateInSampleSize(BitmapFactory.Options options,                                        int reqWidth, int reqHeight) {    final int height = options.outHeight;    final int width = options.outWidth;    int inSampleSize = 1;    if (height > reqHeight || width > reqWidth) {        final int heightRatio = Math.round((float) height / (float) reqHeight);        final int widthRatio = Math.round((float) width / (float) reqWidth);        inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;    }    return inSampleSize;}/** * 旋转图片角度 * * @param angle * @param bitmap * @return */public static Bitmap setRotateAngle(int angle, Bitmap bitmap) {    if (bitmap != null) {        Matrix m = new Matrix();        m.postRotate(angle);        bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),                bitmap.getHeight(), m, true);        return bitmap;    }    return bitmap;}

以上就是上传多张图片的逻辑。

更多相关文章

  1. android 保存图片到数据库
  2. Android动态设置android:drawableLeft|Right|Top|Bottom 并根据
  3. Android使用WebView加载网页选择文件上传
  4. android工程中,各种类型的资源文件
  5. Android的安装过程
  6. android 内存泄露--加载网络图片--android 12742656-byte extern
  7. 下载安装Android(安卓)sdk tools
  8. 关于下载最新版本Android(安卓)Studio却无法启动默认HelloWorld
  9. Android下载文件,如果文件夹下有同名文件,则重命名规则为a(2)、a(3

随机推荐

  1. android内置和外置sdcard区别
  2. Android应用程序组件Content Provider在
  3. android 模拟器横竖切换
  4. Android 远程链接 daemon not running 解
  5. android > Android实现计时与倒计时的几
  6. android6.0创建文件问题
  7. Android 进化
  8. Android Jetpack Navigation 的使用
  9. 怎样使用android自带例子程序
  10. Android AudioRecord录音实现