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
Related
I know how to retrieve a user profile from the ContentResolver. If I have a bitmap, how can I set it as a user profile picture (replace it OR set it, if none exists)?
I load the user profile like following:
Uri dataUri = Uri.withAppendedPath(ContactsContract.Profile.CONTENT_URI, ContactsContract.Contacts.Data.CONTENT_DIRECTORY);
String[] selection = new String[]
{
ContactsContract.Profile._ID,
ContactsContract.Profile.DISPLAY_NAME,
ContactsContract.Profile.PHOTO_URI,
ContactsContract.Profile.LOOKUP_KEY
};
Cursor cursor = MainApp.get().getContentResolver().query(
dataUri,
selection,
null,
null,
null);
if (cursor != null)
{
int id = cursor.getColumnIndex(ContactsContract.Profile._ID);
int name = cursor.getColumnIndex(ContactsContract.Profile.DISPLAY_NAME);
int photoUri = cursor.getColumnIndex(ContactsContract.Profile.PHOTO_URI);
int lookupKey = cursor.getColumnIndex(ContactsContract.Profile.LOOKUP_KEY);
try
{
if (cursor.moveToFirst())
{
int phId = cursor.getInt(id);
mName = cursor.getString(name);
mImageUri = cursor.getString(photoUri);
mLookupKey = cursor.getString(lookupKey);
mExists = true;
}
}
finally
{
cursor.close();
}
}
Here's how to update or create profile images, actually it's working the same way as updating normal contact pictures. I had a problem somewhere else...
Instead of using my UserProfile, just exchange them and hand on the raw id.
private static void updatePhoto(UserProfile profile, Bitmap bitmap, ...)
{
byte[] photo = ImageUtil.convertImageToByteArray(bitmap, true);
ContentValues values = new ContentValues();
int photoRow = -1;
String where = ContactsContract.Data.RAW_CONTACT_ID + " = " + profile.getRawId() + " AND " + ContactsContract.Data.MIMETYPE + "=='" + ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE + "'";
Cursor cursor = MainApp.get().getContentResolver().query(ContactsContract.Data.CONTENT_URI, null, where, null, null);
int idIdx = cursor.getColumnIndexOrThrow(ContactsContract.Data._ID);
if (cursor.moveToFirst()) {
photoRow = cursor.getInt(idIdx);
}
cursor.close();
values.put(ContactsContract.Data.RAW_CONTACT_ID, profile.getRawId());
values.put(ContactsContract.Data.IS_SUPER_PRIMARY, 1);
values.put(ContactsContract.CommonDataKinds.Photo.PHOTO, photo);
values.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE);
if (photoRow >= 0) {
MainApp.get().getContentResolver().update(ContactsContract.Data.CONTENT_URI, values, ContactsContract.Data._ID + " = " + photoRow, null);
} else {
MainApp.get().getContentResolver().insert(ContactsContract.Data.CONTENT_URI, values);
}
...
}
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();
}
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);
}
}
I am opening a folder onClick and sending images thro FTP. But when the size of image is more than 2MB i face an error in transmission. So I want open the folder if it has images with size less than 1MB else it will toast a message to check. Is this scenario possible as am new to android please help me.
protected void LoadGalleryImages() {
final String[] columns = { MediaStore.Images.Media.DATA,
MediaStore.Images.Media._ID };
final String orderBy = MediaStore.Images.Media._ID;
String foldername=sharedpre.getString("FolderName", "");
imagecursor=managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns,MediaStore.Images.Media.DATA + " like ? ",
new String[] {"%/"+foldername+"/%"},null);
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 dataColumnIndex = imagecursor
.getColumnIndex(MediaStore.Images.Media.DATA);
arrPath[i] = imagecursor.getString(dataColumnIndex);
}
you can use MediaStore.Images.Media.SIZE in your managequery and filter it for particular size, so can get only those images which you required according to your requirement..
EDIT :
protected void LoadGalleryImages() {
final String[] columns = { MediaStore.Images.Media.DATA,
MediaStore.Images.Media._ID };
final String orderBy = MediaStore.Images.Media._ID;
String foldername=sharedpre.getString("FolderName", "");
imagecursor=managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns,MediaStore.Images.Media.DATA + " like ? ",
new String[] {"%/"+foldername+"/%"},null);
int image_column_index = 0;
if (imagecursor != null) {
String[] sizecolumns = { MediaStore.Images.Media.DATA, MediaStore.Images.Media.SIZE };
Cursor imgcursor = managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, sizecolumns,MediaStore.Images.Media.SIZE + " < ? ", new String[] {"1024"},null);
if(imgcursor !=null) {
// Your Code
}
}
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 dataColumnIndex = imagecursor
.getColumnIndex(MediaStore.Images.Media.DATA);
arrPath[i] = imagecursor.getString(dataColumnIndex);
}
With
File [] files = %FOLDER%.listFiles();
you get the files of a directory. %FOLDER% is your directory you want to check. Just iterate over these files and return false if one of them is bigger than one MB.
if(files.length > 1048576){
return false;
}
Put it in a method, pass the folder as argument and that is it.
After taking a photo by using intent ACTION_IMAGE_CAPTURE with given location to save photo on some devices (my test device is Sony Ericsson Mt11i with android 2.3) default camera app is saving a duplicate image in camera photos folder.
So my app checks if the photo is duplicated and deletes it. But the camera app also saving the thumbnail for taken photo, so i'm deleting it too, but even after I delete this thumbnail there is somewhere still information about this thumbnail, and in device's gallery app image is still present with default image. If I check details of this image there is still information of this photo (date,location). These are my methods for handling duplicated photo:
public static void handleTakenPicture(Context context, int lastId, File tempFile) {
/*
* Checking for duplicate images
* This is necessary because some camera implementation not only save where you want them to save but also in their default location.
*/
if (lastId == 0)
return;
final String[] projection = {MediaStore.Images.ImageColumns.DATA, MediaStore.Images.ImageColumns.DATE_TAKEN,
MediaStore.Images.ImageColumns.SIZE, MediaStore.Images.ImageColumns._ID};
final String imageOrderBy = MediaStore.Images.Media._ID + " DESC";
final String imageWhere = MediaStore.Images.Media._ID + ">?";
final String[] imageArguments = {Integer.toString(lastId)};
Cursor imageCursor = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection, imageWhere, imageArguments,
imageOrderBy);
// List<File> cameraTakenMediaFiles = new ArrayList<File>();
File gFile = null;
if (imageCursor.getCount() > 0) {
// while( imageCursor.moveToNext() ) {
imageCursor.moveToFirst();
// int id =
// imageCursor.getInt(imageCursor.getColumnIndex(MediaStore.Images.Media._ID));
String path = imageCursor.getString(imageCursor.getColumnIndex(MediaStore.Images.Media.DATA));
gFile = new File(path);
// Long takenTimeStamp =
// imageCursor.getLong(imageCursor.getColumnIndex(MediaStore.Images.Media.DATE_TAKEN));
// Long size =
// imageCursor.getLong(imageCursor.getColumnIndex(MediaStore.Images.Media.SIZE));
// cameraTakenMediaFiles.add(new File(path));
//}
}
imageCursor.close();
//File imageFile = new File(imageFilePath);
if (!tempFile.exists() && gFile != null) {
// was not saved where I wanted, but the camera saved one in the media folder
// try to copy over the one saved by the camera and then delete
try {
Prefsy.copyFile(gFile, tempFile);
gFile.delete();
} catch (IOException e) {
e.printStackTrace();
}
}
if(gFile != null)
{
gFile.delete();
try { // DELETING THUMBNAIL
Cursor th = context.getContentResolver().query(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, new String[] {MediaStore.Images.ImageColumns.DATA},
MediaStore.Images.Thumbnails.IMAGE_ID + " = ?", new String[] {"" + lastId}, MediaStore.Images.Thumbnails._ID + " DESC");
L(th.getCount()+"");
if (th.getCount() > 0)
{
th.moveToFirst();
L(th.getString(0));
L(new File(th.getString(0)).delete()+"");
L(context.getContentResolver().delete(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,
MediaStore.Images.Thumbnails.IMAGE_ID + " = ?", new String[] {"" + lastId})+"");
}
th.close();
} catch (Exception e) {
}
}
/*for( File cameraTakenFile : cameraTakenMediaFiles ) {
// delete the one duplicated
cameraTakenFile.delete();
}*/
}
public static int getLastImageId(Context context){
final String[] imageColumns = { MediaStore.Images.Media._ID };
final String imageOrderBy = MediaStore.Images.Media._ID+" DESC";
final String imageWhere = null;
final String[] imageArguments = null;
Cursor imageCursor = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, imageColumns, imageWhere, imageArguments, imageOrderBy);
if(imageCursor.moveToFirst()){
int id = imageCursor.getInt(imageCursor.getColumnIndex(MediaStore.Images.Media._ID));
imageCursor.close();
return id;
}else{
return 0;
}
}
L() is Log method