德precated ManagedQuery()问题问题、precated、ManagedQuery

由网友(吹箫望江南)分享简介:我有这样的方法:public String getRealPathFromURI(Uri contentUri) {String[] proj = { MediaStore.Images.Media.DATA };Cursor cursor = managedQuery(contentUri, proj, null,...

我有这样的方法:

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);

由于 managedQuery()是德precated。

Because managedQuery() is deprecated.

我怎么可以重写此方法不使用 managedQuery()

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

推荐答案

您可以使用更换context.getContentResolver()查询 LoaderManager (你需要使用兼容包之前API版本11支持的设备)。

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;
}
阅读全文

相关推荐

最新文章