Custom Gallery with Images and Videos in android to select multiple items - android

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.

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
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);

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));
}

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/

int column_index_thumb_data = videoCursor.getColumnIndex(MediaStore.Video.Thumbnails.DATA);
String thumbnail = videoCursor.getString(column_index_thumb_data);

Related

How to auto load all images from phone gallery to image slider in Android?

I want to build a image slider that can shift images automatically with looping all images from phone's gallery. My android device is android 4.0.4. Can anyone help me out?
I have successfully built a image slider that can shift images automatically, but all the images are from the drawable resource that I created using Android Studio. I want the images come from gallery for better user interface.
My code is like this:
int images[] = {R.drawable.slide1, R.drawable.slide7, R.drawable.slide10};
It requires looping with array to do this, but I am a beginner of java.
1) content provider may help. like
private ArrayList<String> getImages(Context context) {
Uri uri;
Cursor cursor;
int column_index_data, column_index_folder_name;
ArrayList<String> listOfAllImages = new ArrayList<String>();
String absolutePathOfImage = null;
uri = android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
String[] projection = { MediaColumns.DATA,
MediaStore.Images.Media.BUCKET_DISPLAY_NAME };
cursor = context.getContentResolver().query(uri, projection, null,
null, null);
column_index_data = cursor.getColumnIndexOrThrow(MediaColumns.DATA);
column_index_folder_name = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.BUCKET_DISPLAY_NAME);
while (cursor.moveToNext()) {
absolutePathOfImage = cursor.getString(column_index_data);
listOfAllImages.add(absolutePathOfImage);
}
return listOfAllImages;
}
loading images from gallery with content provider
also look to the file ways as those are also useful.
listing files
loading images from file path

Display all the video from a specific folder in SD card in android

aI am having a problem in displaying all the videos inside a folder in SD card. Currently, I am able to display all the videos that can be found in SD card, but what I am trying to do is to display all the video inside the "PartyVideo" folder inside the SD card. Can you help me?Below is my code that display all the video in SD card.
final String[] columns = { MediaStore.Video.Media.DATA, MediaStore.Video.Media._ID };
final String orderBy = MediaStore.Video.Media.DATE_TAKEN;
Cursor imagecursor = managedQuery(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, columns, null,null, orderBy + " DESC");
int image_column_index = imagecursor.getColumnIndex(MediaStore.Video.Media._ID);
this.count = imagecursor.getCount();
this.thumbnails = new Bitmap[this.count];
this.arrPath = new String[this.count];
this.thumbnailsselection = new boolean[this.count];
for (int i = 0; i < this.count; i++)
{
imagecursor.moveToPosition(i);
int id = imagecursor.getInt(image_column_index);
int dataColumnIndex = imagecursor.getColumnIndex(MediaStore.Video.Media.DATA);
thumbnails[i] = MediaStore.Video.Thumbnails.getThumbnail(
getApplicationContext().getContentResolver(), id,
MediaStore.Video.Thumbnails.MICRO_KIND, null);
arrPath[i]= imagecursor.getString(dataColumnIndex);
}
GridView imagegrid = (GridView) findViewById(R.id.grid_GalleryImage);
imageAdapter = new ImageAdapter();
imagegrid.setAdapter(imageAdapter);
I tried many different code, and end up with this one. The code below gets all that video in the folder I want but I don't know how can I display it.
String[] fileList = null;
File videoFiles = new File(Environment.getExternalStorageDirectory()+"/PartyVideo");
if(videoFiles.isDirectory())
{
fileList=videoFiles.list();
}
for(int i=0;i<fileList.length;i++)
{
Log.e("Video:"+i+" File name",fileList[i]);
}
Use this File Browser library to browse through files and put the filter of video type in the following code
Intent intent = new Intent(getBaseContext(), FileDialog.class);
intent.putExtra(FileDialog.START_PATH, "/sdcard");
//can user select directories or not
intent.putExtra(FileDialog.CAN_SELECT_DIR, true);
//alternatively you can set file filter
intent.putExtra(FileDialog.FORMAT_FILTER, new String[] { "avi" });
startActivityForResult(intent, REQUEST_SAVE);
Just try this hope it will work for you.
http://android-er.blogspot.in/2011/05/display-video-thumbnail-in-listview.html
try this
Cursor videocursor = getActivity().getContentResolver().query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
columns,
MediaStore.Video.Media.DATA + " like ? ",
new String[]{"%/" + Your folder name + "/%"},
null);

Media Gallery ContentProvider

My app will display the complete list of images in my custom gallery .For this, I'm using ContentProvider of Image Thumbnails. Upon selecting the thumbnail's I need to display the actual image.According to my understanding Gallery's image do have same unique ID in Thumb and Media Table.
Here is the code. Firstly I queried Thumbnail's ContentProvider and saved URL and ID.
String pictureThumbTemp[] = { MediaStore.Images.Thumbnails._ID, MediaStore.Images.Thumbnails.DATA };
Cursor imagecursor = context.getContentResolver().query (MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,
pictureThumbTemp,null, null, null);
Later I am displaying thumbs in Grid.
Upon selection of thumbnail, I have to display original image. I'm trying to retrieve the original image like
String pictureImageTemp[] = { MediaStore.Images.Media._ID, MediaStore.Images.Media.DATA };
Cursor imagecursor = context.getContentResolver().query( MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
pictureImageTemp, MediaStore.Images.Media._ID + " = " + mediaID + "", null,
MediaStore.Images.Media._ID);
Overall, I'm showing thumbnail through it's url and upon click I'm querying the thumbnail's media ID in Original image table.
But it is returning a cursor with 0 results.
Please help me out.
Thanks,
sha.
I cracked out a way which worked out.
Retrieved the cursor for Original Images.
From that I pulled the ID for every image and Queried the Thumbnails for the ID which returns a cursor containing paths.
Find the code snippet below.
String pictureCols[] = { MediaStore.Images.Media._ID, MediaStore.Images.Media.DATA };
Cursor imagecursor = mContext.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, pictureCols,
null, null, null);
imagecursor.moveToFirst();
mImageUrls = new ArrayList<String>();
try {
// Iterate the cursor for Image urls
for (int index = 0; index < imagecursor.getCount(); index++) {
imagecursor.moveToPosition(index);
preparePicture(imagecursor);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
imagecursor.close();
}
}
Here is the code for preparePicture method
private void preparePicture(Cursor imageCursor) {
// get the ID for the original image
int idColumnIndex = imageCursor.getColumnIndex(mSelectedImage.mediaID);
Long id = imageCursor.getLong(idColumnIndex);
// Thumbnail image Cursor for this specific image.
String thumbCols[] = { MediaStore.Images.Thumbnails._ID, MediaStore.Images.Thumbnails.DATA };
Cursor thumbCursor = MediaStore.Images.Thumbnails.queryMiniThumbnail(mContext.getContentResolver(), id,
Thumbnails.MINI_KIND, thumbCols);
thumbCursor.moveToFirst();
// Save thumbnail URL in MediaInfo
dataColumnIndex = thumbCursor.getColumnIndex(mSelectedThumb.data);
String thumbURL = thumbCursor.getString(dataColumnIndex);
thumbCursor.close();
mImageUrls.add(url);
}
Finally, I will have all my Thumbnail urls in the ArrayList.
The same logic is not working for video thumbnails. Of course that is a different question :)
Regards,
Sha.
try this one
final String[] pictureImageTemp= { MediaStore.Images.Media.DATA, MediaStore.Images.Media._ID };
Cursor imagecursor = managedQuery(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, pictureImageTemp, null,
null, MediaStore.Images.Media._ID);

display all the images except from particular bucket display name in android

I am displaying all the sd card images in grid view.The thing is I have to display all the images which are there on external storage of the device and at the same time not to display the camera images.I know that for this I need to get either camera bucket id or bucket display name.I am not getting how to do this.So can anyone please help me how to do this..
Try this.You might need to modify this
public static ArrayList<String> getPathOfAllImages(Activity activity) {
ArrayList<String> absolutePathOfImageList = new ArrayList<String>();
String absolutePathOfImage = null;
String nameOfFile = null;
String absolutePathOfFileWithoutFileName = null;
Uri uri;
Cursor cursor;
int column_index;
int column_displayname;
int lastIndex;
uri = android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
String[] projection = { MediaColumns.DATA,
MediaColumns.DISPLAY_NAME };
cursor = activity.managedQuery(uri, projection, null, null, null);
column_index = cursor.getColumnIndexOrThrow(MediaColumns.DATA);
column_displayname = cursor
.getColumnIndexOrThrow(MediaColumns.DISPLAY_NAME);
while (cursor.moveToNext()) {
absolutePathOfImage = cursor.getString(column_index);
nameOfFile = cursor.getString(column_displayname);
lastIndex = absolutePathOfImage.lastIndexOf(nameOfFile);
lastIndex = lastIndex >= 0 ? lastIndex
: nameOfFile.length() - 1;
absolutePathOfFileWithoutFileName = absolutePathOfImage
.substring(0, lastIndex);
if (absolutePathOfImage != null&& !absolutePathOfImage.equals("/sdcard/DCMI/")) {
absolutePathOfImageList.add(absolutePathOfImage);
}
}
Use absolutePathOfImageList to populate your grid view
Usualy camera images are stored in a particular folder,DCMI folder.Just dont display images that are in the DCMI folder,skip that folder while you search for images in your sd card

Out of memory error due to large number of image thumbnails to display

The following codes were used to retrieve all photos for the purpose of displaying on gridview. However, if i have 1000 photos, it would result in out of memory error. Is there anyone who could help with this?
P.S. if anyone could, are u able to show the edited codes for the above to make use of lazy loading and caching? I'm pretty lost. The initialise method is used basically to set the ImageAdapter that will be used by gridview
Thanks!!
public void initialize() {
images.clear();
final String[] columns = { MediaStore.Images.Thumbnails._ID };
final String orderBy = MediaStore.Images.Media._ID;
#SuppressWarnings("deprecation")
Cursor imagecursor = managedQuery(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns,
null, null, orderBy);
if(imagecursor != null){
int image_column_index = imagecursor
.getColumnIndex(MediaStore.Images.Media._ID);
int count = imagecursor.getCount();
for (int i = 0; i < count; i++) {
imagecursor.moveToPosition(i);
int id = imagecursor.getInt(image_column_index);
ImageItem imageItem = new ImageItem();
imageItem.id = id;
lastId = id;
imageItem.img = MediaStore.Images.Thumbnails.getThumbnail(
getApplicationContext().getContentResolver(), id,
MediaStore.Images.Thumbnails.MICRO_KIND, null);
images.add(imageItem);
}
//imagecursor.close();
}
notifyDataSetChanged();
}
You're running out of memory because you're loading lots of bitmaps that take up lots of memory. The simple (and correct) answer is: don't load them all at once. Instead you should load each image in the adapter as necessary.

Categories

Resources