0

请看下面的代码,因为你可以看到我的 FilePath 只获得这个路径 /document/1393,那么我如何获得像/sdcard/document/test.docx这样的绝对路径?

 // Get Result Back
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    switch(requestCode){
        case FILE_SELECT_CODE:
            if(resultCode==-1){
                String FilePath = data.getData().getPath();
                Log.d("onActivityResult", "FilePath = " + FilePath);
                cityEditText.setText(FilePath);
            }
            break;

    }

}
4

1 回答 1

1

尝试这个:

Uri uri = Uri.fromFile(new File("/path/to/file"));
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
// Verify that there are applications registered to handle this intent
// (resolveActivity returns null if none are registered)
if (intent.resolveActivity(getPackageManager()) != null) {
    startActivity(intent);
}

根据您从哪里执行此代码(在活动、视图、片段中),您可能需要调整getPackageManager()getActivity().getPackageManager()getContext().getPackageManager()

于 2016-10-13T21:36:47.957 回答