意图选择文件:
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("*/*");
startActivityForResult(intent, FILE_REQUEST_CODE);
onActivityResult代码:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case FILE_REQUEST_CODE:
if (resultCode == RESULT_OK) {
Uri uri = data.getData();
assert uri != null;
String uriString = uri.toString();
File myFile = new File(uri.getPath());
String path = getPathFromUri(uri);
}
}
}
获取路径的代码是
private String getPathFromUri(Uri uri) {
String[] filePathColumn = { MediaStore.Files.FileColumns.DATA };
Cursor cursor = getContentResolver().query(uri,filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String Path = cursor.getString(columnIndex);
cursor.close();
return Path;
}
路径返回为null,我已经尝试了很多东西file.getPath()也不起作用...我错过了什么
你有AndroidManifest的读写外部存储权限吗?