7

i want to create a custom gallery to display all images and videos(along with duration) in sdcard. i am using the following code to build a custom gallery

Code:

final String[] columns = { MediaStore.Images.Media.DATA ,MediaStore.Images.Media._ID};
    final String orderBy = MediaStore.Images.Media.DATE_TAKEN;
    Cursor imagecursor = getContentResolver().query(
            MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns, null,
            null, orderBy + " DESC");

    this.imageUrls = new ArrayList<String>();


    for (int i = 0; i < imagecursor.getCount(); i++) {
        imagecursor.moveToPosition(i);
        int dataColumnIndex = imagecursor.getColumnIndex(MediaStore.Images.Media.DATA);
        imageUrls.add(imagecursor.getString(dataColumnIndex));

    }
  String[] parameters = { MediaStore.Video.Media._ID,
                MediaStore.Video.Media.DATA,
                MediaStore.Video.Media.DISPLAY_NAME,
                MediaStore.Video.Media.SIZE, MediaStore.Video.Media.DURATION,
                MediaStore.Video.Media.DATE_TAKEN,MediaStore.Video.Thumbnails.DATA};



     Cursor   videocursor = getContentResolver().query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
                parameters, null, null, null);

     for (int i = 0; i < videocursor.getCount(); i++) {
         videocursor.moveToPosition(i);
         imageUrls.add(videocursor.getString(videocursor.getColumnIndex(MediaStore.Video.Thumbnails.DATA)));
            }
    options = new DisplayImageOptions.Builder()
        .showStubImage(R.drawable.stub_image)
        .showImageForEmptyUri(R.drawable.image_for_empty_url)
        .cacheInMemory()
        .cacheOnDisc()
        .build();

    imageAdapter = new ImageAdapter(this, imageUrls);

from the above code i am able to get the path of the video, how can i get the video thumbnail along with video duration. and represent it in the gallery

if there are any buit in projects for custom gallery with videos and images please post the links i actually want to create a custom gallery to select multiple image and video files. i searched a lot in google i am finding the custom image gallery but not with videos please help me in solving this problem.

madan V
  • 844
  • 3
  • 19
  • 40

4 Answers4

5

You can take idea from Custom GridView with multiple option selection option. There is an open source project in Github.

https://github.com/paramvir-b/AndroidGridViewCompatLib

enter image description here

in this example you need to change

 imageView.setImageResource(mThumbIds[position]);

to

imageView.setImageURI(uri);// URI of Image from SD Card

or

 imageView.setImageBitmap(bitmap);

For Video:-

Video Thumbnail is in the form of Bitmap so you can show in ImageView.
private Bitmap bmThumbnail;
private ImageView videoview = null;
bmThumbnail = ThumbnailUtils.createVideoThumbnail(PATH_OF_THE_VIDEO,Thumbnails.MICRO_KIND);
videoview.setImageBitmap(bmThumbnail);

for getting Duration:-

String[] proj = { MediaStore.Video.Media.DATA ,MediaStore.Video.Media.DURATION};
Cursor cursor = managedQuery(contentUri, proj, null, null, null);
if (cursor == null)
    return null;
int column_index = cursor
        .getColumnIndexOrThrow(MediaStore.Video.Media.DATA);
int column_index_duration = cursor
        .getColumnIndexOrThrow(MediaStore.Video.Media.DURATION);
cursor.moveToFirst();
long duration= cursor.getInt(column_index_duration);
String videoPath= cursor.getString(column_index);
Amit Gupta
  • 8,914
  • 1
  • 25
  • 33
0

Getting Video Thumbnails and duration:

for(int ii = 0; ii < videocursor.getCount(); ii ++){
                videocursor.moveToPosition(ii);
                int id_v = videocursor.getInt(video_column_index);
                int datacolumn_v = videocursor.getColumnIndex(MediaStore.Video.Media.DATA);
                long duration = videocursor.getInt(video_column_duration);             // getting duration of the every videos

                String hms = String.format("%02d:%02d:%02d", TimeUnit.MILLISECONDS.toHours(duration), 
                        TimeUnit.MILLISECONDS.toMinutes(duration) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(duration)),
                        TimeUnit.MILLISECONDS.toSeconds(duration) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(duration)));

                durationcount.add(hms);

                bitList.add(MediaStore.Video.Thumbnails.getThumbnail(getContentResolver(), id_v,
                        MediaStore.Video.Thumbnails.MICRO_KIND, null));
                arrPathList.add(videocursor.getString(datacolumn_v));
            }
siva
  • 375
  • 6
  • 24
0

Try this link Android custom image gallery with checkbox in grid to select multiple items

http://vikaskanani.wordpress.com/2011/07/20/android-custom-image-gallery-with-checkbox-in-grid-to-select-multiple/

  • 1
    Even if you include a useful link, you should comment in your answer the most important parts of it and how do it work, and include the link as a complement. – nKn Jan 23 '14 at 15:28
0
 int column_index_thumb_data = videoCursor.getColumnIndex(MediaStore.Video.Thumbnails.DATA);

 String thumbnail = videoCursor.getString(column_index_thumb_data);
Siddhivinayak
  • 1,103
  • 1
  • 15
  • 26
bidhu
  • 16
  • 1
  • 2
    Can you explain why your solution works? For future readers it's important to know why something works rather than copy pasting whithout having a clue – Imran Ali Khan Sep 22 '17 at 11:26