doing a query with MediaStore.Images.Media does not show all images whether it is querying internal content or external content
The results (the thumbnails and the location those thumbnails are retrieved from) are not the same across even mainstream devices. Devices with no SD card and only internal storage have different image results, Samsung devices have different kinds of results, Google devices have different kinds of results.
Specifically with this code that is used around stackoverflow as a solution,
final String[] columns = {MediaStore.Images.Thumbnails._ID};
final String orderBy = MediaStore.Images.Media._ID;
Cursor imagecursor = context.getContentResolver().query(
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(
context.getApplicationContext().getContentResolver(), id,
MediaStore.Images.Thumbnails.MINI_KIND, null);
images.add(imageItem);
}
imagecursor.close();
I look at the code and it seems sound, but I need a better solution because results vary, and on some devices I have no idea where the resulting thumbnails have been pulled from
As #danny117 pointed out to me, trying to get THUMBNAILS was a flawed assumption. One cannot rely on the existence of thumbnails for every image in the android system.
So I ultimately retrieve all images based on mime type, and use the Files MediaStore
String[] projection = {
MediaStore.Files.FileColumns._ID,
MediaStore.Files.FileColumns.DATA,
MediaStore.Files.FileColumns.DATE_ADDED,
MediaStore.Files.FileColumns.MEDIA_TYPE,
MediaStore.Files.FileColumns.MIME_TYPE,
MediaStore.Files.FileColumns.TITLE
};
// Return only video and image metadata.
String selection = MediaStore.Files.FileColumns.MEDIA_TYPE + "="
+ MediaStore.Files.FileColumns.MEDIA_TYPE_IMAGE
+ " OR "
+ MediaStore.Files.FileColumns.MEDIA_TYPE + "="
+ MediaStore.Files.FileColumns.MEDIA_TYPE_VIDEO;
Uri queryUri = MediaStore.Files.getContentUri("external");
CursorLoader cursorLoader = new CursorLoader(
getActivity(),
queryUri,
projection,
selection,
null, // Selection args (none).
MediaStore.Files.FileColumns.DATE_ADDED + " DESC" // Sort order.
);
images.clear();
/*
final String[] columns = {MediaStore.Images.ImageColumns._ID, MediaStore.Images.ImageColumns.DATE_ADDED};
final String orderBy = MediaStore.Images.Media.DATE_ADDED + " DESC";
Cursor imagecursor = getActivity().getContentResolver().query(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns,
null, null, orderBy);
*/
Cursor imagecursor = cursorLoader.loadInBackground();
if (imagecursor != null) {
int image_column_index = imagecursor
.getColumnIndex(MediaStore.Files.FileColumns._ID);
int type_column_index = imagecursor.getColumnIndex(MediaStore.Files.FileColumns.MIME_TYPE);
int count = imagecursor.getCount();
for (int i = 0; i < count; i++) {
imagecursor.moveToPosition(i);
int id = imagecursor.getInt(image_column_index);
String mime_type = imagecursor.getString(type_column_index);
ImageItem imageItem = new ImageItem();
imageItem.id = id;
//lastId = id;
if(!mime_type.contains("video"))
imageItem.uriString = MediaStore.Images.Media.EXTERNAL_CONTENT_URI.withAppendedPath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, String.valueOf(id)).toString();
else
imageItem.uriString = MediaStore.Video.Media.EXTERNAL_CONTENT_URI.withAppendedPath(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, String.valueOf(id)).toString();
images.add(imageItem);
}
//add ImageItem at top of list
imagecursor.close();
}
Related
I need to get the uri/paths of all the images taken by the camera (gallery). How can I modify the code below to give me only the images from the gallery. The code below gives me images from other folders too.
public ArrayList<String> getImages()
{
ArrayList<String> paths = new ArrayList<String>();
final String[] columns = { MediaStore.Images.Media.DATA, MediaStore.Images.Media._ID };
final String orderBy = MediaStore.Images.Media.DATE_ADDED;
//Stores all the images from the gallery in Cursor
Cursor cursor = getContentResolver().query(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns, null,
null, orderBy);
//Total number of images
int count = cursor.getCount();
//Create an array to store path to all the images
String[] arrPath = new String[count];
for (int i = 0; i < count; i++) {
cursor.moveToPosition(i);
int dataColumnIndex = cursor.getColumnIndex(MediaStore.Images.Media.DATA);
//Store the path of the image
arrPath[i]= cursor.getString(dataColumnIndex);
paths.add(arrPath[i]);
}
return paths;
}
Just provide the selection argument in query
public ArrayList<String> getImages()
{
ArrayList<String> paths = new ArrayList<String>();
final String[] columns = { MediaStore.Images.Media.DATA, MediaStore.Images.Media._ID };
String selection = MediaStore.Images.Media.BUCKET_DISPLAY_NAME + " = ?";
String[] selectionArgs = new String[] {
"Camera"
};
final String orderBy = MediaStore.Images.Media.DATE_ADDED;
//Stores all the images from the gallery in Cursor
Cursor cursor = getContentResolver().query(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns, selection, selectionArgs, orderBy);
//Total number of images
int count = cursor.getCount();
//Create an array to store path to all the images
String[] arrPath = new String[count];
for (int i = 0; i < count; i++) {
cursor.moveToPosition(i);
int dataColumnIndex = cursor.getColumnIndex(MediaStore.Images.Media.DATA);
//Store the path of the image
arrPath[i]= cursor.getString(dataColumnIndex);
paths.add(arrPath[i]);
}
return paths;
}
Anyone know how to get a listing of all images that show up in the Android Gallery?
This query is only gets pictures taken locally on the phone. Does anyone know the URI where the Picasa image database is stored?? Appreciate the help.
private void getListOfAllPictures()
{
final String[] filePathColumn = { MediaStore.Images.Media._ID, MediaColumns.DATA, MediaColumns.DISPLAY_NAME, Images.Media.ORIENTATION, Images.Media.LATITUDE, Images.Media.LONGITUDE };
Cursor cursor = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
filePathColumn, null, null, null);
Vector<ImageDescriptor> imageDescriptors = new Vector<ImageDescriptor>();
if(cursor != null)
{
cursor.moveToFirst();
int IMG_ID_INDEX = cursor.getColumnIndex(MediaStore.Images.Media._ID);
int DATA_INDEX = cursor.getColumnIndex(MediaColumns.DATA);
int LATITUDE_INDEX = cursor.getColumnIndex(Images.Media.LATITUDE);
int LONGITUDE_INDEX = cursor.getColumnIndex(Images.Media.LONGITUDE);
int ORIENTATION_INDEX = cursor.getColumnIndex(Images.Media.ORIENTATION);
while(!cursor.isAfterLast())
{
//Blah Blah
cursor.moveToNext();
}
}
cursor.close();
Log.v(TAG, "Found " + imageDescriptors.size() + " images.");
}
ok its my code and it work for me it give all images which i can see in Android Gallery
just call this function from this line
getallimages(Environment.getExternalStorageDirectory());
and my function is below
private void getallimages(File dir)
{
String[] STAR = { "*" };
final String orderBy = MediaStore.Images.Media.DEFAULT_SORT_ORDER;
Cursor imagecursor = cntx.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, STAR, null, null, orderBy);
int image_column_index = imagecursor.getColumnIndex(MediaStore.Images.Media.DATA);
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.filePath = imagecursor.getString(imagecursor.getColumnIndex(MediaStore.Images.Media.DATA));
imageItem.id = id;
imageItem.selection = false; //newly added item will be selected by default
controller.images.add(imageItem);
}
}
is possible to load thumbnails with Universal image Loader for android?. i got Thumbnail of image and video file in Sdcard then i don't know how to display these thumbnails with using of this library in gridview so please tell me anyone know how to do?
i got thumbnail from this code:
protected Integer doInBackground(Integer... params) {
// TODO Auto-generated method stub
final String[] columns = { MediaStore.Images.Media.DATA, MediaStore.Images.Media._ID }; // Images getting
final String orderBy = MediaStore.Images.Media.DATE_TAKEN;
imagecursor = mContext.getContentResolver().query(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns, null,
null, orderBy);
int image_column_index = imagecursor.getColumnIndex(MediaStore.Images.Media._ID);
this.count = imagecursor.getCount();
/*this.count = params[0];
if(count == 0)
this.count = imagecursor.getCount();
else if(count >= 12)
*/
bitList = new ArrayList<Bitmap>();
arrPathList = new ArrayList<String>();
selectedPath = new ArrayList<String>();
for (int i = 0; i < this.count; i++) {
imagecursor.moveToPosition(i);
int id = imagecursor.getInt(image_column_index);
int dataColumnIndex = imagecursor.getColumnIndex(MediaStore.Images.Media.DATA);
bitList.add( MediaStore.Images.Thumbnails.getThumbnail(
mContext.getContentResolver(), id,
MediaStore.Images.Thumbnails.MICRO_KIND, null));
arrPathList.add(imagecursor.getString(dataColumnIndex));
}
this.durationcount = new ArrayList<String>(); // Video Getting
final String[] parameters = { MediaStore.Video.Media.DATA, MediaStore.Video.Media._ID, MediaStore.Video.Media.DURATION , MediaStore.Video.Media.MIME_TYPE}; // Videos getting
final String orderBy_v = MediaStore.Video.Media._ID;
videocursor = mContext.getContentResolver().query(
MediaStore.Video.Media.EXTERNAL_CONTENT_URI, parameters, null,
null, orderBy_v);
int video_column_index = videocursor.getColumnIndex(MediaStore.Video.Media._ID);
int video_column_duration = videocursor.getColumnIndexOrThrow(MediaStore.Video.VideoColumns.DURATION); // for duration of the video
totalCount = imagecursor.getCount() + videocursor.getCount(); /// Checking
durationcount_a = new String[imagecursor.getCount() + videocursor.getCount()];
for(int i = 0; i < videocursor.getCount(); i ++){
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);
durationcount_a[(imagecursor.getCount()) + ii] = hms;
bitList.add(MediaStore.Video.Thumbnails.getThumbnail(mContext.getContentResolver(), id_v,
MediaStore.Video.Thumbnails.MICRO_KIND, null));
arrPathList.add(videocursor.getString(datacolumn_v));
}
thumbnailsselection = new boolean[totalCount];
return null;
}
Thanks in advance.
u have to get the path or uri of the thumbnail to load it using universal image loader.
see this to get the uri :-
Get thumbnail Uri/path of the image stored in sd card + android
i have also worked on same kind of project and hosted it over git-hub .. i have two versions of it one without ImageLoader and other with imageloader .. right now i have hosted only former one :-
here is the path https://github.com/r4jiv007/CustomFilePicker.git
here is the method i used :-
private String getImageThumbnail(int id) {
final String thumb_DATA = MediaStore.Images.Thumbnails.DATA;
final String thumb_IMAGE_ID = MediaStore.Images.Thumbnails.IMAGE_ID;
Uri uri = thumbUri;
String[] projection = {thumb_DATA, thumb_IMAGE_ID};
String selection = thumb_IMAGE_ID + "=" + id + " AND " + MediaStore.Images.Thumbnails.KIND + "=" + MediaStore.Images.Thumbnails.MINI_KIND;
Cursor thumbCursor = getContentResolver().query(uri, projection, selection, null, null);
String thumbPath = null;
Bitmap thumbBitmap = null;
if (thumbCursor != null && thumbCursor.getCount() > 0) {
thumbCursor.moveToFirst();
int thCulumnIndex = thumbCursor.getColumnIndex(thumb_DATA);
thumbPath = thumbCursor.getString(thCulumnIndex);
/*
Toast.makeText(getApplicationContext(),
thumbPath,
Toast.LENGTH_LONG).show();*/
// thumbBitmap = BitmapFactory.decodeFile(thumbPath);
}
Log.i("ImageMiniKind", thumbPath + "");
return thumbPath;
}
and u have to use :-
imageLoader.displayImage("file://" + fileX.getmThumbPath() + "", imageView, options);
for loading the image.. and also beware some times there is no thumbnail for images !!
for loading thumbnail of video files
private String getVideoThumbnail(int id) {
final String thumb_DATA = MediaStore.Video.Thumbnails.DATA;
final String thumb_VIDEO_ID = MediaStore.Video.Thumbnails.VIDEO_ID;
Uri uri = MediaStore.Video.Thumbnails.EXTERNAL_CONTENT_URI;
String[] projection = {thumb_DATA, thumb_VIDEO_ID};
String selection = thumb_VIDEO_ID + "=" + id + " AND " + MediaStore.Video.Thumbnails.KIND + "=" + MediaStore.Video.Thumbnails.MINI_KIND;
Cursor thumbCursor = getContentResolver().query(uri, projection, selection, null, null);
String thumbPath = null;
// Bitmap thumbBitmap = null;
if (thumbCursor.moveToFirst()) {
int thCulumnIndex = thumbCursor.getColumnIndex(thumb_DATA);
thumbPath = thumbCursor.getString(thCulumnIndex);
}
return thumbPath;
}
now all you have to do is pass the path to imageloader library
Am working on showing images from gallery BELOW IS THE CODE.
HERE i want to open images in a specific folder which name will be given in a edit text.
Please help me in doing it as currently its opening all the images which is irrelevant to the application and hard for me to identify the images.
protected void LoadGalleryImages() {
final String[] columns = { MediaStore.Images.Media.DATA,
MediaStore.Images.Media._ID };
final String orderBy = MediaStore.Images.Media._ID;
imagecursor = managedQuery(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns, null,
null, orderBy);
int image_column_index = 0;
if (imagecursor != null) {
image_column_index = imagecursor
.getColumnIndex(MediaStore.Images.Media._ID);
count = imagecursor.getCount();
}
imgSelected = new String[count];
arrPath = new String[count];
thumbnailsselection = new boolean[count];
for (int i = 0; i < count; i++) {
if (imagecursor != null) {
imagecursor.moveToPosition(i);
// int id = imagecursor.getInt(image_column_index);
int dataColumnIndex = imagecursor
.getColumnIndex(MediaStore.Images.Media.DATA);
arrPath[i] = imagecursor.getString(dataColumnIndex);
}
}
}
Pooja,
Here is the code to open your specified folder to browse your files:
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
Uri uri = Uri.parse("folder path");
//Change content type as you want if you dont know then just mention "*/*"
intent.setDataAndType(uri, "text/csv");
startActivity(Intent.createChooser(intent, "Open folder"));
Hope this will help you.
I use the following code to get thumbnail and fill them in gridView.
I guess that the image's information such as BUCKET_DISPLAY_NAME,MediaStore.Images.Media.DATA and MediaStore.Images.Media._ID are stored in a database,
and the thumbnail of the image are stored in another database.
So if I want to delete a image, I need to delete three parts, the one is physical file, the two is the record of the image's information in a database, and the three is the recodr of thumbnail in another database, right?
The following code is only to delete two parts, the one is physical file, the two is the record of the image's information in a database. How can I delete the record of thumbnail? Thanks!
BTW, the code
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,
Uri.parse("file://" + Environment.getExternalStorageDirectory())));
is not very good, because it's asynchronous, I need to get the latest thumbnails at once after I delete a image.
And more, will
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,
Uri.parse("file://" + Environment.getExternalStorageDirectory())))
update both the image's information database and thumbnails database ?
private void FillImageAndThumb() {
this.count = curPublic.getCount();
this.thumbnails = new Bitmap[this.count];
this.arrPath = new String[this.count];
this.thumbnailsselection = new boolean[this.count];
this.myID=new int[this.count];
for (int i = 0; i < this.count; i++) {
curPublic.moveToPosition(i);
int id = curPublic.getInt(curPublic.getColumnIndex(MediaStore.Images.Media._ID));
int dataColumnIndex =curPublic.getColumnIndex(MediaStore.Images.Media.DATA);
myID[i]=id;
thumbnails[i] = MediaStore.Images.Thumbnails.getThumbnail(
getApplicationContext().getContentResolver(),
id,
MediaStore.Images.Thumbnails.MICRO_KIND,
null);
arrPath[i] = curPublic.getString(dataColumnIndex);
}
}
private void SetCurPublic(AdapterView<?> arg0,int index){
String[] projection = new String[]{
MediaStore.Images.Media._ID,
MediaStore.Images.Media.BUCKET_DISPLAY_NAME,
MediaStore.Images.Media.DATA
};
Uri images = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
if (index==0){
curPublic = getContentResolver().query(
images,
projection,
"",
null,
""
);
}else{
String s=arg0.getSelectedItem().toString();
curPublic= getContentResolver().query(
images,
projection,
MediaStore.Images.Media.BUCKET_DISPLAY_NAME+"=?",
new String[]{s},
"" // Ordering
);
}
}
public void DeleteSelected() {
for (int i = 0; i < thumbnailsselection.length; i++) {
if (thumbnailsselection[i]) {
File fdelete = new File(arrPath[i]);
if (fdelete.exists()) {
if (fdelete.delete()) {
Uri images = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
String mSelectionClause = MediaStore.Images.Media._ID
+ "=?";
String[] mSelectionArgs = { String.valueOf(myID[i]) };
getContentResolver().delete(
images,
mSelectionClause,
mSelectionArgs
);
}
}
}
}
}