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