116

I have this method:

public String getRealPathFromURI(Uri contentUri) {
    String[] proj = { MediaStore.Images.Media.DATA };
    Cursor cursor = managedQuery(contentUri, proj, null, null, null);
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
}

Unfortunately the compiler show me a problem on:

Cursor cursor = managedQuery(contentUri, proj, null, null, null);

Because managedQuery() is deprecated.

How could I rewrite this method without use managedQuery()?

3 Answers 3

266

You could replace it with context.getContentResolver().query and LoaderManager (you'll need to use the compatibility package to support devices before API version 11).

However, it looks like you're only using the query one time: you probably don't even need that. Maybe this would work?

public String getRealPathFromURI(Uri contentUri) {
    String res = null;
    String[] proj = { MediaStore.Images.Media.DATA };
    Cursor cursor = getContentResolver().query(contentUri, proj, null, null, null);
    if(cursor.moveToFirst()){;
       int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
       res = cursor.getString(column_index);
    }
    cursor.close();
    return res;
}
10
  • ops... no doesn't works in any case... if the uri starts with "file://" doesn't returns the right path
    – AndreaF
    Oct 3, 2012 at 19:54
  • file:// URIs generally can't be resolved using contentUri: if you have a file URI you ALREADY have the real path.
    – Femi
    Oct 3, 2012 at 19:55
  • Could you give me more details? I have an "Uri", my problem is to get the real absolute path without file://, /content:/ and others attributes.
    – AndreaF
    Oct 3, 2012 at 20:15
  • 1
    For a content URI, you'll need a resolver to get a file URI, and once you have a file URI you can just do new File(new URI(uri.getPath()));.
    – Femi
    Oct 3, 2012 at 21:29
  • 1
    Ah, sure: new File(new URI(uri.getPath())).getAbsolutePath(); is what you need, no?
    – Femi
    Oct 3, 2012 at 21:38
3
public void getBrowserHist(Context context) {
        Cursor mCur = context.getContentResolver().query(Browser.BOOKMARKS_URI,
                Browser.HISTORY_PROJECTION, null, null, null);
        mCur.moveToFirst();
        if (mCur != null && mCur.moveToFirst() && mCur.getCount() > 0) {
            while (mCur.isAfterLast() == false) {
                Log.e("hist_titleIdx",
                        mCur.getString(Browser.HISTORY_PROJECTION_TITLE_INDEX));
                Log.e("hist_urlIdx",
                        mCur.getString(Browser.HISTORY_PROJECTION_URL_INDEX));
                mCur.moveToNext();
            }
        }
    }
-8

you need to initialize the cursor becauese it will be close before method start or some where else

cursor = null;
public void method(){
// do your stuff here 
cursor.close();
}
1
  • 8
    Initialize cursor helps with deprecated method, really?
    – IliaEremin
    Sep 13, 2014 at 6:54

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.