微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

android – 如何在SD卡中保存GIF图像?

我是新的android,我想通过android编程保存sdcard中的gif图像.目前我已经从谷歌做了一些代码来保存SD卡中的gif图像.但是当我将该图像保存到SD卡时,它将显示正常图像而不是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();
            }

        }
    });

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

解决方法:

我不确定,但根据您的要求,您应首先打开您的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"/>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐