【问题标题】:Is there anyway to save GIF image in gallery programatically?无论如何以编程方式将GIF图像保存在画廊中?
【发布时间】:2017-03-02 12:56:13
【问题描述】:

我是 Android 新手,我正在实现图片库应用程序。我的问题是我想在画廊中保存 GIF 图像。我已经将图像保存在画廊中,但是当我在画廊中保存 GIF 图像时,它会显示普通图像而不是 GIF 图像。我正在使用 glide 库在 imageview 中显示 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();
        }
    });

图像适配器

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

那么,我怎样才能在我的应用中实现这种类型的功能呢?

【问题讨论】:

  • 但是当我保存 GIF 时 ...使用 MediaStore.Images.Media.insertImage( getContentResolver(), bitmap, "imageRes","imageRes" ); 你没有保存 GIF
  • @Selvin 那么,我该怎么做呢
  • 你检查过thisthis吗?
  • 你在哪里使用滑翔?我没看到。

标签: android


【解决方案1】:

您可以使用此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();
    }

【讨论】:

  • 查看我的图像适配器我能做什么??
猜你喜欢
  • 1970-01-01
  • 2014-01-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-23
  • 2010-11-27
相关资源
最近更新 更多