Guest User

Untitled

a guest
Feb 17th, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.17 KB | None | 0 0
  1. public class AttachmentsFragment extends Fragment {
  2.  
  3. private Button btncf;
  4. private TextView tvaf;
  5.  
  6. @Override
  7. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  8. Bundle savedInstanceState) {
  9. View view = inflater.inflate(R.layout.fragment_attachments,container,false);
  10.  
  11. if(Build.VERSION.SDK_INT > Build.VERSION_CODES.M && ActivityCompat.checkSelfPermission(getContext(),android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
  12. != PackageManager.PERMISSION_GRANTED) {
  13. requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1001);
  14. }
  15.  
  16. btncf = view.findViewById(R.id.btn_choose_files);
  17. tvaf = view.findViewById(R.id.tv_choose_files_placeholder);
  18.  
  19. btncf.setOnClickListener(new View.OnClickListener() {
  20. @Override
  21. public void onClick(View v) {
  22. new MaterialFilePicker()
  23. .withActivity(getActivity())
  24. .withRequestCode(1000)
  25. .withHiddenFiles(true) // Show hidden files and folders
  26. .start();
  27. }
  28. });
  29. return view;
  30. }
  31.  
  32. @Override
  33. public void onActivityResult(int requestCode, int resultCode, Intent data) {
  34. super.onActivityResult(requestCode, resultCode, data);
  35.  
  36. startActivityForResult(data, requestCode);
  37.  
  38. if (requestCode == 1000 && resultCode == RESULT_OK) {
  39. String filePath = data.getStringExtra(FilePickerActivity.RESULT_FILE_PATH);
  40. // Do anything with file
  41. tvaf.setText(filePath);
  42. }
  43. }
  44.  
  45. @Override
  46. public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
  47. switch (requestCode) {
  48. case 1001: {
  49. if(grantResults[0] == PackageManager.PERMISSION_GRANTED) {
  50. Toast.makeText(getActivity(), "Permission not granted!", Toast.LENGTH_SHORT).show();
  51. isRemoving();
  52. }
  53. }
  54. }
  55. }
  56. }
Add Comment
Please, Sign In to add comment