IllegalArgumentException:列_data不存在 [英] IllegalArgumentException: column '_data' does not exist
问题描述
在牛轧糖中,此功能不起作用。
In Nougat, this function is not working.
String path = getRealPathFromURI(this, getIntent().getParcelableExtra(Intent.EXTRA_STREAM));
public String getRealPathFromURI(Context context, Uri contentUri) {
Cursor cursor = null;
try {
String[] proj = {MediaStore.Images.Media.DATA};
cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
if (cursor == null) return contentUri.getPath();
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
} finally {
if (cursor != null) {
cursor.close();
}
}
}
崩溃日志:
java.lang.RuntimeException: Unable to start activity ComponentInfo{class path}: java.lang.IllegalArgumentException: column '_data' does not exist
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2659)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2724)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1473)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6123)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757)
Caused by java.lang.IllegalArgumentException: column '_data' does not exist
at android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:333)
at android.database.CursorWrapper.getColumnIndexOrThrow(CursorWrapper.java:87)
at com.package.SaveImageActivity.getRealPathFromURI()
at com.package.SaveImageActivity.onCreate()
at android.app.Activity.performCreate(Activity.java:6672)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1140)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2612)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2724)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1473)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6123)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757)
此功能正常运行在Android N之前的设备中。我阅读了 file://方案与Intent一起附加在targetSdkVersion 24(Android Nougat)上。但是找不到任何解决方案。因此,请帮忙。
This function is working properly in devices before Android N. I read the article file:// scheme is now not allowed to be attached with Intent on targetSdkVersion 24 (Android Nougat). But couldn't find any solution. So please help.
推荐答案
此功能在Android N之前的设备中正常运行
This function is working properly in devices before Android N
它适用于极少的 Uri
值,可能没有结果(例如,对于事物由 MediaStore
索引的不是本地文件),并且可能没有可用的结果(例如,对于可移动存储上的文件)。
It works for very few Uri
values, may not have a result (e.g., for things that are indexed by MediaStore
that are not local files), and may not have a usable result (e.g., for files on removable storage).
所以请帮忙。
So please help.
使用 ContentResolver
和 openInputStream()
在<$ c所标识的内容上获得 InputStream
$ c> Uri 。理想情况下,无论您要做什么,都直接使用该流。或者,在您控制的文件上使用该 InputStream
和一些 FileOutputStream
来复制内容,然后使用该文件。
Use a ContentResolver
and openInputStream()
to get an InputStream
on the content identified by the Uri
. Ideally, just use that stream directly, for whatever it is that you are trying to do. Or, use that InputStream
and some FileOutputStream
on a file that you control to make a copy of the content, then use that file.
这篇关于IllegalArgumentException:列_data不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!