ContentResolver:column ‘_data‘ does not exist. Available columns: [document_id, mime_type,

    private  void getContentResolverInfo(Uri uri, int width, int height, SlideFactory.MediaType mediaType) {
        Cursor cursor = null;
        long   start  = System.currentTimeMillis();

        try {
            cursor = context.getContentResolver().query(uri, null, null, null, null);

            if (cursor != null && cursor.moveToFirst()) {
                String fileName = cursor.getString(cursor.getColumnIndexOrThrow(OpenableColumns.DISPLAY_NAME));
                long   fileSize = cursor.getLong(cursor.getColumnIndexOrThrow(OpenableColumns.SIZE));
                long   filePath = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA) );

                String mimeType = context.getContentResolver().getType(uri);

            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (cursor != null)
                cursor.close();
        }
    }
2021-05-25 12:49:05.292 17563-17563/org.securesms W/System.err: java.lang.IllegalArgumentException: column '_data' does not exist. Available columns: [document_id, mime_type, _display_name, summary, last_modified, flags, _size]
2021-05-25 12:49:05.292 17563-17563/org.securesms W/System.err:     at android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:351)
2021-05-25 12:49:05.292 17563-17563/org.securesms W/System.err:     at android.database.CursorWrapper.getColumnIndexOrThrow(CursorWrapper.java:91)
2021-05-25 12:49:05.292 17563-17563/org.securesms W/System.err:     at org.thoughtcrime.securesms.wallet.ui.WalletImportActivity.getContentResolverInfo(WalletImportActivity.java:234)
2021-05-25 12:49:05.292 17563-17563/org.securesms W/System.err:     at org.thoughtcrime.securesms.wallet.ui.WalletImportActivity.onActivityResult(WalletImportActivity.java:214)
2021-05-25 12:49:05.292 17563-17563/org.securesms W/System.err:     at android.app.Activity.dispatchActivityResult(Activity.java:8310)
2021-05-25 12:49:05.292 17563-17563/org.securesms W/System.err:     at android.app.ActivityThread.deliverResults(ActivityThread.java:5008)
2021-05-25 12:49:05.292 17563-17563/org.securesms W/System.err:     at android.app.ActivityThread.handleSendResult(ActivityThread.java:5056)
2021-05-25 12:49:05.292 17563-17563/org.securesms W/System.err:     at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:51)
2021-05-25 12:49:05.292 17563-17563/org.securesms W/System.err:     at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
2021-05-25 12:49:05.292 17563-17563/org.securesms W/System.err:     at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
2021-05-25 12:49:05.292 17563-17563/org.securesms W/System.err:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
2021-05-25 12:49:05.292 17563-17563/org.securesms W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:106)
2021-05-25 12:49:05.292 17563-17563/org.securesms W/System.err:     at android.os.Looper.loop(Looper.java:223)
2021-05-25 12:49:05.292 17563-17563/org.securesms W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:7656)
2021-05-25 12:49:05.292 17563-17563/org.securesms W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
2021-05-25 12:49:05.292 17563-17563/org.securesms W/System.err:     at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
2021-05-25 12:49:05.292 17563-17563/org.securesms W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)

一般使用这个时都是从系统读取资源后希望根据MediaStore.Files.FileColumns.DATA字段获取文件的绝对路径,从而读取文件内容。

但是从api 29开始MediaStore.Files.FileColumns.DATA字段已经废弃了,如果想读取文件内容,可以使用FileDescriptor:

    private  void getContentResolverInfo(Uri uri, int width, int height, SlideFactory.MediaType mediaType) {
        Cursor cursor = null;
        long   start  = System.currentTimeMillis();

        try {
            cursor = context.getContentResolver().query(uri, null, null, null, null);

            if (cursor != null && cursor.moveToFirst()) {
                String fileName = cursor.getString(cursor.getColumnIndexOrThrow(OpenableColumns.DISPLAY_NAME));
                long   fileSize = cursor.getLong(cursor.getColumnIndexOrThrow(OpenableColumns.SIZE));
                //long   filePath = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA) );

                ParcelFileDescriptor parcelFileDescriptor = context.getContentResolver().openFileDescriptor(uri,"rw");
                FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();
                String keystoreContent = FileUtils.readFileContent(fileDescriptor);

                FileInputStream fileInputStream = new FileInputStream(fileDescriptor);

                String mimeType = context.getContentResolver().getType(uri);

            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (cursor != null)
                cursor.close();
        }
    }

    public static String readFileContent( FileDescriptor fileDescriptor) {
        Log.i(TAG, "readFileContent(), fileDescriptor=" + fileDescriptor);
        if(fileDescriptor == null) {
            return null;
        }
        FileInputStream fileInputStream = null;
        try {
            fileInputStream = new FileInputStream(fileDescriptor);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, Charset.defaultCharset());
        BufferedReader reader = new BufferedReader(inputStreamReader);
        StringBuffer sb = new StringBuffer("");
        String line;
        try {
            while ((line = reader.readLine()) != null) {
                sb.append(line);
                sb.append("\n");
            }
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        } finally {
            try {
                reader.close();
                inputStreamReader.close();
                fileInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return sb.toString();
    }

stackoverflow上的一个回答:
在这里插入图片描述
在这里插入图片描述

项目之前的compileSdkVersion是28,而项目新版本的源码更新了compileSdkVersion是30,而Android系统从api 29开始不能根据MediaStore.Files.FileColumns.DATA字段直接读取ContentResolver中的文件内容,要根据文件描述符FileDescriptor进行读取文件内容。

参考:
https://stackoverflow.com/questions/61809732/android-data-does-not-exist-on-getcontentresolver

这个错误是因为在插入联系人信息的时候,没有正确地获取到新插入的RawContact的id。在这段代码中,我们尝试通过调用`contentResolver.insert()`方法来插入联系人信息,但是这个方法会返回新插入的记录的URI。我们需要从这个URI中提取新插入联系人的RawContact的id,然后在插入电话号码和电子邮件地址的时候使用这个id。 具体来说,我们应该在插入联系人信息之前,先插入一个空的RawContact记录,然后再根据返回的URI获取到这个记录的id。代码可以修改为以下形式: ``` val rawContact = ContentValues() val rawContactUri = contentResolver.insert(ContactsContract.RawContacts.CONTENT_URI, rawContact) val rawContactId = ContentUris.parseId(rawContactUri) ``` 然后在插入电话号码和电子邮件地址的时候,使用这个`rawContactId`即可。 ``` val phone = ContentValues().apply { put(ContactsContract.Data.RAW_CONTACT_ID, rawContactId) put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE) put(ContactsContract.CommonDataKinds.Phone.NUMBER, contacti.phone) } contentResolver.insert(ContactsContract.Data.CONTENT_URI, phone) val email = ContentValues().apply { put(ContactsContract.Data.RAW_CONTACT_ID, rawContactId) put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE) put(ContactsContract.CommonDataKinds.Email.ADDRESS, contacti.email) } contentResolver.insert(ContactsContract.Data.CONTENT_URI, email) ```
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值