I want to download file by using PRDownloader library but I have two equations
first: how to use the library to download file ? ...
second: what must I put in String dirPath
?....
My MainActivity
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
PRDownloader.initialize(this);
PRDownloaderConfig config = PRDownloaderConfig.newBuilder()
.setDatabaseEnabled(true)
.build();
PRDownloader.initialize(getApplicationContext(), config);
String url = "https://firebasestorage.googleapis.com/v0/b/mohammed-1f35b.appspot.com/o/fear-storynory-jana.mp3?alt=media&token=4d0fc7a3-2b95-4ddd-8ecc-c792a09ab539";
String fileName ="myPhoto";
String dirPath = "????????";
int downloadId = PRDownloader.download(url, dirPath, fileName)
.build()
.setOnStartOrResumeListener(new OnStartOrResumeListener() {
@Override
public void onStartOrResume() {
}
})
.setOnPauseListener(new OnPauseListener() {
@Override
public void onPause() {
}
})
.setOnCancelListener(new OnCancelListener() {
@Override
public void onCancel() {
}
})
.setOnProgressListener(new OnProgressListener() {
@Override
public void onProgress(Progress progress) {
}
})
.start(new OnDownloadListener() {
@Override
public void onDownloadComplete() {
}
@Override
public void onError(Error error) {
}
});
PRDownloader.pause(downloadId);
PRDownloader.cancel(downloadId);
}
}
Here is an example how to download with PRDownloader:
private int downloadFile(url){
String fileName = url.substring(url.lastIndexOf('/')+1);
String dirPath = getFilesDir().getAbsolutePath()+File.separator+"downloads";
//String dirPath = android.os.Environment.getExternalStorageDirectory().getAbsolutePath()+File.separator+"downloads";
return /*download id*/ PRDownloader.download(url, dirPath, fileName)
.build()
.start(new OnDownloadListener() {
@Override
public void onDownloadComplete() {
log("download complete!");
}
@Override
public void onError(Error error) {
log("onError: " +error.toString());
}
});
}
fileName
is what the file will be stored as once downloaded.
dirPath
is where it will be stored.
Example on how to call it:
String url = "https://www.your-url.com/files/file.txt"
int downloadId = downloadFile(url);
You can store files either on internal storage with getFilesDir()
, or external storage with getExternalStorageDirectory()
.
You do not need any special permissions to store to internal storage, however the data will not be accessible by the user. It will also be deleted, if the user deletes the app.
If you store on external storage, you need to request permission from the user. He will be able to see the files (using a file browser, for example).