StandaloneFileBrowserFragment.java 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. package com.sfb.standalonefilebrowser;
  2. import android.app.Activity;
  3. import android.app.Fragment;
  4. import android.content.ContentResolver;
  5. import android.content.Intent;
  6. import android.database.Cursor;
  7. import android.net.Uri;
  8. import java.io.File;
  9. import java.io.FileOutputStream;
  10. import java.io.InputStream;
  11. import java.io.OutputStream;
  12. public class StandaloneFileBrowserFragment extends Fragment {
  13. public static final int PICKFILE_RESULT_CODE = 1;
  14. private StandaloneFileBrowserAndroidListener fileBrowserAndroidListener;
  15. private String title;
  16. private Boolean multiple;
  17. public void setFileBrowserAndroidListener(final StandaloneFileBrowserAndroidListener fileBrowserAndroidListener) {
  18. this.fileBrowserAndroidListener = fileBrowserAndroidListener;
  19. }
  20. public void setTitle(final String title) {
  21. this.title = title;
  22. }
  23. public void setMultiple(final Boolean multiple) {
  24. this.multiple = multiple;
  25. }
  26. @Override
  27. public void onStart () {
  28. super.onStart ();
  29. final Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
  30. if (title != null) {
  31. intent.putExtra("android.intent.extra.TITLE", title);
  32. }
  33. if (multiple) {
  34. intent.putExtra("android.intent.extra.ALLOW_MULTIPLE", true);
  35. }
  36. intent.setType("*/*");
  37. startActivityForResult(intent, PICKFILE_RESULT_CODE);
  38. }
  39. private String getFileCopyPath(final Uri uri) {
  40. if (uri == null) {
  41. return null;
  42. }
  43. final ContentResolver contentResolver = getActivity().getContentResolver();
  44. Cursor cursor = null;
  45. String filename = null;
  46. try {
  47. cursor = contentResolver.query(uri, null, null, null, null);
  48. if (cursor != null && cursor.moveToFirst()) {
  49. filename = cursor.getString(cursor.getColumnIndex("_display_name"));
  50. }
  51. }
  52. catch (Exception e) {
  53. return null;
  54. }
  55. finally {
  56. if (cursor != null) {
  57. cursor.close();
  58. }
  59. }
  60. try {
  61. final InputStream input = contentResolver.openInputStream(uri);
  62. if (input == null) {
  63. return null;
  64. }
  65. final File file = new File(getActivity().getCacheDir(), filename);
  66. OutputStream output = null;
  67. try {
  68. output = new FileOutputStream(file, false);
  69. final byte[] buffer = new byte[4096];
  70. int len;
  71. while ((len = input.read(buffer)) > 0) {
  72. output.write(buffer, 0, len);
  73. }
  74. return file.getAbsolutePath();
  75. }
  76. finally {
  77. if (output != null) {
  78. output.close();
  79. }
  80. input.close();
  81. }
  82. }
  83. catch (Exception e) {
  84. return null;
  85. }
  86. }
  87. @Override
  88. public void onActivityResult(int requestCode, int resultCode, Intent data) {
  89. super.onActivityResult(requestCode, resultCode, data);
  90. if (fileBrowserAndroidListener == null) {
  91. return;
  92. }
  93. switch (requestCode) {
  94. case PICKFILE_RESULT_CODE:
  95. String filenames = "";
  96. if (resultCode == Activity.RESULT_OK) {
  97. if (data.getClipData() != null) {
  98. for (int i = 0; i < data.getClipData().getItemCount(); ++i) {
  99. if (filenames != "") {
  100. filenames += "|";
  101. }
  102. filenames += getFileCopyPath(data.getClipData().getItemAt(i).getUri());
  103. }
  104. }
  105. else if (data.getData() != null) {
  106. filenames = getFileCopyPath(data.getData());
  107. }
  108. }
  109. fileBrowserAndroidListener.onFilesSelected(filenames);
  110. break;
  111. }
  112. getFragmentManager().beginTransaction().remove(this).commit();
  113. }
  114. }