Allow user to choose path to save file on Android

I am developing one application for our client, and there is one small functionality where we are stuck, so you need your help,

Scenarion: We developed one type of processing from which users can see a list of images, songs and videos by category, now the User has one option to view or listen to images, audio or video, and there is also another option for downloading it.

Need help @ . We did this with one static path where the user can save all the files, but our client wants to allow users to choose the path to save the files, and for this we need a dialogue with the file, from where the user can select the location.

Note: The guys note that for one static path we did this and it works perfectly, we also save this path in the local database so that we can use it later. So now just stay, how can we allow the user to choose a location to save the file?

+5
source share
1 answer

I think Android DirectoryChooser will help you choose the directory to save the file.

manifest

You need to declare DirectoryChooserActivity and request permission android.permission.WRITE_EXTERNAL_STORAGE .

 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> ... <application> <activity android:name="net.rdrei.android.dirchooser.DirectoryChooserActivity" /> </application> 

activity

To select a directory, run the action from your application logic:

  final Intent chooserIntent = new Intent(this, DirectoryChooserActivity.class); final DirectoryChooserConfig config = DirectoryChooserConfig.builder() .newDirectoryName("DirChooserSample") .allowReadOnlyDirectory(true) .allowNewDirectoryNameModification(true) .build(); chooserIntent.putExtra(DirectoryChooserActivity.EXTRA_CONFIG, config); // REQUEST_DIRECTORY is a constant integer to identify the request, eg 0 startActivityForResult(chooserIntent, REQUEST_DIRECTORY); 

Process the result in the onActivityResult method:

 @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == REQUEST_DIRECTORY) { if (resultCode == DirectoryChooserActivity.RESULT_CODE_DIR_SELECTED) { handleDirectoryChoice(data .getStringExtra(DirectoryChooserActivity.RESULT_SELECTED_DIR)); } else { // Nothing selected } } } 
+7
source

All Articles