德precated ManagedQuery()问题 [英] Deprecated ManagedQuery() issue

查看:161
本文介绍了德precated ManagedQuery()问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的方法:

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

这篇关于德precated ManagedQuery()问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆