我的示例应用程序是打开文件选择器并选择文件/目录,然后将路径放入 EditText,我尝试了这个问题中的方法,我得到了这个结果
public class MainActivity extends AppCompatActivity {
private Button browse;
private EditText editTextPath;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
browse = (Button) findViewById(R.id.browse);
editTextPath = (EditText) findViewById(R.id.path);
browse.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/*");
intent.addCategory(Intent.CATEGORY_OPENABLE);
startActivityForResult(Intent.createChooser(intent,"Select file or dir"), 1);
setResult(Activity.RESULT_OK);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1 && resultCode == Activity.RESULT_OK) {
String Fpath = data.getDataString();
editTextPath.setText(Fpath);
}
}
}
我想将这样的内部和外部存储添加到文件选择器