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
Related
I have a custom folder in the Pictures directory, like this Pictures/MyFolder. It has images in MyFolder. Here is how to share the images using ContentResolver on MyFolder only. Or is any alternative ways to share the image. It is on android 11.
I queried images using the following snippets
String[] projection = new String[]{
MediaStore.Images.Media.DATA,
MediaStore.Images.Media._ID,
MediaStore.Images.Media.BUCKET_DISPLAY_NAME,
MediaStore.Images.Media.DATE_TAKEN};
Uri images = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
final String orderBy = MediaStore.Images.Media.DATE_TAKEN;
Cursor cur = context.getContentResolver().query(images,
projection, // Which
// columns
// to return
null, // Which rows to return (all rows)
null, // Selection arguments (none)
orderBy + " DESC" // Ordering
);
int bucketColumn = cur.getColumnIndex(MediaStore.Images.Media.BUCKET_DISPLAY_NAME);
int dateColumn = cur.getColumnIndex(MediaStore.Images.Media.DATE_TAKEN);
do {
bucket = cur.getString(bucketColumn);
if (bucket.equals(context.getString(R.string.app_name))) {
date = cur.getString(dateColumn);
imagePath = listImageByFolder.get(bucket);
if (imagePath == null) {
imagePath = new ArrayList<String>();
}
imagePath.add(cur.getString(cur.getColumnIndex(MediaStore.Images.Media.DATA)));
Uri x = Uri.withAppendedPath(MediaStore.Images.Media.INTERNAL_CONTENT_URI, String.valueOf(cur.getString(cur.getColumnIndex(MediaStore.Images.Media.DATA))));
listImageByFolder.put(bucket, imagePath);
} ;
} while (cur.moveToNext());
While I try to share the image, Getting android.os.FileUriExposedException: file:///storage/emulated/0/Pictures/WhatsTouch/1628090801008.jpg exposed beyond app through ClipData.Item.getUri() exception. Is any way to correct it or is any alternative way?
I am getting audio files from specific folder and binding them on recyclerview. now i want Uri of each file so i can upload them one-by-one. i am using Cursor to get files. below is my code :
Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
String[] projection = {MediaStore.Audio.AudioColumns.SIZE,MediaStore.Audio.AudioColumns.TITLE,android.provider.MediaStore.Audio.AudioColumns.DATA,
MediaStore.Audio.AudioColumns.DURATION,MediaStore.Audio.AudioColumns.TITLE};
Cursor c = context.getContentResolver().query(uri, projection, MediaStore.Audio.Media.DATA + " like ? ", new String[]{"%/storage/emulated/0/Call%"},null);
if (c != null ) {
for (int i = 0; i < 50 && c.moveToNext(); ++i) {
// Create a model object.
POJO audioModel = new POJO();
long id = c.getLong(4);
String SIZE = c.getString(0); // Retrieve path.
String NAME = c.getColumnName(1); // Retrieve name.
String PATH = c.getString(2); // Retrieve album name.
String DURATION = c.getString(3); // Retrieve artist name.
Uri contentUri = ContentUris.withAppendedId(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,id);
audioModel.setSize(SIZE);
audioModel.setName(NAME);
audioModel.setPath(PATH);
audioModel.setDuration(DURATION);
audioModel.setUri(contentUri);
Log.d("#", String.valueOf(contentUri));
arrayListFile.add(audioModel);
}
c.close();
}
return arrayListFile;
in contentUri i am getting this ( content://media/external/audio/media/0 ) . and what i want is
content://com.android.providers.media.documents/document/xyz.mp3. how can i do ??
why do not use MediaStore.Files.FileColumns.DATA ? the path can be also used to upload to server.
Please consider using (MediaStore.Audio.Media.DATA must be part of your projection):
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
Uri yourFileUri = Uri.parse(cursor.getString(column_index));
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
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);
I am trying to get imagepath based on thumbail path , i have already tried solution from
android-getting-path-to-image-from-thumbnail, but it is based on gridview position, i am only retrieving specific images. Also i found one sample code from SO, the code is
private String getImagePathFromThumbPath(String thumbPath)
{
String imgPath=null;
// String[] projection = new String[] {MediaStore.Images.Thumbnails._ID, MediaStore.Images.Thumbnails.IMAGE_ID};
String[] imagesDataPath={ MediaStore.Images.Media.DATA ,MediaStore.Images.Media._ID};
//mResolver.query() requires android API 16
Cursor thumbnails = mResolver.query(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, imagesDataPath,MediaStore.Images.Thumbnails.DATA+"=?",new String[]{thumbPath}, null, null);
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor imageCursor = mResolver.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, filePathColumn, MediaStore.Images.Media._ID + "=?", new String[] {thumbId}, null);
if (imageCursor != null && imageCursor.moveToFirst()) {
// Your file-path will be here
imgPath= imageCursor.getString(imageCursor.getColumnIndex(filePathColumn[0]));
}
return imgPath;
}
The above method is bit modified to suit my needs and it returns nothing on Toasting, please tell how to retrieve imagepath using thumbnail path?
After a long time and relentless try, the solution is here
1. You need to find the image id which is image unique id from images table in thumbnail table, query to thumbnail provider (MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI) if you do not understand it, refer here , specifically IMAGE_ID,
Step 1 is to get retrievedImageId.
retrievedImageId = Long.parseLong(cursor.getString(imageIdInImages));
2. Now using the retrievedImageId get the image path, by again querying the content provider, only this time query the Images media provider (MediaStore.Images.Media.EXTERNAL_CONTENT_URI)
String getImagePathFromThumbPath(String thumbPath)
{
String imagePath = null;
if(thumbPath != null )
{
String[] columns_to_return = {MediaStore.Images.Thumbnails.IMAGE_ID};
String where = MediaStore.Images.Thumbnails.DATA+" LIKE ?";
long retrievedImageId = -1;
String valuesAre[] = {"%"+thumbPath};
Cursor cursor = getContentResolver().query(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, columns_to_return, where, valuesAre, null);
if(cursor != null)
{
int imageIdInImages = cursor.getColumnIndex(MediaStore.Images.Thumbnails.IMAGE_ID);
for (cursor.moveToFirst();!cursor.isAfterLast(); cursor.moveToNext())
{
//STEP 1 to retrieve image ID
retrievedImageId = Long.parseLong(cursor.getString(imageIdInImages));
}
if(retrievedImageId != -1)
{
// STEP 2 Now
Log.i(TAG, "imageId-" + retrievedImageId);
String[] columnsReturn = {MediaStore.Images.Media.DATA};
String whereimageId = MediaStore.Images.Media._ID+" LIKE ?";
String valuesIs[] = {"%" + retrievedImageId};
Cursor mCursor = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columnsReturn, whereimageId, valuesIs, null);
int rawDataPath= mCursor.getColumnIndex(MediaStore.Images.Media.DATA);
for (mCursor.moveToFirst();!mCursor.isAfterLast(); mCursor.moveToNext())
{
imagePath = mCursor.getString(rawDataPath);
}
}
}
}
return imagePath;
}
If you still have doubt or error/exception, leave comment!