Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
344 views
in Technique[技术] by (71.8m points)

java - IllegalArgumentException: Invalid column latitude

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

Then I get the following error:

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)
        ...

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.

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


My Question:

Why is this happening and how can I overcome it?


Edit:

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...