Deprecated ManagedQuery() issue
Asked Answered
M

3

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

Monacid answered 3/10, 2012 at 18:36 Comment(0)
E
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;
}
Effect answered 3/10, 2012 at 18:45 Comment(11)
ops... no doesn't works in any case... if the uri starts with "file://" doesn't returns the right pathMonacid
file:// URIs generally can't be resolved using contentUri: if you have a file URI you ALREADY have the real path.Effect
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.Monacid
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()));.Effect
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 UriMonacid
Ah, sure: new File(new URI(uri.getPath())).getAbsolutePath(); is what you need, no?Effect
But in this way ...what about file extension? The user will receive a file without any extension?Monacid
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 :(Monacid
Hey did you figure out the solution at the end AndreaFOkie
Thanks! getContentResolver().query(uri, projection, null, null, null); works nicely.Melva
This code has an extra semicolon at the end of ` if(cursor.moveToFirst()){;`Guntar
A
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();
            }
        }
    }
Asha answered 19/8, 2013 at 12:55 Comment(0)
P
-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();
}
Pled answered 1/4, 2014 at 8:4 Comment(1)
Initialize cursor helps with deprecated method, really?Proline

© 2022 - 2024 — McMap. All rights reserved.