I have been trying to figure out a way for my application to open a file browser where the user can select a video File and have my app store that path into a string to use later. Is there an Intent I can use or function? Any suggestions?
2
- I am not sure what you asking for .dharmendra– dharmendra2014-05-21 05:52:53 +00:00Commented May 21, 2014 at 5:52
- I was asking how I can get my app to open a file browser so the user can select a video. My app would then get the path of the video that the user selected and store it into a string.RedShirt– RedShirt2014-05-21 05:59:51 +00:00Commented May 21, 2014 at 5:59
Add a comment |
3 Answers
You can do it via intent chooser. Try this code i think this solution solve your purpose.
Intent intent = new Intent(); intent.addCategory(Intent.CATEGORY_OPENABLE); // Set your required file type intent.setType("*/*"); intent.setAction(Intent.ACTION_GET_CONTENT); startActivityForResult(Intent.createChooser(intent, "DEMO"),1001); then onActivityResult method.
public void onActivityResult(int requestCode, int resultCode, Intent data) { // TODO Auto-generated method stub super.onActivityResult(requestCode, resultCode, data); // super.onActivityResult(requestCode, resultCode, data); if (requestCode == 1001) { Uri currFileURI = data.getData(); String path=currFileURI.getPath(); }} Comments
Here is a code I have used one of my previous projects.
Intent intent = new Intent(); intent.setType("video/*"); intent.setAction(Intent.ACTION_GET_CONTENT); startActivityForResult(Intent.createChooser(intent,"Select Video "), 33); public void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode == RESULT_OK) { if (requestCode == 33) { Uri selectedMediaUri = data.getData(); filemanagerstring = selectedMediaUri.getPath(); selectedMediaPath = getPath(selectedMediaUri); if (!selectedMediaPath.equals("")) { filePath = selectedMediaPath; } else if (!filemanagerstring.equals("")) { filePath = filemanagerstring; } int lastIndex = filePath.lastIndexOf("/"); fileName = filePath.substring(lastIndex+1); //filepath is your file's path } } } public String getPath(Uri uri) { String[] projection = { MediaStore.MediaColumns.DATA }; Cursor cursor = getContentResolver().query(uri, projection, null, null, null); if (cursor != null) { // HERE YOU WILL GET A NULLPOINTER IF CURSOR IS NULL // THIS CAN BE, IF YOU USED OI FILE MANAGER FOR PICKING THE MEDIA int column_index = cursor .getColumnIndexOrThrow(MediaStore.MediaColumns.DATA); cursor.moveToFirst(); return cursor.getString(column_index); } else return ""; } Comments
use below code.
Intent intent = new Intent(Intent.ACTION_GET_CONTENT); intent.setType("file/*"); startActivityForResult(intent,PICKFILE_RESULT_CODE); and on activity result you get file path.
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { // TODO Auto-generated method stub switch(requestCode){ case PICKFILE_RESULT_CODE: if(resultCode==RESULT_OK){ String FilePath = data.getData().getPath(); textFile.setText(FilePath); } break; } }