Skip to content

Commit

Permalink
修复多图片上传文件名导致的bug
Browse files Browse the repository at this point in the history
  • Loading branch information
r17171709 committed Dec 6, 2020
1 parent 40d40ec commit 7e4485f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, File> 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%进度刷新一次,上传完成不进行修改以防止与后续成功的回调不一致
Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit 7e4485f

Please sign in to comment.