android – Converting a video file to gif-ThrowExceptions

Exception or error:

I’ve been trying to figure out how to convert a video file to a gif in android. I found a class online, the AnimatedGifEncoder class ( But it doesn’t really seem to work. I wrapped the video to gif conversion process in an AsnycTask. The code is shown below:

protected String doInBackground(File[] files) {
    MediaMetadataRetriever mmr = new MediaMetadataRetriever();
    try {
    } catch (Exception e){
        Log.i("VMPF", e.toString());
    //int framesRate = Integer.parseInt(mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_CAPTURE_FRAMERATE));
    int duration = Integer.parseInt(mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION));
    Log.i("gifduration","" + duration);
    //Log.i("giffps","" + framesRate);
    int millisPerframe = 1000/24;
    int timeAt = 0;
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    AnimatedGifEncoder encoder = new AnimatedGifEncoder();
    while (timeAt < duration){
        timeAt += millisPerframe;
        Log.i("wenfhluwhru", timeAt+"");
    Log.i("wenfhluwhru", "loop finished");
    FileOutputStream outStream = null;
    try {
        outStream = new FileOutputStream(path);
        Log.i("wenfhluwhru", "outstream closed");
        return path;
    } catch(Exception e) {
    return null;

Is there something wrong with my code (I assume they’re nothing wrong with the AnimatedGifEncoder class)? Also I thought about converting a file to a byte[] and just submitting the byte[] into the GifImageView via Glide but I’m willing to guess that the byte[] for a video is different from that of a gif file (if I’m wrong, please let me know)?

How to solve:

You can find frames at different times and create a animation drawable out of it

public void getGIF(){
    int counter = 0;
    Bitmap b = mmRetriever.getFrameAtTime(counter);
    while(counter < duration && b != null){
        frame = new BitmapDrawable(getResources(), b);
        animationDrawable.addFrame(frame, 50);
        counter += incrementor;
        b = mmRetriever.getFrameAtTime(counter);


you can use FFMPEG like this to convert video file to gif:

In your app’s build.gradle add

implementation 'nl.bravobit:android-ffmpeg:1.1.7'


        var name="sample"
        var inFIle = Environment
                        .DIRECTORY_DOWNLOADS).toString() + "/${name}.mp4"

        var gifFIle = Environment
                        .DIRECTORY_DOWNLOADS).toString() + "/${name}gif.gif"
        val cmd = arrayOf("-i", inFile, gifFIle)

        val ffmpeg = FFmpeg.getInstance(baseContext)

        if (ffmpeg.getInstance(baseContext).isSupported){

         ffmpeg.execute(cmd, object : ExecuteBinaryResponseHandler() {
            override fun onStart() {


            override fun onProgress(message: String) {


            override fun onFailure(message: String) {


            override fun onSuccess(message: String) {


            override fun onFinish() {


Leave a Reply

Your email address will not be published. Required fields are marked *