我有这个getPath
方法,它有效,但是我想摆脱警告,因为它说不推荐使用managedQuery
方法。如何在没有警告的情况下实现这种精确的getPath
方法?谢谢。
public String getPath(Uri uri) {
System.out.println("IM IN getPath");
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
编辑:我现在收到此logcat错误:
10-23 07:19:14.080: E/ActivityThread(594): Activity com.android.internal.app.ChooserActivity has leaked IntentReceiver com.android.internal.app.ResolverActivity$1@416c9560 that was originally registered here. Are you missing a call to unregisterReceiver()?
10-23 07:19:14.080: E/ActivityThread(594): android.app.IntentReceiverLeaked: Activity com.android.internal.app.ChooserActivity has leaked IntentReceiver com.android.internal.app.ResolverActivity$1@416c9560 that was originally registered here. Are you missing a call to unregisterReceiver()?
10-23 07:19:14.080: E/ActivityThread(594): at android.app.LoadedApk$ReceiverDispatcher.<init>(LoadedApk.java:763)
10-23 07:19:14.080: E/ActivityThread(594): at android.app.LoadedApk.getReceiverDispatcher(LoadedApk.java:567)
10-23 07:19:14.080: E/ActivityThread(594): at android.app.ContextImpl.registerReceiverInternal(ContextImpl.java:1043)
10-23 07:19:14.080: E/ActivityThread(594): at android.app.ContextImpl.registerReceiver(ContextImpl.java:1030)
10-23 07:19:14.080: E/ActivityThread(594): at android.app.ContextImpl.registerReceiver(ContextImpl.java:1024)
10-23 07:19:14.080: E/ActivityThread(594): at android.content.ContextWrapper.registerReceiver(ContextWrapper.java:341)
10-23 07:19:14.080: E/ActivityThread(594): at com.android.internal.content.PackageMonitor.register(PackageMonitor.java:65)
10-23 07:19:14.080: E/ActivityThread(594): at com.android.internal.app.ResolverActivity.onCreate(ResolverActivity.java:99)
10-23 07:19:14.080: E/ActivityThread(594): at com.android.internal.app.ChooserActivity.onCreate(ChooserActivity.java:53)
10-23 07:19:14.080: E/ActivityThread(594): at android.app.Activity.performCreate(Activity.java:4465)
10-23 07:19:14.080: E/ActivityThread(594): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
10-23 07:19:14.080: E/ActivityThread(594): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
10-23 07:19:14.080: E/ActivityThread(594): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
10-23 07:19:14.080: E/ActivityThread(594): at android.app.ActivityThread.access$600(ActivityThread.java:123)
10-23 07:19:14.080: E/ActivityThread(594): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
10-23 07:19:14.080: E/ActivityThread(594): at android.os.Handler.dispatchMessage(Handler.java:99)
10-23 07:19:14.080: E/ActivityThread(594): at android.os.Looper.loop(Looper.java:137)
10-23 07:19:14.080: E/ActivityThread(594): at android.app.ActivityThread.main(ActivityThread.java:4424)
10-23 07:19:14.080: E/ActivityThread(594): at java.lang.reflect.Method.invokeNative(Native Method)
10-23 07:19:14.080: E/ActivityThread(594): at java.lang.reflect.Method.invoke(Method.java:511)
10-23 07:19:14.080: E/ActivityThread(594): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
10-23 07:19:14.080: E/ActivityThread(594): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
10-23 07:19:14.080: E/ActivityThread(594): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:2)
您可以按getContentResolver.query()
所以它看起来像这样。
public String getPath(Uri uri) {
System.out.println("IM IN getPath");
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
答案 1 :(得分:0)
试试这个 - 它有效
fun getPath(context: Context?, uri: Uri?): String? {
print(uri)
if (uri == null)
return null
val projection = arrayOf(MediaStore.Video.Media.DATA)
val cursor: Cursor? = context?.contentResolver?.query(uri, projection, null, null, null)
val path = if (cursor != null) {
val columnIndex: Int = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA)
cursor.moveToFirst()
cursor.getString(columnIndex)
} else null
cursor?.close()
return path
}
新问题“'DATA: String' 已弃用。Java Q 中已弃用”
MediaStore.Video.Media.DATA