Why my cursor is always null? - android

Following code I use to get images from gallery
final Uri sourceUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
CursorLoader cursorLoader = new CursorLoader( this,sourceUri,null,null,null,MediaStore.Audio.Media.TITLE );
Cursor cursor = cursorLoader.loadInBackground();
here cursor fetches all the images but when I give a specific path as uri like below
String dirUri = "/storage/emulated/myimages";
Cursor cursor = getContentResolver().query( Uri.parse(dirUri), null, null, null, null );
then cursor always gets null value.
How to resolve this ?

Try out as below to get the image from particular folder.
String dirUri =Environment.getExternalStorageDirectory()+ "/myimages";
Cursor cursor = getContentResolver().query(Uri.parse(new File(dirUri).toString()), null, null, null, null );

Use this code instead of your code:
File images = new File(Environment.getExternalStorageDirectory()
+ "/myimages");
Cursor cursor = getContentResolver().query( Uri.parse(images.toString()), null, null, null, null );

Related

How to iterate through all Contentresolver entries?

I have a database in my app, which I fill with data and an ID.
Then I request the database with the ID's I want:
Cursor cursor = contentResolver.query(uriPath, null, "ID = ?", new String[]{id1, id2, etc.}, null);
This retrieves me a cursor with all requested ID's .
How can I retrieve all objects in my UriPath ?
Just enter the Uri and leave the rest null
Cursor cursor = contentResolver.query(uriPath, null, null, null, null);

Empty cursor row when filtering with selection

I'm not confident with cursors and I'm facing some problem when filtering one with a WHERE clause.
What I'm doing:
ContentResolver contentResolver = context.getContentResolver();
Uri uriConversation = Uri.parse("content://mms-sms/conversations/");
String[] projection = new String[]{"*"};
String selection = "address=" + phoneNumberForThread;
Cursor cursor = contentResolver.query(uriConversation, projection, null, null, null);
Executing this code the cursor get filled and works perfectly.
However, if I swap the null selection argument with my selection String as
Cursor cursor = contentResolver.query(uriConversation, projection, selection, null, null);
I then get an empty cursor. I even check for !phoneNumberForThread.isEmpty()
I think I'm doing something wrong but again I am not confident with cursor yet.
Any help would be really appreciated.
If your input variabel is stored as a string data type, you should enclose it with ' as such:
String selection = "address='" + phoneNumberForThread + "'";

Cursor query from specific image folder, not load all image from sdcard

The code below will return all image from the SD card. But, I need to modify it so that it only display images from other folders.
Uri mImageUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
ContentResolver mContentResolver = mContext.getContentResolver();
Cursor mCursor = mContentResolver.query(mImageUri, null, null, null, null);
I have tried this:
String path = Environment.getExternalStorageDirectory().getAbsolutePath()+"/Testing/";
Uri uri = Uri.parse(path);
Cursor mCursor = mContentResolver.query(Uri, null, null, null, null);
and am getting an error. Any help would be appreciated.
Uri mImageUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
// field data which u need
final String[] columns = { MediaStore.Images.Media.DATA, MediaStore.Images.Media.DATE_ADDED};
Cursor mCursor = mContentResolver.query(mImageUri, columns, MediaStore.Images.Media.DATA + " like ? ",new String[] {"%/YourFolderName/%"}, null);
ArrayList<String> fileNames =null
File path =
File(getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).path,
"Your Folder Name")
if (path.exists()) {
fileNames = path.getList() //you may need to to adjust the Data type of fileNames
}
So, now you have list of images path that you can pass on to recycler view using recycler view Adapter.

NullPointerException when trying to initialize Cursor using getContentResolver()

So what I am trying to do is display my database in another activity and add an onItemClickListener. Then I am sending my info by using a URI to another class. I am retrieving the uri in the recipient class and using it to create a Cursor.
When I try to initialize my Cursor using getContentResolver().query(), I am getting a nullpointerexception within my log. The line of code looks like this:
Cursor cursor = getContentResolver().query(uri, null, null, null, null);
The uri variable I am using comes from this line of code:
Uri uri = getIntent().getData();
To make sure that some sort of uri is being returned, I output the uri in my log and got this:
content://com.example.myfirstapp.ContactProvider/CONTACT/(The id of the item selected)
Here is my log:
01-06 21:31:43.913: E/AndroidRuntime(619): Caused by: java.lang.NullPointerException
01-06 21:31:43.913: E/AndroidRuntime(619): at com.example.myfirstapp.ContactActivity.onCreate(ContactActivity.java:22)
The code at line 22:
Cursor cursor = getContentResolver().query(uri, null, null, null, null);
Your line should looks like this
Uri uri = getIntent().getData();
Cursor cursor = getContentResolver().query(uri, null, null, null, null);
instead of this
Cursor cursor = getContentResolver().query(uri, null, null, null, null);
Uri uri = getIntent().getData();
You have used uri before you have initialized.
and also check for manifest permissions
android.permission.READ_CONTACTS
you can try this
Cursor cursor = getApplicationContext().getContentResolver().query(uri, null, null, null, null);
This code
Cursor cursor = getApplicationContext().getContentResolver().query(uri, null, null, null, null);
does the job. Do not call your function before oncreate.

Handling ACTION_SEND

I want to handle ACTION_SEND intent.
So i get an uri of the shared item using this code:
Bundle extras = intent.getExtras();
if (extras.containsKey(Intent.EXTRA_STREAM))
{
Uri uri = (Uri) extras.getParcelable(Intent.EXTRA_STREAM);
....
this uri is something like this:
content://com.android.contacts/contacts/as_vcard/0n3B4537432F4531
How i can get exact contact from this uri?
I tried this:
Cursor cursor = managedQuery(uri, null, null, null, null);
and this:
Cursor cursor = getContentResolver().query(uri, null, null, null, null);
and got an exception and program termination in both cases.
Please help!
Just remove as_vcard in content://com.android.contacts/contacts/as_vcard/0n3B4537432F4531
If it does not work try this.
String key= uri.getPathSegments().get(2);
Cursor cursor = getContentResolver().query(Uri.withAppendedPath(Contacts.CONTENT_LOOKUP_URI, key),
null, null, null, null);
If you receive a content:// URI in an EXTRA_STREAM you should be able to query OpenableColumns to get the meta information.
String name = null;
String size = null;
Cursor cursor = getContentResolver().query(uri, new String[] { OpenableColumns.DISPLAY_NAME, OpenableColumns.SIZE }, null, null, null);
if (cursor != null) {
try {
if (cursor.moveToFirst()) {
name = cursor.getString(0);
size = cursor.getString(1);
}
} finally {
cursor.close();
}
}
To get the actual content use ContentResolver.openInputStream(Uri) and read the stream. Don't forget to close it afterwards.

Categories

Resources