其他分享
首页 > 其他分享> > android – IllegalArgumentException:列’_data’不存在

android – IllegalArgumentException:列’_data’不存在

作者:互联网

在Nougat中,此功能无效.

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:// scheme is now not allowed to be attached with Intent on targetSdkVersion 24 (Android Nougat).但找不到任何解决方案.所以请帮忙.

解决方法:

This function is working properly in devices before Android N

它适用于非常少的Uri值,可能没有结果(例如,对于由MediaStore索引的非本地文件的东西),并且可能没有可用的结果(例如,对于可移动存储上的文件).

So please help.

使用ContentResolver和openInputStream()在Uri标识的内容上获取InputStream.理想情况下,只需直接使用该流,无论您尝试做什么.或者,在您控制的文件上使用该InputStream和一些FileOutputStream来制作内容的副本,然后使用该文件.

标签:android-7-0-nougat,android,android-7-1-nougat
来源: https://codeday.me/bug/20190930/1835529.html