How to create GIF from multiple bitmaps of images in android

The main theme of my application is that the user has to select images from their device gallery and the selected images are converted to GIFs. ... I am converting these selected images to bitmaps and I am using this GIFEncoder.java file to convert the selected images to GIF and I have achieved this. when I check it in my folder the GIF was created, but when I open the GIF there was no animation, a black screen appeared.

Here is my MainActivity looks like this:

public class MainActivity extends AppCompatActivity {

private static final int SELECT_PHOTO = 102;

private FileOutputStream outStream;
ArrayList<Bitmap> bitmaps = new ArrayList<>();
private Button generateImageGIF, selectImages;

String BASE_PATH = Environment.getExternalStorageDirectory().toString() + File.separator + "ImagesToGif";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    File mydir = new File(BASE_PATH);
    if (!mydir.exists()) {
        mydir.mkdirs();
    }

    generateImageGIF = (Button) findViewById(R.id.generate_image_gif);
    selectImages = (Button) findViewById(R.id.select_images);

    selectImages.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
            photoPickerIntent.setType("image/*");
            startActivityForResult(photoPickerIntent, SELECT_PHOTO);
        }
    });

    generateImageGIF.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            try {
                Toast.makeText(getApplicationContext(), "gif creation started", Toast.LENGTH_LONG).show();
                outStream = new FileOutputStream(BASE_PATH + File.separator + getString(R.string.app_name) + ".gif");
                outStream.write(generateGIF());
                outStream.close();
                Toast.makeText(getApplicationContext(), "gif creation ended", Toast.LENGTH_LONG).show();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}


@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == SELECT_PHOTO && resultCode == RESULT_OK && data != null) {
        Bitmap bitmap1 = BitmapFactory.decodeFile(String.valueOf(data.getData()));
        bitmaps.add(bitmap1);
    }
}

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

      

+3
android image bitmap gif


source to share


No one has answered this question yet

Check out similar questions:

3295
Why is the Android emulator so slow? How can we speed up the development of an Android emulator?
1338
How to vertically align an image inside a div
1264
How can I automatically resize the image to fit the container div?
1207
Strange memory issue when loading image into Bitmap object
879
How do I convert Drawable to Bitmap?
363
How to convert Bitmap to Drawable in Android?
2
setText on a button from another kind of android activity
1
Image cropping does not work in Android version 7.0 and later
1
Get a number from "Contacts" to "Edit"
-1
Android: viewing multiple images



All Articles
Loading...
X
Show
Funny
Dev
Pics