我正在尝试将从文件选择器意图中选择的文件(不仅限于图像,可以是任何文件)放入 zip 文件中。我需要完整的文件路径才能做到这一点,但意图只提供 uri 路径。
- 我尝试了 .getPath(),但那并没有给出文件的真实路径
- 我尝试了 getRealPathFromRealURI: android get real path by Uri.getPath()
- 我尝试了 File file = new File(), file.getPath()
这是我的代码:
public void onActivityResult(int requestCode, int resultCode, Intent result){
if(requestCode == 111) {
if(null != result) { // checking empty selection
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
if(null != result.getClipData()) { // checking multiple selection or not
for(int i = 0; i < result.getClipData().getItemCount(); i++) {
String uri = result.getClipData().getItemAt(i).getUri().getPath();
uriList.add(uri);
Log.d("PATH: ",uri);
}
confirmationDialog();
} else {
String uri = result.getData().getPath();
uriList.add(uri);
Log.d("PATH: ",uri);
confirmationDialog();
}
}else{Toast.makeText(getApplicationContext(),
"An error has occured: API level requirements not met",Toast.LENGTH_SHORT).show();};
}
}
}
它应该给出真实路径,例如:"/sdcard/filename.example",但它实际上给出了这个路径:"/document/9016-4ef8:filename.example"。
file.getAbsoultePath()
- Jay Kumar Panera