【问题标题】:How to save GIF image in sdcard?如何将GIF图像保存在SD卡中?
【发布时间】:2018-12-19 23:48:34
【问题描述】:

我是新的 android,我想通过 android 编程将 GIF 图像保存在 sdcard 中。目前我已经从谷歌完成了一些代码来将 GIF 图像保存在 sdcard 中。但是当我将该图像保存到 sdcard 时,它将显示普通图像而不是 GIF 图像。

这是我显示 GIF 图片的代码

//Save code
    save.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Bitmap image = BitmapFactory.decodeResource(getResources(),
                    R.drawable.gpp3);
            File outputFile = new File("/sdcard/gpp3.gif");
            FileOutputStream fos = null;
            try {
                fos = new FileOutputStream(outputFile);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }

            if (fos != null) {
                AnimatedGifEncoder gifEncoder = new AnimatedGifEncoder();
                gifEncoder.start(fos);
                gifEncoder.addFrame(image);
                gifEncoder.finish();
            }

        }
    });

那么,上面的代码有什么问题。请告诉我。

【问题讨论】:

  • 完全不要使用 BitmapFactory。不要使用 AnimatedGiffEncoder。我想你已经在drawables中有一个gif图像。所以只需将其复制到文件系统即可。
  • @greenapps 当我使用你的方式时,它将显示 .jpg 扩展名,图像为 .gif
  • when i using your way it will display .jpg extension and image is .gif ???不可能的。那不是我的方式!因为是您决定文件名。当然,您不应该先转换为 Drawable 或 Bitmap。或使用 MediaStore。您还应该在此处发布您的代码。我在第一条评论中告诉你的是将字节数组从资源直接保存到文件中。从您的资源中打开 InputStream 并复制到 FileOutputStream。

标签: android


【解决方案1】:

我不确定,但根据您的要求,您应该先打开 GIF,然后转换为字节数组,然后保存。希望您能得到解决方案

private void saveGIF()
    {
        try
        {
            File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "shared_gif_shai" + System.currentTimeMillis() + ".gif");

            long startTime = System.currentTimeMillis();

            Log.d(TAG, "on do in background, url open connection");

            InputStream is = getResources().openRawResource(R.drawable.g);
            Log.d(TAG, "on do in background, url get input stream");
            BufferedInputStream bis = new BufferedInputStream(is);
            Log.d(TAG, "on do in background, create buffered input stream");

            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            Log.d(TAG, "on do in background, create buffered array output stream");

            byte[] img = new byte[1024];

            int current = 0;

            Log.d(TAG, "on do in background, write byte to baos");
            while ((current = bis.read()) != -1) {
                baos.write(current);
            }


            Log.d(TAG, "on do in background, done write");

            Log.d(TAG, "on do in background, create fos");
            FileOutputStream fos = new FileOutputStream(file);
            fos.write(baos.toByteArray());

            Log.d(TAG, "on do in background, write to fos");
            fos.flush();

            fos.close();
            is.close();
            Log.d(TAG, "on do in background, done write to fos");
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }

并且还要在你的 AndroidMenifest.xml 文件中授予此权限

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

【讨论】:

  • 它对我来说非常有用.. 非常感谢!
【解决方案2】:

使用提供的代码并确保在 meaifest 中添加 WRITE_EXTERNAL_STORAGE。

 protected void saveGifToInternalStorage(String fileName, URL url) {
    try {
        String PATH = Environment.getExternalStorageDirectory() + "/download/your_app_name";
        File file = new File(PATH);
        if(!file.exists()) {
            file.mkdirs();
        }

        long startTime = System.currentTimeMillis();
        Log.d(TAG, "download begining");
        Log.d(TAG, "download url:" + url);
        Log.d(TAG, "downloaded file name:" + fileName);
        /* Open a connection to that URL. */
        URLConnection ucon = url.openConnection();

        /*
         * Define InputStreams to read from the URLConnection.
         */
        InputStream is = ucon.getInputStream();
        BufferedInputStream bis = new BufferedInputStream(is);

        /*
         * Read bytes to the Buffer until there is nothing more to read(-1).
         */
        ByteArrayBuffer baf = new ByteArrayBuffer(50);
        int current = 0;
        while ((current = bis.read()) != -1) {
            baf.append((byte) current);
        }

        /* Convert the Bytes read to a String. */
        File outputFile = new File(file, fileName+".gif");
        FileOutputStream fos = new FileOutputStream(outputFile);
        fos.write(baf.toByteArray());
        fos.close();
        Log.d(TAG, "download ready in"
                + ((System.currentTimeMillis() - startTime) / 1000)
                + " sec");

    } catch (IOException e) {
        Log.d(TAG, "Error: " + e);
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-31
    相关资源
    最近更新 更多