I would like my app to save a GIF image type file to the devices internal storage in the Pictures directory. I have already learned how to save JPEG and PNG image files this way, but I can't figure out how to save GIF image files like the way the code below saves JPEG and PNG files.
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();
}
}
Just so that you all know, I am not using a regular ImageView in my XML file to display the GIF image. I found a dependency for a GIFViewer. I have this dependency in my build.gradle
implementation 'com.github.Cutta:GifView:1.4'
Here is my 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"/>
Thanks, I appreciate any help I can get with this!