如何在SD卡中保存GIF图像? [英] How to save GIF image in sdcard?

查看:163
本文介绍了如何在SD卡中保存GIF图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am new android and I want to save GIF image in sdcard through android programming. Currently I had done some code from google to save GIF image in sdcard. But When I am saving that image to sdcard it will display normal image not GIF Image.

这是我显示GIF图片的代码

Here this is my code to display GIF image

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

        }
    });

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

So,What is the problem in above code.Please tell me.

推荐答案

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

I am not sure but as per your requirement you should first open your GIF and then after you convert into byte array then after you save it.i hope you will get your solution

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"/>

这篇关于如何在SD卡中保存GIF图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆