我正在尝试将文件附加到我的项目中。但路径不会显示在文本视图中。代码中没有错误,但似乎找不到路径。我是新手创建一个片段。我怎样才能获得字符串路径?
以下是我的代码:
public class AttachmentsFragment extends Fragment {
private Button btncf;
private TextView tvaf;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_attachments,container,false);
if(Build.VERSION.SDK_INT > Build.VERSION_CODES.M && ActivityCompat.checkSelfPermission(getContext(),android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1001);
}
btncf = view.findViewById(R.id.btn_choose_files);
tvaf = view.findViewById(R.id.tv_choose_files_placeholder);
btncf.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new MaterialFilePicker()
.withActivity(getActivity())
.withRequestCode(1000)
.withHiddenFiles(true) // Show hidden files and folders
.start();
}
});
return view;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
startActivityForResult(data, requestCode);
if (requestCode == 1000 && resultCode == RESULT_OK) {
String filePath = data.getStringExtra(FilePickerActivity.RESULT_FILE_PATH);
// Do anything with file
tvaf.setText(filePath);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode) {
case 1001: {
if(grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(getActivity(), "Permission not granted!", Toast.LENGTH_SHORT).show();
isRemoving();
}
}
}
}
}
答案 0 :(得分:2)
onActivityResult
内有错误,
删除startActivityForResult(data, requestCode);
工作函数应如下所示:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1000 && resultCode == RESULT_OK) {
String filePath = data.getStringExtra(FilePickerActivity.RESULT_FILE_PATH);
// Do anything with file
tvaf.setText(filePath);
}
}
在片段中使用此库时,您需要
.withSupportFragment(this)
另外,我建议检查buttonClick的权限,而不是onCreateView。
功能齐全的代码如下所示:
public class AttachmentsFragment extends Fragment {
public static final int PERMISSIONS_REQUEST_CODE = 0;
public static final int FILE_PICKER_REQUEST_CODE = 1;
public AttachmentsFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_attachments, container, false);
Button pickButton = view.findViewById(R.id.btn_choose_files);
pickButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
checkPermissionsAndOpenFilePicker();
}
});
return view;
}
private void checkPermissionsAndOpenFilePicker() {
String permission = Manifest.permission.READ_EXTERNAL_STORAGE;
if (ContextCompat.checkSelfPermission(getContext(), permission) != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(getActivity(), permission)) {
showError();
} else {
ActivityCompat.requestPermissions(getActivity(), new String[]{permission}, PERMISSIONS_REQUEST_CODE);
}
} else {
openFilePicker();
}
}
private void openFilePicker() {
new MaterialFilePicker()
.withSupportFragment(this)
.withRequestCode(FILE_PICKER_REQUEST_CODE)
.withHiddenFiles(true)
.withTitle("Sample title")
.start();
}
private void showError() {
Toast.makeText(getContext(), "Allow external storage reading", Toast.LENGTH_SHORT).show();
}
@Override
public void onRequestPermissionsResult(int requestCode,
@NonNull String permissions[], @NonNull int[] grantResults) {
switch (requestCode) {
case PERMISSIONS_REQUEST_CODE: {
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
openFilePicker();
} else {
showError();
}
}
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == FILE_PICKER_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
String path = data.getStringExtra(FilePickerActivity.RESULT_FILE_PATH);
if (path != null) {
Log.d("Path (fragment): ", path);
Toast.makeText(getContext(), "Picked file in fragment: " + path, Toast.LENGTH_LONG).show();
}
}
}