2016-09-30 62 views
0

我是新來的Android和我採取的圖片庫的應用程序。我的問題是,我想在圖庫中保存GIF圖像。我已經完成了在圖庫中保存圖像,但是當我在圖庫中保存GIF圖像時,它將顯示正常圖像而不是GIF圖像。我正在使用glide庫在imageview中顯示GIF圖像。無論如何要以編程方式在畫廊中保存GIF圖像嗎?

這裏這是我的代碼來保存圖像畫廊

save.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 

      drawable = getResources().getDrawable(imageRes); 

      bitmap = ((BitmapDrawable)drawable).getBitmap(); 

      ImagePath = MediaStore.Images.Media.insertImage(
        getContentResolver(), 
        bitmap, 
        "imageRes","imageRes" 
      ); 

      URI = Uri.parse(ImagePath); 

      Context context = getApplicationContext(); 

      // Create layout inflator object to inflate toast.xml file 
      LayoutInflater inflater = getLayoutInflater(); 

      // Call toast.xml file for toast layout 
      View toastRoot = inflater.inflate(R.layout.layout_toast3, null); 

      Toast toast = new Toast(context); 

      // Set layout to toast 
      toast.setView(toastRoot); 
      toast.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL, 
        0,0); 
      toast.setDuration(Toast.LENGTH_LONG); 
      toast.show(); 
     } 
    }); 

Imageadapter

public class ImageAdapter extends BaseAdapter { 

static WallpaperInfo info; 
private Context mContext; 

public ImageAdapter() { 


} 

public int getCount() { 
    return mThumbIds.length; 
} 
public Object getItem(int position) { 
    return mThumbIds[position]; 
} 
public long getItemId(int position) { 
    return 0; 
} 
public ImageAdapter(Context c) { 
    mContext = c; 
} 

public View getView(final int position, View convertView, ViewGroup parent) { 
    ImageView imageView; 
    if (convertView == null){ 
     imageView = new ImageView(mContext); 
     imageView.setLayoutParams(new GridView.LayoutParams(200, 200)); 
     imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); 
     imageView.setPadding(3, 3, 3, 3); 
     imageView.setMaxHeight(300); 
     imageView.setMaxWidth(300); 
     imageView.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       MyPreferenceActivity myPref = new MyPreferenceActivity(mContext); 
       myPref.setGifImage(position); 


       Intent intent = new Intent(mContext, FullScreenImage.class); 
       intent.putExtra("imageID", mThumbIds[position]); 
       /*intent.putExtra(EXTRA_LIVE_WALLPAPER_INTENT, intent); 
       intent.putExtra(EXTRA_LIVE_WALLPAPER_SETTINGS, info.getSettingsActivity()); 
       intent.putExtra(EXTRA_LIVE_WALLPAPER_PACKAGE, info.getPackageName());*/ 
       mContext.startActivity(intent); 
      } 
     }); 

     Animation anim = AnimationUtils.loadAnimation(mContext.getApplicationContext(), R.anim.fly); 
     imageView.setAnimation(anim); 
     anim.start(); 

    } 
    else{ 
     imageView = (ImageView) convertView; 
    } 
    imageView.setImageResource(mThumbIds[position]); 
    return imageView; 
} 
public Integer[] mThumbIds = { 
     R.drawable.gpp1, R.drawable.gpp2, 
     R.drawable.gpp3,R.drawable.gpp4, 
     R.drawable.gpp5,R.drawable.gpp6, 
     R.drawable.gpp7,R.mipmap.h8, 
     R.mipmap.h9,R.mipmap.h10, 
     R.mipmap.h11,R.drawable.gp3, 
     R.drawable.gp2,R.drawable.gp, 
     R.drawable.onehalloween 
}; 
} 

所以,我該怎麼辦我的應用程序這種類型的功能?

+0

*但是當我保存GIF * ...用'MediaStore .Images.Media.insertImage( getContentResolver(), 位圖, 「imageRes」, 「imageRes」 );'你不保存GIF – Selvin

+0

@Selvin所以,我怎麼能做到這一點 –

+2

你檢查[這](http://stackoverflow.com/a/14641585/2591002)和[這](http://stackoverflow.com/a/23126885/2591002)? –

回答

0

您可以使用此GIF Encoder使用生成圖像:

public byte[] generateGIF() { 
    ArrayList<Bitmap> bitmaps = adapter.getBitmapArray(); 
    ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
    AnimatedGifEncoder encoder = new AnimatedGifEncoder(); 
    encoder.start(bos); 
    for (Bitmap bitmap : bitmaps) { 
     encoder.addFrame(bitmap); 
    } 
    encoder.finish(); 
    return bos.toByteArray(); 
} 

然後你就可以使用保存:

FileOutputStream outStream = null; 
    try{ 
     outStream = new FileOutputStream("/sdcard/generate_gif/test.gif"); 
     outStream.write(generateGIF()); 
     outStream.close(); 
    }catch(Exception e){ 
     e.printStackTrace(); 
    } 
+0

看到我imageadapter我能做什麼? –

+0

不能按我的要求工作,顯示正常圖像 –

+0

驗證保存的圖像是否爲gif並且是否有效。 –

相關問題