2016-10-03 3 views
2

私は新しいアンドロイドです。アンドロイドプログラミングを通じて、sdcardにGIFイメージを保存したいと思います。現在、GoogleからGIFイメージをsdcardに保存するコードをいくつか作成していました。しかし、そのイメージをsdcardに保存すると、GIF Imageではなく通常イメージが表示されます。GIFイメージをsdcardに保存するには?

ここでは、これは何code.Please上記の問題点は、私に教えている、だから、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(); 
      } 

     } 
    }); 

を表示するための私のコードです。

+0

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

+0

は全くBitmapFactory使用しないでください。 AnimatedGiffEncoderは使用しないでください。あなたはすでに私が想定しているdrawablesのgifイメージを持っています。ファイルシステムにコピーするだけです。 – greenapps

+0

@greenappsあなたの方法では.jpg拡張子が表示され、画像は.gif –

答えて

5

あなたがit.iを保存した後、私はわからないが、あなたの要件ごとに、あなたが最初にあなたの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

それは私のために完全に働いています..ありがとう! –

関連する問題