fix: 保存 Gif 时文件后缀为 .jpg

This commit is contained in:
HuanCheng65 2022-06-10 16:46:18 +08:00
parent c64d427c4d
commit 661bc9bbe5
No known key found for this signature in database
GPG Key ID: E9031EF91A805148
2 changed files with 62 additions and 3 deletions

View File

@ -12,6 +12,7 @@ import android.os.Build;
import android.os.Environment; import android.os.Environment;
import android.provider.DocumentsContract; import android.provider.DocumentsContract;
import android.provider.MediaStore; import android.provider.MediaStore;
import android.text.TextUtils;
import android.webkit.URLUtil; import android.webkit.URLUtil;
import com.huanchengfly.tieba.post.R; import com.huanchengfly.tieba.post.R;
@ -233,4 +234,16 @@ public class FileUtil {
} }
return false; return false;
} }
//修改文件扩展名
public static String changeFileExtension(String fileName, String newExtension) {
if (TextUtils.isEmpty(fileName)) {
return fileName;
}
int index = fileName.lastIndexOf(".");
if (index == -1) {
return fileName + newExtension;
}
return fileName.substring(0, index) + newExtension;
}
} }

View File

@ -1,6 +1,7 @@
package com.huanchengfly.tieba.post.utils; package com.huanchengfly.tieba.post.utils;
import static com.huanchengfly.tieba.post.utils.FileUtil.FILE_FOLDER; import static com.huanchengfly.tieba.post.utils.FileUtil.FILE_FOLDER;
import static com.huanchengfly.tieba.post.utils.FileUtil.changeFileExtension;
import android.annotation.SuppressLint; import android.annotation.SuppressLint;
import android.content.ContentResolver; import android.content.ContentResolver;
@ -87,6 +88,36 @@ public class ImageUtil {
public static final int LOAD_TYPE_ALWAYS_ROUND = 3; public static final int LOAD_TYPE_ALWAYS_ROUND = 3;
public static final String TAG = "ImageUtil"; public static final String TAG = "ImageUtil";
private static boolean isGifFile(File file) {
try {
return isGifFile(new FileInputStream(file));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return false;
}
private static boolean isGifFile(FileInputStream inputStream) {
try {
int[] flags = new int[5];
flags[0] = inputStream.read();
flags[1] = inputStream.read();
flags[2] = inputStream.read();
flags[3] = inputStream.read();
inputStream.skip(inputStream.available() - 1);
flags[4] = inputStream.read();
inputStream.close();
return flags[0] == 71 && flags[1] == 73 && flags[2] == 70 && flags[3] == 56 && flags[4] == 0x3B;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
public static File compressImage(Bitmap bitmap, File output, int maxSize) { public static File compressImage(Bitmap bitmap, File output, int maxSize) {
ByteArrayOutputStream baos = new ByteArrayOutputStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream();
int quality = 100; int quality = 100;
@ -235,6 +266,9 @@ public class ImageUtil {
} }
new DownloadAsyncTask(context, url, file -> { new DownloadAsyncTask(context, url, file -> {
String fileName = URLUtil.guessFileName(url, null, MimeType.JPEG.toString()); String fileName = URLUtil.guessFileName(url, null, MimeType.JPEG.toString());
if (isGifFile(file)) {
fileName = changeFileExtension(fileName, "gif");
}
String relativePath = Environment.DIRECTORY_PICTURES + File.separator + FILE_FOLDER; String relativePath = Environment.DIRECTORY_PICTURES + File.separator + FILE_FOLDER;
if (forShare) { if (forShare) {
relativePath += File.separator + "shareTemp"; relativePath += File.separator + "shareTemp";
@ -299,6 +333,7 @@ public class ImageUtil {
return; return;
} }
copyFile(file, destFile); copyFile(file, destFile);
checkGifFile(destFile);
if (!forShare) { if (!forShare) {
context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(new File(destFile.getPath())))); context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(new File(destFile.getPath()))));
Toast.makeText(context, context.getString(R.string.toast_photo_saved, destFile.getPath()), Toast.LENGTH_SHORT).show(); Toast.makeText(context, context.getString(R.string.toast_photo_saved, destFile.getPath()), Toast.LENGTH_SHORT).show();
@ -309,6 +344,17 @@ public class ImageUtil {
}).execute(); }).execute();
} }
private static void checkGifFile(File file) {
if (isGifFile(file)) {
File gifFile = new File(file.getParentFile(), FileUtil.changeFileExtension(file.getName(), "gif"));
if (gifFile.exists()) {
file.delete();
} else {
file.renameTo(gifFile);
}
}
}
@SuppressLint("StaticFieldLeak") @SuppressLint("StaticFieldLeak")
private static void downloadBelowQ(Context context, String url) { private static void downloadBelowQ(Context context, String url) {
downloadBelowQ(context, url, false, null); downloadBelowQ(context, url, false, null);
@ -554,9 +600,9 @@ public class ImageUtil {
} }
public static class DownloadAsyncTask extends AsyncTask<Void, Integer, File> { public static class DownloadAsyncTask extends AsyncTask<Void, Integer, File> {
private WeakReference<Context> contextWeakReference; private final WeakReference<Context> contextWeakReference;
private TaskCallback callback; private final TaskCallback callback;
private String url; private final String url;
public DownloadAsyncTask(Context context, String url, TaskCallback callback) { public DownloadAsyncTask(Context context, String url, TaskCallback callback) {
this.contextWeakReference = new WeakReference<>(context); this.contextWeakReference = new WeakReference<>(context);