How do I implement the android gif encoder in my app?

3.2k Views Asked by At

I am trying to use this Android Gif Encoder https://github.com/nbadal/android-gif-encoder/blob/master/GifEncoder.java

I'm having errors trying to implement this in my android app.

the following errors are:

  1. outStream.write(generateGIF()); Description Resource Path Location Type The method generateGIF() is undefined for the type new View.OnClickListener(){} Main.java /f/src/net/s/f line 197 Java Problem

  2. public boolean finish() { Description Resource Path Location Type The return type is incompatible with Activity.finish() AnimatedGifEncoder.java /f/src/net/s/f line 164 Java Problem

  3. public byte[] onClick(View v) { Description Resource Path Location Type The return type is incompatible with View.OnClickListener.onClick(View) Main.java /f/src/net/s/f line 180 Java Problem

my code is below:

save.setOnClickListener(new OnClickListener() {

@Override
public byte[] onClick(View v) {
    // TODO Auto-generated method stub              


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

    Toast.makeText(Main.this, "Saved.", Toast.LENGTH_SHORT).show();

}
});

///////////////////////////////////////////////////////////////

To use this class, here is an example helper method to generate GIF byte array. Note here the getBitmapArray() function is a method to return all the Bitmap files in an image adapter at once. So the input is all the Bitmap files in one adapter, the output is a byte array which you can write to the file.

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

To use this function, do the following then you can save the file into SDcard.

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

There are 2 best solutions below

0
On

On click() return type must be void.

0
On

If you want to save the gif directly in a file, your onClick() method should looks like that:

@Override
public void onClick(View v) {
    ArrayList<Bitmap> bitmaps = adapter.getBitmapArray();
    File outputFile = new File("/sdcard/generate_gif/test.gif");
    FileOutputStream fos = null;
    try {
        fos = new FileOutputStream(outputFile);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

    if (fos != null) {
        AnimatedGifEncoder gifEncoder = new AnimatedGifEncoder();
        gifEncoder.start(fos);

        for (Bitmap bitmap : bitmaps) {
            gifEncoder.addFrame(bitmap);
        }

        gifEncoder.finish();
    }

    Toast.makeText(Main.this, "Saved.", Toast.LENGTH_SHORT).show();
}