Why the cursor null? - android

First, I use below ode to get all image path.
And save to string array path.
String[] projection = {MediaStore.Images.Media._ID, MediaStore.Images.Media.DATA, MediaStore.Images.ImageColumns.DATA};
Cursor cursor = managedQuery( MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection, null, null, MediaStore.Images.Media._ID);
int count = cursor.getCount();
int image_column_index = cursor.getColumnIndex(MediaStore.Images.Media._ID);
int image_path_index = cursor.getColumnIndex(MediaStore.Images.Media.DATA);
int i;
for(i = 0; i < count; i++) {
cursor.moveToPosition(i);
int id = cursor.getInt(image_column_index);
pat[i[ = cursor.getString(image_path_index);
}
After finish, I try below code to get thumbnail.
int i;
for(i = 0; i < count; i++) {
String[] projection = {MediaStore.Images.Media._ID, MediaStore.Images.Media.DATA};
Cursor cursor = act.managedQuery( MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection, MediaStore.Images.Media.DATA + "=?", new String[] {path[i]}, MediaStore.Images.Media._ID); }
But while the file number very large(about 1000 files), the cursor show null.
I confirm it not cause by path name.
Any other reason?

The second try add cursor.close();
As below:
int i;
for(i = 0; i < count; i++) {
String[] projection = {MediaStore.Images.Media._ID, MediaStore.Images.Media.DATA};
Cursor cursor = act.managedQuery( MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection, MediaStore.Images.Media.DATA + "=?", new String[] {path[i]}, MediaStore.Images.Media._ID);
cursor.close();
}

Related

Query DCIM folder

public static ArrayList<String> listOfImages(Context context) {
Uri uri;
Cursor cursor;
final String[] columns = { MediaStore.Images.Media.DATA, MediaStore.Images.Media._ID };
final String orderBy = MediaStore.Images.Media._ID;
//Stores all the images from the gallery in Cursor
cursor = context.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
ArrayList<String> arrPath = new ArrayList<>();
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.add(cursor.getString(dataColumnIndex));
}
cursor.close();
return arrPath;
}
I just want to query only images from DCIM folder. How can I setup cursor query to do it?
This query will return all images from all folder in Android

get images from gallery only

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

Get all photos from Android device android programming

I am trying to fetch all the photos of my android device.
I have an onCreate function:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageAdapter adapter= new ImageAdapter(this);
final String[] columns = { MediaStore.Images.Media.DATA, MediaStore.Images.Media._ID };
final String orderBy = MediaStore.Images.Media._ID;
Uri rr= MediaStore.Images.Media.INTERNAL_CONTENT_URI;
Cursor imagecursor = getContentResolver().query(
MediaStore.Images.Media.INTERNAL_CONTENT_URI,null, null,
null, orderBy);
int image_column_index = imagecursor.getColumnIndex(MediaStore.Images.Media._ID);
this.count = imagecursor.getCount();
this.thumbnails = new Bitmap[this.count];
for (int i = 0; i < this.count; i++) {
imagecursor.moveToPosition(i);
int id = imagecursor.getInt(image_column_index);
thumbnails[i] = MediaStore.Images.Thumbnails.getThumbnail(
getApplicationContext().getContentResolver(), id,
MediaStore.Images.Thumbnails.MICRO_KIND, null);
adapter.mThumbIds[i]= thumbnails[i];
}
}
where the count returned on imageCursor.getCount() is returning 0.
Can someone please guide me since I am not able to fetch any media using this code?
Note: I am testing on Galaxy Note-2
First do not forget to
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
This method will return list of photo in your gallery
public static ArrayList<String> getImagesPath(Activity activity) {
Uri uri;
ArrayList<String> listOfAllImages = new ArrayList<String>();
Cursor cursor;
int column_index_data, column_index_folder_name;
String PathOfImage = null;
uri = android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
String[] projection = { MediaColumns.DATA,
MediaStore.Images.Media.BUCKET_DISPLAY_NAME };
cursor = activity.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()) {
PathOfImage = cursor.getString(column_index_data);
listOfAllImages.add(PathOfImage);
}
return listOfAllImages;
}
just give it a try even if your device has only internal storage no external storage,
try adding permissions:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
and read using
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI;

Get Listing Of All Photos (Even pictures stored online) - Picasa image database Uri?

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

Creating query to MediaStore for albums in folder?

Could anybody help me to make query to Android DB.
I need select all albums in selected folder.
For all songs I make this:
private Cursor allSongs() {
String[] projection = {MediaStore.Audio.Media._ID, // 0
MediaStore.Audio.Media.ARTIST, // 1
MediaStore.Audio.Media.TITLE, // 2
MediaStore.Audio.Media.ALBUM_ID, // 3
MediaStore.Audio.Media.ALBUM, // 4
MediaStore.Audio.Media.DATA, // 5
MediaStore.Audio.Media.DISPLAY_NAME, // 6
MediaStore.Audio.Media.DURATION, //7
MediaStore.Audio.Media.TITLE_KEY //8
};
String selection = android.provider.MediaStore.Audio.Media.DATA + " like ? ";
String MEDIA_PATH = Environment.getExternalStorageDirectory() + "/Music/";
Cursor musicListSDCardCursor = mQuery(
getActivity(),
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
projection, selection, new String[]{"%" + MEDIA_PATH + "%"}, null, 0);
return musicListSDCardCursor;
}
and mQuery code:
public static Cursor mQuery(Context context, Uri uri, String[] projection,
String selection, String[] selectionArgs, String sortOrder, int limit) {
try {
ContentResolver resolver = context.getContentResolver();
if (resolver == null) {
return null;
}
if (limit > 0) {
uri = uri.buildUpon().appendQueryParameter("limit", "" + limit).build();
}
return resolver.query(uri, projection, selection, selectionArgs, sortOrder);
} catch (UnsupportedOperationException ex) {
return null;
}
}
But how make query to MediaStore to get all Albums from this folder /sdcard/Music/ ?
I make it, like:
private Cursor allAlbums() {
Cursor cursor_albums_from_songs = allSongs();
HashSet<String> albumsList = new HashSet<String>();
int count = cursor_albums_from_songs.getCount();
for (int i = 0; i < count; i++) {
cursor_albums_from_songs.moveToPosition(i);
albumsList.add(cursor_albums_from_songs.getString(3));
Log.i("cursor", "" + cursor_albums_from_songs.getString(3));
}
Log.i("allAlbums", "" + albumsList.size());
ArrayList<String> list = new ArrayList<String>(albumsList.size());
list.addAll(albumsList);
int count_ids = list.size();
Cursor[] cursors = new Cursor[count_ids];
for (int i = 0; i < count_ids; i++) {
String selection = MediaStore.Audio.Albums._ID + " = ? ";
String[] selectionArgs = new String[]{list.get(i)};
String[] projection = {MediaStore.Audio.Albums._ID, //0
MediaStore.Audio.Albums.ALBUM, // 1
MediaStore.Audio.Albums.ALBUM_ART, // 2
MediaStore.Audio.Albums.ARTIST, //3
MediaStore.Audio.Albums.FIRST_YEAR, //4
MediaStore.Audio.Albums.NUMBER_OF_SONGS //5
};
Cursor artistListSDCardCursor = mQuery(
mContext,
MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI,
projection, selection, selectionArgs, null, 0);
cursors[i] = artistListSDCardCursor;
}
MergeCursor mMergeCursor = new MergeCursor(cursors);
return mMergeCursor;
}

Categories

Resources