Code example for CursorLoader

Methods: loadInBackground

0
    public static String getContentPathFromUri(final Context context, final Uri uri) {
        Cursor cursor = null;
        String contentPath = null;
        try { 
            final String[] proj = { MediaStore.Images.Media.DATA };
            final CursorLoader loader = new CursorLoader(context, uri, proj, null, null, null);
            cursor = loader.loadInBackground();
            final int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToFirst();
            contentPath = cursor.getString(columnIndex);
        } catch (final Exception e) {
            Log.w(TAG, "getContentPathFromURI(" + uri.toString() + "): " + e.getMessage());
        } finally { 
            if (cursor != null)
                cursor.close();
        } 
 
        return contentPath != null ? contentPath : "";
 
    }