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.
Related
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);
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 );
I've an cursor that fills up my ListView in Android.
My code of cursor:
Cursor cursor = sqliteDatabase.query("ProductosTratamientos", null,
"codigoproducto <> '70027, 70029, 70024'", null, null, null, null);
It is not working. It continues filling the list with these values: 70027, 70029, 70024.
What am I doing wrong?
Thanks.
Instead of using <>, you need to use NOT IN which offers look-up with in a set of values. So try changing your query to something like:
Cursor cursor = sqliteDatabase.query("ProductosTratamientos", null,
"codigoproducto NOT IN (70027, 70029, 70024)", null, null, null, null);
Try this code:
Cursor cursor = sqliteDatabase.query("ProductosTratamientos", null,
"codigoproducto <> ? AND codigoproducto <> ? AND codigoproducto <> ?',
new String[]{"70027", "70029", "70024"},
null, null, null);
I'm working on Sony GTV and the Logitech revue GTV.
I want to run managedQuery for the Google quicksearchbox.
I found after running dumpstate, in the "Registered ContentProviders" section,
that the Content_Uri is : "com.android.quicksearchbox.google".
But when I use that URI in a query:
Uri uri = Uri.parse("content://com.android.quicksearchbox.google");
Cursor mCursor = managedQuery(uri, null, null, null, null);
I receive an "Unknown URI" error message.
It works with a YouTube content_uri:
Uri uri = Uri.parse("content://com.google.android.youtube.googletv.SuggestionProvider");
Cursor mCursor = managedQuery(uri, null, null, null, null);
Where did I go wrong?
Querying the quicksearchbox provider is not supported.
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.