项目中有一个功能:发帖。
帖子内容:文字,图片(可以是多张)。这就涉及到多张图片的上传问题。
后台接口文档如下:
使用Retrofit+Rxjava上传多张图片的代码如下:
/**
* 上传多张图片(带参数)
*/
public void uploadMoreImage2(RetrofitSubscriber<BaseRspBean<ImageUploadBean>> subscriber, List<File> imageFileList){
List<MultipartBody.Part> 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;
}
以上就是上传多张图片的逻辑。