diff --git a/ImageLibrary/src/main/java/com/renyu/imagelibrary/commonutils/Utils.java b/ImageLibrary/src/main/java/com/renyu/imagelibrary/commonutils/Utils.java index 1a06a7a..199c5dd 100644 --- a/ImageLibrary/src/main/java/com/renyu/imagelibrary/commonutils/Utils.java +++ b/ImageLibrary/src/main/java/com/renyu/imagelibrary/commonutils/Utils.java @@ -215,6 +215,19 @@ public static void choiceVideo(Fragment fragment, int maxNum, int requestCode) { * @return */ public static File compressPic(Context context, String filePath, String destinationDirectoryPath) { + return compressPic(context, filePath, destinationDirectoryPath, System.currentTimeMillis() + ".jpg"); + } + + /** + * 图片压缩 + * + * @param context + * @param filePath + * @param destinationDirectoryPath + * @param destinationPath + * @return + */ + public static File compressPic(Context context, String filePath, String destinationDirectoryPath, String destinationPath) { BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeFile(filePath, options); @@ -227,7 +240,7 @@ public static File compressPic(Context context, String filePath, String destinat .setQuality(70) .setCompressFormat(Bitmap.CompressFormat.JPEG) .setDestinationDirectoryPath(destinationDirectoryPath) - .compressToFile(new File(filePath), System.currentTimeMillis() + ".jpg"); + .compressToFile(new File(filePath), destinationPath); } catch (Exception e) { e.printStackTrace(); } diff --git a/app/src/main/java/com/renyu/androidimagelibrary/UploadImageManager.java b/app/src/main/java/com/renyu/androidimagelibrary/UploadImageManager.java index 84d40bf..1d94aac 100644 --- a/app/src/main/java/com/renyu/androidimagelibrary/UploadImageManager.java +++ b/app/src/main/java/com/renyu/androidimagelibrary/UploadImageManager.java @@ -63,10 +63,10 @@ public synchronized void addTask(String filePath, String url, String tag) { bean.setStatue(UploadTaskBean.UploadState.UPLOADING); // 剪裁图片 - File cropFile = Utils.compressPic(com.blankj.utilcode.util.Utils.getApp(), filePath, InitParams.CACHE_PATH); + File cropFile = Utils.compressPic(com.blankj.utilcode.util.Utils.getApp(), filePath, InitParams.CACHE_PATH, new File(filePath).getName()); HashMap fileHashMap = new HashMap<>(); - fileHashMap.put("fileData", cropFile); + fileHashMap.put("fileData", cropFile == null ? new File(filePath) : cropFile); String uploadValue = okHttpUtils.syncUpload(url, null, fileHashMap, null, (l, l1) -> { Log.d("UploadImageManager", "UploadImageManager " + l + " " + l1); // 上传每20%进度刷新一次,上传完成不进行修改以防止与后续成功的回调不一致 @@ -151,7 +151,7 @@ else if (beans.get(tag).getStatue() == UploadTaskBean.UploadState.UPLOADFAIL) { * * @param tag */ - public void cancelTask(String tag) { + public synchronized void cancelTask(String tag) { if (tasks.containsKey(tag)) { tasks.remove(tag).cancel(true); }