我试图使用意图选择文件和activityresult来表示该动作的响应,以便在回收站视图中显示路径文件,因此我的代码在android 4>之前可以工作到7 <,并且对于那些版本来说效果很好,但在7> =该代码存在问题,尤其是在android 9中。
public void openGallery() {
try {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select file to upload "),
SettingsUtil.ACTIVITY_GALLERY);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == SettingsUtil.ACTIVITY_GALLERY && resultCode == RESULT_OK && null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver()
.query(selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
String code = year + "-" + reservation + "-" + (counter++);
File auxFile = new File(picturePath);
}
}
我期望String picturePath = cursor.getString(columnIndex);得到的路径类似于旧版android vesions 4+ <7,但是在那些新版本中,在最新版本中工作时会出现问题