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()?

none
  • 11,793
  • 9
  • 51
  • 87
AndreaF
  • 11,975
  • 27
  • 102
  • 168

3 Answers3

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;
}
Femi
  • 64,273
  • 8
  • 118
  • 148
  • ops... no doesn't works in any case... if the uri starts with "file://" doesn't returns the right path – AndreaF Oct 03 '12 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 03 '12 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 03 '12 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 03 '12 at 21:29
  • but I have to send only the absolute path to an external specific library ... so I need a solution to get the absolute path from the Uri – AndreaF Oct 03 '12 at 21:36
  • 1
    Ah, sure: `new File(new URI(uri.getPath())).getAbsolutePath();` is what you need, no? – Femi Oct 03 '12 at 21:38
  • But in this way ...what about file extension? The user will receive a file without any extension? – AndreaF Oct 04 '12 at 00:52
  • btw with new File(new URI(uri.getPath())).getAbsolutePath(); I get the Exception URI is not absolute. I have marked your answer because answers the initial question, however I haven't solved my problem :( – AndreaF Oct 04 '12 at 04:29
  • Hey did you figure out the solution at the end AndreaF – Lion789 Dec 22 '13 at 21:21
  • Thanks! getContentResolver().query(uri, projection, null, null, null); works nicely. – Mangi Morobe Oct 22 '15 at 03:53
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();
            }
        }
    }
PrvN
  • 2,385
  • 6
  • 28
  • 30
-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();
}
Buggy IdioT
  • 6
  • 1
  • 12