2016-10-03 47 views
2

我是新的android,我想通過android編程在SD卡中保存GIF圖像。目前我已經從谷歌做了一些代碼來保存SD卡中的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(); 
      } 

     } 
    }); 

那麼,什麼是上述code.Please告訴我的問題。

+0

https://github.com/madmaw/animatedgifencoder/ –

+0

不要使用BitmapFactory可言。不要使用AnimatedGiffEncoder。假設你已經有了一個可繪製的gif圖像。所以把它複製到文件系統。 – greenapps

+0

@greenapps當我用你的方式它會顯示.jpg擴展名和圖像是.gif –

回答

5

我不知道,但根據您的要求,你應該先打開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"/> 
+0

謝謝..它的工作.. –

+0

它完全爲我工作..非常感謝你! –