Developers: Use System Explorer to select directories or files
Jul 05
Applications, Development, System Explorer, Tutorial Android, Intent, PICK_DIRECTORY, PICK_FILE, System Explorer No Comments
I have added PICK_FILE and PICK_DIRECTORY intents to System Explorer so that developers can use it to select files or directories. You can also pass the file name or directory to open and System Explorer will start there.
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 | /** * Opens System Explorer to open a file. */ private static final int REQUEST_RESULT = 1; private void openFile(String fileName) { Intent intent = new Intent("com.androidworkz.action.PICK_FILE"); // Construct URI from file name. intent.setData(Uri.parse("file://" + fileName)); intent.putExtra("com.androidworkz.extra.TITLE", "Select a file to open"); intent.putExtra("com.androidworkz.extra.BUTTON_TEXT", "Open"); try { startActivityForResult(intent, REQUEST_RESULT); } catch (ActivityNotFoundException e) { e.printStackTrace(); Toast.makeText(this, "System Explorer is not installed", Toast.LENGTH_SHORT).show(); } } /** * Opens System Explorer to open a directory. */ private void openDirectory(String directoryName) { Intent intent = new Intent("com.androidworkz.action.PICK_DIRECTORY"); // Construct URI from file name. intent.setData(Uri.parse("file://" + directoryName)); // Set fancy title and button (optional) intent.putExtra("com.androidworkz.extra.TITLE", getString(R.string.pick_directory_title)); intent.putExtra("com.androidworkz.extra.BUTTON_TEXT", getString(R.string.pick_directory_button)); try { startActivityForResult(intent, REQUEST_RESULT); } catch (ActivityNotFoundException e) { e.printStackTrace(); Toast.makeText(this, "System Explorer is not installed", Toast.LENGTH_SHORT).show(); } } /** * This is called when the user has selected the file or directory and clicked a button to return to your application. */ @Override protected void onActivityResult(int requestCode, int resultCode, Intent intent) { super.onActivityResult(requestCode, resultCode, intent); switch (requestCode) { case REQUEST_RESULT: if (resultCode == RESULT_OK && intent != null) { // obtain the filename String filename = intent.getDataString(); if (filename != null) { // Get rid of URI prefix: if (filename.startsWith("file://")) { filename = filename.substring(7); } // Do something with the file here } } break; } } |