fix: 修复 gif 文件保存扩展名为 jpg
This commit is contained in:
parent
8c07ef51ed
commit
89c0b286f2
|
|
@ -23,6 +23,7 @@ import android.os.ParcelFileDescriptor;
|
||||||
import android.provider.MediaStore;
|
import android.provider.MediaStore;
|
||||||
import android.text.TextUtils;
|
import android.text.TextUtils;
|
||||||
import android.util.Base64;
|
import android.util.Base64;
|
||||||
|
import android.util.Log;
|
||||||
import android.webkit.URLUtil;
|
import android.webkit.URLUtil;
|
||||||
import android.widget.ImageView;
|
import android.widget.ImageView;
|
||||||
import android.widget.Toast;
|
import android.widget.Toast;
|
||||||
|
|
@ -96,23 +97,16 @@ public class ImageUtil {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static boolean isGifFile(FileInputStream inputStream) {
|
//判断是否为GIF文件
|
||||||
|
private static boolean isGifFile(InputStream inputStream) {
|
||||||
|
byte[] bytes = new byte[4];
|
||||||
try {
|
try {
|
||||||
int[] flags = new int[5];
|
inputStream.read(bytes);
|
||||||
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();
|
inputStream.close();
|
||||||
return flags[0] == 71 && flags[1] == 73 && flags[2] == 70;
|
String str = new String(bytes);
|
||||||
} catch (FileNotFoundException e) {
|
return str.equalsIgnoreCase("GIF8");
|
||||||
e.printStackTrace();
|
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
@ -271,10 +265,13 @@ public class ImageUtil {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
new DownloadAsyncTask(context, url, file -> {
|
new DownloadAsyncTask(context, url, file -> {
|
||||||
|
String mimeType = MimeType.JPEG.toString();
|
||||||
String fileName = URLUtil.guessFileName(url, null, MimeType.JPEG.toString());
|
String fileName = URLUtil.guessFileName(url, null, MimeType.JPEG.toString());
|
||||||
if (isGifFile(file)) {
|
if (isGifFile(file)) {
|
||||||
fileName = changeFileExtension(fileName, "gif");
|
fileName = changeFileExtension(fileName, ".gif");
|
||||||
|
mimeType = MimeType.GIF.toString();
|
||||||
}
|
}
|
||||||
|
Log.i(TAG, "download: fileName = " + fileName);
|
||||||
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";
|
||||||
|
|
@ -282,7 +279,7 @@ public class ImageUtil {
|
||||||
ContentValues values = new ContentValues();
|
ContentValues values = new ContentValues();
|
||||||
values.put(MediaStore.Images.Media.RELATIVE_PATH, relativePath);
|
values.put(MediaStore.Images.Media.RELATIVE_PATH, relativePath);
|
||||||
values.put(MediaStore.Images.Media.DISPLAY_NAME, fileName);
|
values.put(MediaStore.Images.Media.DISPLAY_NAME, fileName);
|
||||||
values.put(MediaStore.Images.Media.MIME_TYPE, MimeType.JPEG.toString());
|
values.put(MediaStore.Images.Media.MIME_TYPE, mimeType);
|
||||||
values.put(MediaStore.Images.Media.DESCRIPTION, fileName);
|
values.put(MediaStore.Images.Media.DESCRIPTION, fileName);
|
||||||
Uri uri = null;
|
Uri uri = null;
|
||||||
ContentResolver cr = context.getContentResolver();
|
ContentResolver cr = context.getContentResolver();
|
||||||
|
|
@ -335,7 +332,7 @@ public class ImageUtil {
|
||||||
}
|
}
|
||||||
String fileName = URLUtil.guessFileName(url, null, MimeType.JPEG.toString());
|
String fileName = URLUtil.guessFileName(url, null, MimeType.JPEG.toString());
|
||||||
if (isGifFile(file)) {
|
if (isGifFile(file)) {
|
||||||
fileName = changeFileExtension(fileName, "gif");
|
fileName = changeFileExtension(fileName, ".gif");
|
||||||
}
|
}
|
||||||
File destFile = new File(appDir, fileName);
|
File destFile = new File(appDir, fileName);
|
||||||
if (destFile.exists()) {
|
if (destFile.exists()) {
|
||||||
|
|
@ -355,7 +352,7 @@ public class ImageUtil {
|
||||||
|
|
||||||
private static void checkGifFile(File file) {
|
private static void checkGifFile(File file) {
|
||||||
if (isGifFile(file)) {
|
if (isGifFile(file)) {
|
||||||
File gifFile = new File(file.getParentFile(), FileUtil.changeFileExtension(file.getName(), "gif"));
|
File gifFile = new File(file.getParentFile(), FileUtil.changeFileExtension(file.getName(), ".gif"));
|
||||||
if (gifFile.exists()) {
|
if (gifFile.exists()) {
|
||||||
file.delete();
|
file.delete();
|
||||||
} else {
|
} else {
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,3 @@
|
||||||
|
package com.huanchengfly.tieba.post.utils
|
||||||
|
|
||||||
|
object SAFUtils
|
||||||
Loading…
Reference in New Issue