1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
import android.app.Activity; import android.content.Context; import android.content.Intent; import android.database.Cursor; import android.net.Uri; import android.provider.OpenableColumns; import android.widget.ImageView; import android.widget.Toast; import com.bumptech.glide.Glide; import com.bumptech.glide.request.RequestOptions; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; import java.io.InputStream; public class DocPickerClass { private static int RESULT_LOAD_IMAGE = 783; private DocPickerMethod docPickerMethod; private Activity activity; public DocPickerClass(DocPickerMethod docPickerMethod, Activity activity) { this.docPickerMethod = docPickerMethod; this.activity = activity; } public void openPicker() { Intent intent = new Intent(Intent.ACTION_GET_CONTENT); intent.setType("*/*"); intent.addCategory(Intent.CATEGORY_OPENABLE); /*String[] mimeTypes = {"application/pdf", "application/msword", "application/xlsx"}; intent.putExtra(Intent.EXTRA_MIME_TYPES, mimeTypes); intent.setAction(Intent.ACTION_GET_CONTENT);*/ try { activity.startActivityForResult( Intent.createChooser(intent, "Select a File to Upload"), RESULT_LOAD_IMAGE); } catch (android.content.ActivityNotFoundException ex) { // Potentially direct the user to the Market with a Dialog Toast.makeText(activity, "Please install a File Manager.", Toast.LENGTH_SHORT).show(); } } public void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == RESULT_LOAD_IMAGE && resultCode == Activity.RESULT_OK && data != null) { Uri uri = data.getData(); try { if (uri != null) { String path = uri.getPath(); File file = new File(path); if(file.exists()){ docPickerMethod.selectedFile(file); }else{ Cursor cursor = activity.getApplicationContext().getContentResolver() .query(uri, null, null, null, null, null); if (cursor != null && cursor.moveToFirst()) { byte[] fileData = readTextFromUri(uri); // Note it's called "Display Name". This is // provider-specific, and might not necessarily be the file name. String fileName = cursor.getString( cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME)); System.out.println("displayName: " + fileName); cursor.close(); docPickerMethod.fileData(fileData, fileName ); } } } // System.out.println(fileData); } catch (IOException e) { docPickerMethod.docPickingError(e); }catch (Exception e){ docPickerMethod.docPickingError(e); } } } /*private String readTextFromUri(Uri uri) throws IOException { InputStream inputStream = activity.getContentResolver().openInputStream(uri); BufferedReader reader = new BufferedReader(new InputStreamReader( inputStream)); StringBuilder stringBuilder = new StringBuilder(); String line; while ((line = reader.readLine()) != null) { stringBuilder.append(line); } return stringBuilder.toString(); }*/ private byte[] readTextFromUri(Uri uri) throws IOException { InputStream inputStream = activity.getContentResolver().openInputStream(uri); // this dynamically extends to take the bytes you read ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream(); // this is storage overwritten on each iteration with bytes int bufferSize = 1024; byte[] buffer = new byte[bufferSize]; // we need to know how may bytes were read to write them to the byteBuffer int len = 0; while ((len = inputStream.read(buffer)) != -1) { byteBuffer.write(buffer, 0, len); } // and then we can return your byte array. return byteBuffer.toByteArray(); } public interface DocPickerMethod { void fileData(byte[] data, String fileName); void selectedFile(File file); void docPickingError(Exception ex); } } |
implement ImagePickerClass.ImagePickerMethod in your activity or fragment
1 2 3 4 5 6 7 8 |
@Override public void fileData(byte[] data, String fileName) { } @Override public void selectedFile(File file) { } |
initlize object
1 2 |
private DocPickerClass docPickerClass; docPickerClass = new DocPickerClass(this, this); |
call
1 |
docPickerClass.openPicker(); |
in onActivityResult
1 2 3 4 5 |
@Override public void onActivityResult(int requestCode, int resultCode, Intent data) { if (docPickerClass != null) docPickerClass.onActivityResult(requestCode, resultCode, data); } |