1

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!

1 Answers1

0

Thanks to March3April4's comment I was able to get it. I re-wrote it to my liking. Just in case if this helps anyone else, here you go! Thanks again to March3April4!!!

private void saveGIFImageToGallery(String setFolderName, String setFileName, int gifFile) {
        try {

            File file = Environment.getExternalStorageDirectory();
            File dir = new File(file.getAbsolutePath() + "/" + setFolderName);
            dir.mkdir();

            String fileName = String.format(setFileName + ".gif", System.currentTimeMillis());
            File outFile = new File(dir, fileName);

            if (outFile.exists()) {
                file.delete();
                Toast.makeText(getApplicationContext(), "GIF Re-Saving...", Toast.LENGTH_SHORT).show();
            }

            InputStream inputStream = getResources().openRawResource(gifFile);
            BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);

            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

            byte[] gif = new byte[1024];

            int current = 0;

            while ((current = bufferedInputStream.read()) != -1) {
                byteArrayOutputStream.write(current);
            }

            FileOutputStream fileOutputStream = new FileOutputStream(outFile);
            fileOutputStream.write(byteArrayOutputStream.toByteArray());

            fileOutputStream.flush();
            fileOutputStream.close();
            inputStream.close();

            Toast.makeText(getApplicationContext(), "GIF Saved To Gallery!", Toast.LENGTH_SHORT).show();

        }catch(Exception e) {
            e.printStackTrace();
        }
    }

Button Click Listener calling the instance

saveGIFImageToGallery("FolderName", "FileName", R.raw.my_gif);

Note: Make sure to create a new folder named "raw" in the res folder of your project if there isn't one that already exists. Place your gif file in the raw folder.