IllegalArgumentException:无效的列纬度 [英] IllegalArgumentException: Invalid column latitude

查看:105
本文介绍了IllegalArgumentException:无效的列纬度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

遵循文档我尝试查询 ContentResolver 来获取文件大小,如下所示:

Following the documentation I tried querying ContentResolver to get the file size, like this:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == video_request_code) {
        if (resultCode == RESULT_OK) {
            Uri uri = data.getData();
            Cursor cur = getContentResolver().query(uri, null, null, null, null);
            try {
                if (cur != null && cur.moveToFirst()){
                    if (uri.getScheme() != null){
                        if (uri.getScheme().equals("content")) {
                            int sizeIndex = cur.getColumnIndex(OpenableColumns.SIZE);
                            Log.e("size", ""+cur.getLong(sizeIndex));
                        }else if (uri.getScheme().equals("file")) {
                            File ff = new File(uri.getPath());
                            Log.e("size", ""+ff.length());
                        }
                    }
                }
            }catch (Exception e){
                e.printStackTrace();
            }
            finally {
                if (cur != null)
                    cur.close();
            }
        }
    }
}

然后我得到以下错误:

java.lang.IllegalArgumentException: Invalid column latitude
        at android.app.ActivityThread.deliverResults(ActivityThread.java:4845)
        at android.app.ActivityThread.handleSendResult(ActivityThread.java:4886)
        at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:51)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7356)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
     Caused by: java.lang.IllegalArgumentException: Invalid column latitude
        at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:170)
        at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:140)
        at android.content.ContentProviderProxy.query(ContentProviderNative.java:423)
        at android.content.ContentResolver.query(ContentResolver.java:944)
        at android.content.ContentResolver.query(ContentResolver.java:880)
        at android.content.ContentResolver.query(ContentResolver.java:836)
        at com.my.package.MainActivity.onActivityResult(MainActivity.java:146)
        ...

崩溃指向 Cursor cur = getContentResolver().query(uri,null,null,null,null); ,但是如您所见,我不是在查询纬度列.实际上,在崩溃时,我还没有查询任何列.

The crash is pointing to Cursor cur = getContentResolver().query(uri, null, null, null, null);, but as you can see, I'm not querying the latitude column. In fact, at the time of the crash, I have not queried any column.

我只能在运行Android 10的设备上从Google Photo选择文件时重现此内容.

I could only reproduce this when selecting a file from Google Photo on a device running Android 10.

我的问题:

为什么会发生这种情况,我该如何克服呢?

Why is this happening and how can I overcome it?

我尝试传递列的投影.为了测试,我通过了以下内容:

I tried passing the projection of the columns. To test, I passed the following:

String[] projection = {OpenableColumns.SIZE, OpenableColumns.DISPLAY_NAME};
Cursor cur = getContentResolver().query(uri, projection, null, null, null);

但是我仍然遇到相同的错误.

But I still get the same error.

推荐答案

我在Android 10+中也遇到了同样的问题.此问题归因于Android 10中引入的范围存储.

I got the same issue in Android 10+. This issue is due to scoped storage which is introduced in Android 10.

解决方案:我对这个问题的解决方案是从Uri创建输入流.查看下面的代码段.

Solutions: My solution to this problem is to create an input stream from a Uri. Check the code snippet below.

fun getVideoRealPath(
        context: Context, uri: Uri
    ): File? {
        val contentResolver = context.contentResolver ?: return null

        // Create file path inside app's data dir
        val filePath = (context.applicationInfo.dataDir + File.separator
                + System.currentTimeMillis())
        val file = File(filePath)
        try {
            val inputStream = contentResolver.openInputStream(uri) ?: return null
            val outputStream: OutputStream = FileOutputStream(file)
            val buf = ByteArray(1024)
            var len: Int
            while (inputStream.read(buf).also { len = it } > 0) outputStream.write(buf, 0, len)
            outputStream.close()
            inputStream.close()
        } catch (ignore: IOException) {
            return null
        }
        return file
    }

这篇关于IllegalArgumentException:无效的列纬度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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