title: How to Create a Video From List of Images in Android
body: <p>I already chose images from gallery however, after that what should I do, I can't understand. Which library should be used? And how to implement it? </p>
  
  <p>In my code below, How to get a <code>ArrayList&lt;File&gt;images</code> from gallery selection. I've used <code>Jcodec Library</code>. If any other solution is there then please suggest.</p>
  
  <p><strong>MainActivity.java:</strong></p>
  
  <pre><code>public class MainActivity extends Activity {
  
  GridView gridGallery;
  Handler handler;
  GalleryAdapter adapter;
  Button btnGalleryPick;
  Button btnGalleryPickMul;
  ViewSwitcher viewSwitcher;
  ImageLoader imageLoader;
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
  
      initImageLoader();
      init();
  }
  
  private void initImageLoader() {
      DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
              .cacheOnDisc().imageScaleType(ImageScaleType.EXACTLY_STRETCHED)
              .bitmapConfig(Bitmap.Config.RGB_565).build();
      ImageLoaderConfiguration.Builder builder = new ImageLoaderConfiguration.Builder(
              this).defaultDisplayImageOptions(defaultOptions).memoryCache(
              new WeakMemoryCache());
      ImageLoaderConfiguration config = builder.build();
      imageLoader = ImageLoader.getInstance();
      imageLoader.init(config);
  }
  
  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
      super.onActivityResult(requestCode, resultCode, data);
  
      if (requestCode == 200 &amp;&amp; resultCode == Activity.RESULT_OK) {
  
          String[] all_path = data.getStringArrayExtra("all_path");
  
          ArrayList&lt;CustomGallery&gt; dataT = new ArrayList&lt;CustomGallery&gt;();
  
          for (String string : all_path) {
              CustomGallery item = new CustomGallery();
              item.sdcardPath = string;
              dataT.add(item);
          }
  
          viewSwitcher.setDisplayedChild(0);
          adapter.addAll(dataT);
      }
  }
  
  private void init() {
  
      handler = new Handler();
      gridGallery = (GridView) findViewById(R.id.gridGallery);
      gridGallery.setFastScrollEnabled(true);
      adapter = new GalleryAdapter(getApplicationContext(), imageLoader);
      adapter.setMultiplePick(false);
      gridGallery.setAdapter(adapter);
  
      viewSwitcher = (ViewSwitcher) findViewById(R.id.viewSwitcher);
      viewSwitcher.setDisplayedChild(1);
  
      btnGalleryPick = (Button) findViewById(R.id.btnGalleryPick);
      btnGalleryPick.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              if (gridGallery.getChildCount() == 0) {
                  Toast.makeText(getApplicationContext(), "Please Select Photo First", Toast.LENGTH_SHORT).show();
              } else {
                  String location = String.valueOf(Environment.getExternalStorageDirectory());
                  File root = new File(Environment.getExternalStorageDirectory()
                          .getAbsolutePath() + "/Birthday/");
                  ArrayList&lt;File&gt; fileList = new ArrayList&lt;File&gt;();
                  File listFile[] = root.listFiles();
                  if (listFile != null &amp;&amp; listFile.length &gt; 0) {
                      for (int i = 0; i &lt; listFile.length; i++) {
                          /*if (listFile[i].isDirectory()) {
                              fileList.add(listFile[i]);
                              getfile(listFile[i]);
                          } else {*/
                          if (listFile[i].getName().endsWith(".png")
                                  || listFile[i].getName().endsWith(".jpg")
                                  || listFile[i].getName().endsWith(".jpeg")
                                  || listFile[i].getName().endsWith(".gif"))
                          {
                              fileList.add(listFile[i]);
                          }
                      }
                  }
                  softwareMakeVideo(fileList, location, "test");
              }
          }
      });
      btnGalleryPickMul = (Button) findViewById(R.id.btnGalleryPickMul);
      btnGalleryPickMul.setOnClickListener(new View.OnClickListener() {
  
          @Override
          public void onClick(View v) {
              Intent i = new Intent(Action.ACTION_MULTIPLE_PICK);
              startActivityForResult(i, 200);
          }
      });
  }
  public String softwareMakeVideo(ArrayList&lt;File&gt; images, String location,
                                  String name) {
      File directory = new File(location);
      if (!directory.exists()) {
          directory.mkdir();
      }
      File file = new File(directory, name + ".mp4");
      try {
          SequenceEncoder encoder = new SequenceEncoder(file);
          for (Iterator&lt;File&gt; iterator = images.iterator(); iterator.hasNext(); ) {
              File image = iterator.next();
              if (!image.exists() || image.length() == 0) {
                  continue;
              }
              Bitmap frame = BitmapFactory.decodeFile(image.getAbsolutePath());
              try {
                  encoder.encodeNativeFrame(this.fromBitmap(frame));
              } catch (Exception e) {
                  e.printStackTrace();
              }
          }
          encoder.finish();
      } catch (IOException e) {
          e.printStackTrace();
      }
      return file.getAbsolutePath();
  }
  public Picture fromBitmap(Bitmap src) {
      Picture dst = Picture.create(src.getWidth(), src.getHeight(), ColorSpace.RGB);
      fromBitmap(src, dst);
      return dst;
  }
  private void fromBitmap(Bitmap src, Picture dst) {
      int[] dstData = dst.getPlaneData(0);
      int[] packed = new int[src.getWidth() * src.getHeight()];
      src.getPixels(packed, 0, src.getWidth(), 0, 0, src.getWidth(), src.getHeight());
      for (int i = 0, srcOff = 0, dstOff = 0; i &lt; src.getHeight(); i++) {
          for (int j = 0; j &lt; src.getWidth(); j++, srcOff++, dstOff += 3) {
              int rgb = packed[srcOff];
              dstData[dstOff] = (rgb &gt;&gt; 16) &amp; 0xff;
              dstData[dstOff + 1] = (rgb &gt;&gt; 8) &amp; 0xff;
              dstData[dstOff + 2] = rgb &amp; 0xff;
          }
      }
  }
  }
  </code></pre>