1

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?

In my code below, How to get a ArrayList<File>images from gallery selection. I've used Jcodec Library. If any other solution is there then please suggest.

MainActivity.java:

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 && resultCode == Activity.RESULT_OK) {

        String[] all_path = data.getStringArrayExtra("all_path");

        ArrayList<CustomGallery> dataT = new ArrayList<CustomGallery>();

        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<File> fileList = new ArrayList<File>();
                File listFile[] = root.listFiles();
                if (listFile != null && listFile.length > 0) {
                    for (int i = 0; i < 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<File> 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<File> 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 < src.getHeight(); i++) {
        for (int j = 0; j < src.getWidth(); j++, srcOff++, dstOff += 3) {
            int rgb = packed[srcOff];
            dstData[dstOff] = (rgb >> 16) & 0xff;
            dstData[dstOff + 1] = (rgb >> 8) & 0xff;
            dstData[dstOff + 2] = rgb & 0xff;
        }
    }
}
}
Shubham Namdeo
  • 1,845
  • 2
  • 24
  • 40
Rahul Sabhaya
  • 11
  • 1
  • 3

2 Answers2

0

Use this library : http://androidwarzone.blogspot.in/2011/12/ffmpeg4android.html

You will find also there.

Patel Jaimin
  • 231
  • 1
  • 11
0

I guess you are referring to the same problem as of below -

Convert set of images to a single video file in android

Try to follow the above link,hope you would get an answer.

Community
  • 1
  • 1
B.shruti
  • 1,589
  • 1
  • 21
  • 41