1

我希望我的应用程序将 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"/>

谢谢,我很感激我能得到的任何帮助!

4

1 回答 1

0

感谢 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();
        }
    }

按钮单击侦听器调用实例

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

注意:确保在项目的 res 文件夹中创建一个名为“raw”的新文件夹,如果该文件夹不存在的话。将您的 gif 文件放在 raw 文件夹中。

于 2021-03-05T12:17:03.850 回答