James_Watson
Android Enthusiast
On a Android 11 device, through SAF(Storage Access Framework) system file picker, my app picks a PDF file which locates in the public folder Downloads.
In order to get its full path, I try to query the uri table through context.getContentResolver().query().
But now, the cursor returned is not null while cursor.moveToFirst() == false. As a result, my app failed to get the full path.
What's wrong?
My Android device: Android 11, Mi 12 Pro. The READ_EXTERNAL_STORAGE and MANAGE_EXTERNAL_STORAGE are all in manifest.
code snippet a-
......
//now, uri = "content://com.android.providers.downloads.documents/document/msf%3A7755"
final String id = DocumentsContract.getDocumentId(uri); //now, id = msf:7755
final String[] split = id.split(":");
final String type = split[0]; //now, type="msf"
String strRealId = split[1]; //now, strRealId = 7755
final Uri contentUri;
contentUri =ContentUris.withAppendedId(MediaStore.Downloads.getContentUri(MediaStore.VOLUME_EXTERNAL),
Long.valueOf(strRealId));
//contentUri =ContentUris.withAppendedId(MediaStore.Downloads.EXTERNAL_CONTENT_URI,
//Long.valueOf(strRealId));
//now, contentUri = "content://media/external/downloads/7755"
path = getDataColumn(context, contentUri, null, null);
code snippet b -
......
private static String getDataColumn(Context context, Uri uri, String selection, String[] selectionArgs) {
Cursor cursor = null;
final String column = "_data";
final String column2 = "_id";
final String[] projection = {column,column2};
try {
cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null);
//The row as below. step debug to here. I found cursor != null while cursor.moveToFirst() == false! What's wrong?
if (cursor != null && cursor.moveToFirst()) {
final int column_index = cursor.getColumnIndexOrThrow(column);
String strTest = "";
int nTest = 0;
strTest = cursor.getString(column_index);
nTest = cursor.getInt(1);
return strTest;
}
} catch(Exception e)
{
String strError = e.getMessage();
}
finally {
if (cursor != null)
cursor.close();
}
return null;
}
In order to get its full path, I try to query the uri table through context.getContentResolver().query().
But now, the cursor returned is not null while cursor.moveToFirst() == false. As a result, my app failed to get the full path.
What's wrong?
My Android device: Android 11, Mi 12 Pro. The READ_EXTERNAL_STORAGE and MANAGE_EXTERNAL_STORAGE are all in manifest.
code snippet a-
......
//now, uri = "content://com.android.providers.downloads.documents/document/msf%3A7755"
final String id = DocumentsContract.getDocumentId(uri); //now, id = msf:7755
final String[] split = id.split(":");
final String type = split[0]; //now, type="msf"
String strRealId = split[1]; //now, strRealId = 7755
final Uri contentUri;
contentUri =ContentUris.withAppendedId(MediaStore.Downloads.getContentUri(MediaStore.VOLUME_EXTERNAL),
Long.valueOf(strRealId));
//contentUri =ContentUris.withAppendedId(MediaStore.Downloads.EXTERNAL_CONTENT_URI,
//Long.valueOf(strRealId));
//now, contentUri = "content://media/external/downloads/7755"
path = getDataColumn(context, contentUri, null, null);
code snippet b -
......
private static String getDataColumn(Context context, Uri uri, String selection, String[] selectionArgs) {
Cursor cursor = null;
final String column = "_data";
final String column2 = "_id";
final String[] projection = {column,column2};
try {
cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null);
//The row as below. step debug to here. I found cursor != null while cursor.moveToFirst() == false! What's wrong?
if (cursor != null && cursor.moveToFirst()) {
final int column_index = cursor.getColumnIndexOrThrow(column);
String strTest = "";
int nTest = 0;
strTest = cursor.getString(column_index);
nTest = cursor.getInt(1);
return strTest;
}
} catch(Exception e)
{
String strError = e.getMessage();
}
finally {
if (cursor != null)
cursor.close();
}
return null;
}
Last edited: