我希望我的应用程序将 GIF 图像类型文件保存到图片目录中的设备内部存储中。我已经学会了如何以这种方式保存 JPEG 和 PNG 图像文件,但是我不知道如何像下面的代码保存 JPEG 和 PNG 文件那样保存 GIF 图像文件。
private void saveImageToGallery(String setFolderName, String setFileName, String fileTypeExt, int imageQuality) {
BitmapDrawable bitmapDrawable = (BitmapDrawable) imageView.getDrawable();
Bitmap bitmap = bitmapDrawable.getBitmap();
FileOutputStream outputStream = null;
File file = Environment.getExternalStorageDirectory();
File dir = new File(file.getAbsolutePath() + "/" + setFolderName);
dir.mkdir();
String fileName = String.format(setFileName, System.currentTimeMillis());
File out = new File(dir, fileName);
if (out.exists()) {
file.delete();
Toast.makeText(getApplicationContext(), "Image Re-Saving...", Toast.LENGTH_SHORT).show();
}
try {
outputStream = new FileOutputStream(out);
if (fileTypeExt.equals("PNG") || fileTypeExt.equals("png") || fileTypeExt.equals(".PNG") || fileTypeExt.equals(".png")) {
bitmap.compress(Bitmap.CompressFormat.PNG, imageQuality, outputStream);
Toast.makeText(getApplicationContext(), "Image Saved To Gallery!", Toast.LENGTH_SHORT).show();
}else if (fileTypeExt.equals("JPEG") || fileTypeExt.equals("jpeg") || fileTypeExt.equals(".JPEG") || fileTypeExt.equals(".jpeg")) {
bitmap.compress(Bitmap.CompressFormat.JPEG, imageQuality, outputStream);
Toast.makeText(getApplicationContext(), "Image Saved To Gallery!", Toast.LENGTH_SHORT).show();
}
}catch (Exception e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
}
try {
outputStream.flush();
}catch (Exception e) {
e.printStackTrace();
}
try {
outputStream.close();
}catch (Exception e) {
e.printStackTrace();
}
}
大家都知道,我没有在我的 XML 文件中使用常规的 ImageView 来显示 GIF 图像。我找到了 GIFViewer 的依赖项。我的 build.gradle 中有这个依赖项
implementation 'com.github.Cutta:GifView:1.4'
这是我的 XML GIFViewer
<com.cunoraz.gifview.library.GifView
android:id="@+id/gifViewer"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:layout_below="@id/share_image_btn" android:layout_centerInParent="true"
app:gif="@drawable/link_triforce"/>
谢谢,我很感激我能得到的任何帮助!