简体   繁体   English

如何将 GIF 图像保存到图库?

[英]How Can I Save GIF Image To Gallery?

I would like my app to save a GIF image type file to the devices internal storage in the Pictures directory.我希望我的应用程序将 GIF 图像类型文件保存到图片目录中的设备内部存储中。 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.我已经学会了如何以这种方式保存 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();
    }
}

Just so that you all know, I am not using a regular ImageView in my XML file to display the GIF image.只是为了让大家知道,我没有在我的 XML 文件中使用常规的 ImageView 来显示 GIF 图像。 I found a dependency for a GIFViewer.我找到了 GIFViewer 的依赖项。 I have this dependency in my build.gradle我的 build.gradle 中有这个依赖项

implementation 'com.github.Cutta:GifView:1.4'

Here is my XML GIFViewer这是我的 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!谢谢,我很感激我能得到的任何帮助!

Thanks to March3April4's comment I was able to get it.感谢 March3April4 的评论,我能够得到它。 I re-wrote it to my liking.我根据自己的喜好重新编写了它。 Just in case if this helps anyone else, here you go!以防万一,如果这对其他人有帮助,那就去吧! Thanks again to March3April4!!!再次感谢 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.注意:确保在项目的 res 文件夹中创建一个名为“raw”的新文件夹,如果该文件夹不存在的话。 Place your gif file in the raw folder.将您的 gif 文件放在 raw 文件夹中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM