Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
847 views
in Technique[技术] by (71.8m points)

android - How to save GIF image in sdcard?

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.

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

And also Give this permission in you AndroidMenifest.xml file

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...