0
我正在編寫一個應用程序來使用手電筒和相機傳輸和接收文件。 現在我試圖選擇文件並在發送片段中顯示其名稱。如何獲取文件名並在列表中顯示
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
browse = (Button)getActivity().findViewById(R.id.browse);
browse.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent chooseFile;
Intent intent;
chooseFile = new Intent(Intent.ACTION_GET_CONTENT);
chooseFile.setType("file/*");
intent = Intent.createChooser(chooseFile, "Choose a file");
startActivityForResult(intent, ACTIVITY_CHOOSE_FILE);
}
});
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case 1:
if (resultCode == Activity.RESULT_OK) {
Uri uri = data.getData();
String uriString = uri.toString();
File myFile = new File(uriString);
String displayName = null;
if (uriString.startsWith("file://")) {
displayName = myFile.getName();
}
}
break;
}
super.onActivityResult(requestCode, resultCode, data);
}
如何顯示列表中所選文件的名稱? 然後,如何發送選定的文件?