Cursor always returning null on query - android

I'm using an Intent to select a file from my Android device. When shown the Chooser, I select the Astro file manager and then procede to select a file (a video in this case).
I get back a URI (content://com.metago.astro.filecontent/file/file/sdcard/Download/video_video.mp4) and I'm trying to process it with the following:
Cursor cursor = null;
final String column = null;
final String[] projection = null;
try {
cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null);
if (cursor != null && cursor.moveToFirst()) {
// Never enters here because cursor always null
L.p("Hi!");
}
} catch (Exception e) {
L.p(e.getMessage()); // Never hit
} finally {
if (cursor != null)
cursor.close();
}
however cursor always returns null . Any idea why this happens?

Related

getContentResolver() value null for data but receiving name correctly

I'm not able to read data part from the selected image Uri, this happens on some specific device Lg nexus 5 api 6.0.1
Uri of a selected image
content://com.google.android.apps.photos.contentprovider/-1/1/content://media/external/images/media/200/ORIGINAL/NONE/2077196451
Code used to get data from the uri
public static String getDataColumn(Context context, Uri uri, String selection,
String[] selectionArgs) {
Cursor cursor = null;
final String column = "_data";
final String[] projection = {
column
};
try {
cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs,
null);
if (cursor != null && cursor.moveToFirst()) {
final int index = cursor.getColumnIndexOrThrow(column);
return cursor.getString(index);
}
}catch (Exception e){
e.printStackTrace();
}
finally {
if (cursor != null)
cursor.close();
}
return null;
}
Not issue with the permissions handling, Required Permissions are given.

How to display the media title in a listview?

How can I get media titles from COntentResolver. i tried but it no works, i want to display media titles in my listView .
Cursor cursor = getContentResolver().query(uri, null, null, null, null);
if (cursor != null) {
int ind = cursor.getColumnIndex(MediaStore.Audio.Media.TITLE);
do {
String T = cursor.getString(ind);
abcl.add(T);
Log.i("SOngName", T);
} while (cursor.moveToNext());
}
but this gives me error of IndexOutOfBondsExceptions, my error logcat is
please any one guide , thanks
well first of all you should go for Cursor Documentation in android . it will tell you that after using the cursor is not null check; you have to move cursor to the start of first row on incoming result set.
Cursor cursor = getContentResolver().query(uri, null, null, null, null);
if (cursor != null) {
if(cursor.MoveToFirst()){
int ind = cursor.getColumnIndex(MediaStore.Audio.Media.TITLE);
do {
String T = cursor.getString(ind);
abcl.add(T);
Log.i("SOngName", T);
} while (cursor.moveToNext());
}
}

How to get file path of google drive document in Android

I am trying to access file from device. All works goods but when i browse and select file from google drive it gives null path. I have seen many question but didn't got the answer. One solution which i got below only gives me the file name.
public static String getGDriveDataColumn(Context context, Uri uri, String selection,
String[] selectionArgs) {
Cursor cursor = null;
final String column = "_display_name";
final String[] projection = {
column
};
try {
cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null);
if (cursor != null && cursor.moveToFirst()) {
final int column_index = cursor.getColumnIndexOrThrow(column);
return cursor.getString(column_index);
}
} finally {
if (cursor != null)
cursor.close();
}
return null;
}
Is it possible to get that file? Because that file is on the drive and not on the device. Please help me how can i get that file.

File can't be found in file DB

I am starting an intent to pick a file, I do pick a file and I get a URI, which I obviously can't use.
I am trying to convert this Uri to a legit file path and for that I use this method
public string GetPathToImage(global::Android.Net.Uri uri)
{
string path = null;
// The projection contains the columns we want to return in our query.
string[] projection = new[] { global::Android.Provider.MediaStore.Images.Media.InterfaceConsts.Data };
using (global::Android.Database.ICursor cursor = ManagedQuery(uri, projection, null, null, null))
{
if (cursor != null)
{
int columnIndex = cursor.GetColumnIndexOrThrow(global::Android.Provider.MediaStore.Images.Media.InterfaceConsts.Data);
cursor.MoveToFirst();
path = cursor.GetString(columnIndex);
}
}
return path;
}
Problem: this method returns a null string. But I need a working one.
Any suggestions?

where to close the cursor?

i have a cursor that I am using in the following code. But I want to close the cursor after it is used and no longer needed. The problem is that the cursor is used in the return statement, but I can't close it after the return statement because that is unreachable code. It is used in the return statement so I can't close it above that line. How do I close the cursor? This is not like the old managedQuery, I assume that you have to close it.
public String getPath(Uri uri) {
String[] projection = { MediaStore.Audio.Media.DATA };
Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
// cursor.close() <--- not possible because it is unreachable code after return
}
You can assign it to a String object then close cursor and return it.
cursor.moveToFirst();
String result = cursor.getString(column_index);
cursor.close();
return result;
Would using a finally block work for you? I'm not experienced in your particular problem but could you put your cleanup in the finally block that would otherwise be bypassed by the return statement:
http://docs.oracle.com/javase/tutorial/essential/exceptions/finally.html
Just an idea! Don't shoot me if not!
Try - Finally to the rescue...
public String getPath(Uri uri) {
Cursor cursor = null;
try {
String[] projection = { MediaStore.Audio.Media.DATA };
cursor = getContentResolver().query(uri, projection, null, null, null);
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
finally {
if (cursor != null)
cursor.close(); // close the cursor
}
}
NOTE
Just noted another answer below..
If you're inside an activity, you can call startManaginCursor() and pass the cursor instance so Android will manage its lifetime. If you don't have access to Android Application Context (ex: code is in a utility class) use above method. (and sprinkle a bit of some error handling ;) )
if (this.daoDatabase != null) {
final Cursor genericCursor = this.daoDatabase.query(this.tableName, null, null, null, null, null, orderByCriteria);
if (genericCursor != null) {
if (genericCursor.moveToFirst()) {
do {
// do stuff...
} while (genericCursor.moveToNext());
}
genericCursor.close();
}
}

Categories

Resources