如何在存储卡中保存GIF图像



我是一个新的android,我想通过android编程在sd卡中保存GIF图像。目前我已经从谷歌做了一些代码来保存GIF图像在sd卡。但是当我保存图像到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"/>

使用提供代码,并确保在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);
    }
}

最新更新