ProgressBar during download with DownloadManager and sleeping thread in Android - android

I use the DownloadManager to download a xml file from a URL. Then I use a Thread to wait 2 seconds to complete saving the file to the sd card.
I would like to have a activity circle as shown here. What is the easiest way to realize this? Do I need to implement a AsyncTask?
My code to download and wait:
//Download XML file from URL
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(URL));
request.setTitle("Download von "+Name+".xml");
// in order for this if to run, you must use the android 3.2 to compile your app
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
}
request.setDestinationInExternalPublicDir(FileSeperator+"XML"+FileSeperator, Name + FileExtension);
// get download service and enqueue file
DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
manager.enqueue(request);
File file = new File(Environment.getExternalStorageDirectory()+ FileSeperator
+"XML"+FileSeperator+ Name + FileExtension);
System.out.println("File existiert "+file.exists());
//insert delay after download to finish save progress before starting to parse the xml
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
UPDATE
Here is my implemented AsyncTask
private class DownloadFile extends AsyncTask<String, Integer, String> {
#Override
protected String doInBackground(String... sUrl) {
try {
//Download XML file from URL
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(URL));
request.setTitle("Download von "+Name+".xml");
// in order for this if to run, you must use the android 3.2 to compile your app
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
}
request.setDestinationInExternalPublicDir(FileSeperator+"XML"+FileSeperator, Name + FileExtension);
// get download service and enqueue file
DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
manager.enqueue(request);
File file = new File(Environment.getExternalStorageDirectory()+ FileSeperator
+"XML"+FileSeperator+ Name + FileExtension);
System.out.println("File existiert "+file.exists());
//insert delay after download to finish save progress before starting to parse the xml
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
} catch (Exception e) {
}
return null;
}
#Override
protected void onPreExecute() {
super.onPreExecute();
pDialog.show();
}
protected void onPostExecute() {
super.onPreExecute();
pDialog.dismiss();
}
#Override
protected void onProgressUpdate(Integer... progress) {
super.onProgressUpdate(progress);
}
}
And I call it like that:
// instantiate it within the onCreate method
pDialog = new ProgressDialog(CreateProject.this);
pDialog.setMessage("Lädt...");
pDialog.setIndeterminate(true);
// execute this when the downloader must be fired
DownloadFile downloadFile = new DownloadFile();
downloadFile.execute();

I think yes, you should implement this with AsynsTask class, it's clear, fast and easy. You can read a short tutorial about AsyncTask here

You can very well full fill your requirement with asynctask onPreExecute() show your progress dialog do your process in the doInBackground() and onPostExecture dismiss the dialog and show your result.

Just call super.onPostExecute() instead of super.onPreExecute() in the subclass onPostExecute or it won't work well

Related

Reusing code for sharing/saving pdf document

So I have a fragment where I show the user terms and conditions for something and those terms and conditions are in the form of pdf file which is retrieved from the server.
This is the code that retrieves the pdf and gives the pdfView an inputstream to show the data.
class RetrievePDFFromUrl extends AsyncTask<String, Void, InputStream> {
#Override
protected InputStream doInBackground(String... strings) {
InputStream inputStream = null;
try {
URL url = new URL(strings[0]);
HttpURLConnection urlConnection = (HttpsURLConnection) url.openConnection();
if (urlConnection.getResponseCode() == 200) {
inputStream = new BufferedInputStream(urlConnection.getInputStream());
}
} catch (IOException e) {
e.printStackTrace();
return null;
}
return inputStream;
}
#Override
protected void onPostExecute(InputStream inputStream) {
pdfView.fromStream(inputStream).load();
}
}
So far so good.
But now I have to add the functionality to share and save the document.
The problem is that I have to use other pieces of code to accomplish the task.
And since I cant share the document without downloading it, its a little messy.
Heres how I download the document.
private void downloadPDFContent(){
String fileName = getCurrentDocumentName();;
String urlToDownload = !secondDocument ? documentUrl1 : documentUrl2;
File outputFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), fileName);
if (outputFile.exists()) {
return;
}
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(urlToDownload));
request.setTitle(fileName);
request.setMimeType("application/pdf");
request.allowScanningByMediaScanner();
request.setAllowedOverMetered(true);
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName);
DownloadManager downloadManager = (DownloadManager) getContext().getSystemService(Context.DOWNLOAD_SERVICE);
downloadManager.enqueue(request);
}
The problem comes when trying to share the document, Its just wrong to put 200ms delay before trying to share it, because no one knows how slow a connection can be sometimes and it wont work.
private void shareDocument() {
downloadPDFContent();
new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
#Override
public void run() {
File outputFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), getCurrentDocumentName());
Uri uri = FileProvider.getUriForFile(getContext(),
getContext().getPackageName() + ".provider", outputFile);
Intent share = new Intent();
share.setAction(Intent.ACTION_SEND);
share.setType("application/pdf");
share.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
share.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(share, "Share document"));
}
}, 200);
}
Does anyone have any better ideas how can I achieve those 3 tasks - using inputstream to load the doc for the user to view and to share/save it also while reusing code and not doing it in different and unstable ways?
**UPDATE: I added a broadcastreceiver to start when the downloading is finished, instead of waiting a fixed amount of time like i do here.
Which is one idea better but still not what i wanted.
You have been using many legacy tools for this task and it is not clear do you have business constraints for it or not.
If your business use case just to download pdf and share it with another android app within your device, I would use Kotlin Flow for the async download task.
When you download your pdf and save it in storage, you could use a callback from Kotlin Flow as a trigger for your sharing intent. You would not need anymore any delay.
Please note, depends on your business use case you could use ContentProvider to give access to your app's files and p2p 3rd party tools for downloading and sharing your files.

Downloading file with DownloadManager Android

I am trying to download a file using the DownloadManager inside an AsyncTask
private class DownloadTask extends AsyncTask<String, Void, Boolean>
{
private Context mContext;
public DownloadTask(Context context)
{
mContext = context;
}
#Override
protected Boolean doInBackground(String... strings) {
String fileName = strings[2]+"_"+strings[3]+ strings[4];
String destination = mDestination + fileName;
final Uri uri = Uri.parse("file://" + destination);
mDownloading = true;
//If the file is already downloading just return.
File file = new File(destination);
if (file.exists()) {
return true;
}
//set downloadmanager
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(strings[0]));
request.setDescription(mContext.getString(R.string.downloading)+ " "+strings[1]);
request.setTitle(mContext.getString(R.string.downloading_title));
//set destination
request.setDestinationUri(uri);
// get download service and enqueue file
final DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
final long downloadId = manager.enqueue(request);
//set BroadcastReceiver to enable next download
BroadcastReceiver onComplete = new BroadcastReceiver() {
public void onReceive(Context ctxt, Intent intent) {
unregisterReceiver(this);
mDownloading = false;
}
};
//register receiver for when file download is compete
registerReceiver(onComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
return true;
}
}`
The code works fine when I pass the url of the file. The thing is, that we want to make a GET call to a WEB made on PHP. This WEB method creates or selects a file and redirects using header("Location: ".$database->single()['Url']);
But when we make the call from DownloadManager it just calls registerReceiver right away.
Does anyone knows why this happens?
If we use a HttpURLConnection it works fine, but we would like to delegate all the hard work of the download in the DownloadManager.
Thank you for your comments.
3xx: redirects is't supported by DownloadManger. Source code at line 510
And it will download the redirect response and finish right way.
So you should get the response head[Location] by yourself, and pass it to the task.
By the way, you needn't put the download task in the AsyncTask.

downloading files using dropbox url android

I am trying to download files using dropbox url. I copied a code from Download a file with Android, and showing the progress in a ProgressDialog which uses DownloadManager class.
public void downloadFromDropBoxUrl(View view) {
//verfying if the downloadmanager is available first.
if (isDownloadManagerAvailable(getApplication())) {
String url = "https://www.dropbox.com/s/m4z5u9qstxdtbc3/AllExams22.pdf?dl=0";
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
request.setDescription("Some descrition");
request.setTitle("Some title");
// in order for this if to run, you must use the android 3.2 to compile your app
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
}
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "my-map.pdf");
// get download service and enqueue file
DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
manager.enqueue(request);
}
}
public static boolean isDownloadManagerAvailable(Context context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
return true;
}
return false;
}
It is working on achiving direct files with urls but this time I am trying to do it with dropbox share links but its not working out. I don't want to connect to dropbox api. I think it is useless. Is there any way I can download files directly from the dropbox url?
just replace
String url = "https://www.dropbox.com/s/m4z5u9qstxdtbc3/AllExams22.pdf?dl=0";
by:
String url = "https://dl.dropboxusercontent.com/s/m4z5u9qstxdtbc3/AllExams22.pdf";
Then:
final DownloadTask downloadTask = new DownloadTask(YourActivity.this);
downloadTask.execute(url);
Please see this link on how to download a shared file from Dropbox
https://blogs.dropbox.com/developers/2013/08/programmatically-download-content-from-share-links/

Control the download ordering of Download manager in android

have an use case like the following:
There are several files to download e.g. A B C D E F
When the downloading is started , say the A B is finished and C is downloading, I would like to interrupt the download of C and start the download of E
Then, after E is finished (if there is no other interruption), continue to C D F.
So far form my research there is only cancel method
downloadManager.remove(downloadReference);
How to achieve this through Download manager or are there other approach ? thanks
private long startDownload(String url) {
Uri DownloadUri = Uri.parse(url);
String fileName = StorageUtils.getFileNameFromUrl(url);
String destination = null;
downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
DownloadManager.Request request = new DownloadManager.Request(
DownloadUri);
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE);
request.setAllowedOverRoaming(false);
request.setTitle(fileName);
request.setDescription("com.example.services");
if (StorageUtils.isSDCardPresent()
&& StorageUtils.isSdCardWrittenable()
&& StorageUtils.checkAvailableStorage()) {
destination = StorageUtils.SDCARD_ROOT;
}
try {
StorageUtils.mkdir();
} catch (IOException e) {
e.printStackTrace();
}
request.setDestinationInExternalPublicDir(destination, fileName);
downloadReference = downloadManager.enqueue(request);
Log.d("Downloader","Start download manager: " + destination + fileName);
return downloadReference;
}
Regarding this answer, it looks like you can cancel the download and then download the rest of the file. For example:
Register a BrodcastReciever to notify you when C is completed:
BroadcastReceiver onComplete = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
//check if it is B that is complete
//cancel C
// download E
//check if it is E that is complete
// Open connection to URL.
HttpURLConnection connection =
(HttpURLConnection) url.openConnection();
// Specify what portion of file to download.
connection.setRequestProperty("Range", "bytes=" + downloaded + "-");
// here "downloaded" is the data length already previously downloaded.
// Connect to server.
connection.connect();
}
};
registerReceiver(onComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
//download A
//download B
//download C

Downloading an mp3 file from server in android

I am trying to create an app that can download music files, .mp3 to be precise, from the server.As I am a rookie in this Android Development field so I will appreciate any help from you guys.
I need something to start on and I will really appreciate if u can give me some links for useful resources.
Thanks
If you want to play the .mp3 file from any url then follow the code suggested by nik.
But if you want to download a file form the server and store it in any place on sdcard or internal storage device then follow this code,
private class DownloadFile extends AsyncTask<String, Integer, String>{
#Override
protected String doInBackground(String... urlParams) {
int count;
try {
URL url = new URL("url of your .mp3 file");
URLConnection conexion = url.openConnection();
conexion.connect();
// this will be useful so that you can show a tipical 0-100% progress bar
int lenghtOfFile = conexion.getContentLength();
// downlod the file
InputStream input = new BufferedInputStream(url.openStream());
OutputStream output = new FileOutputStream("/sdcard/somewhere/nameofthefile.mp3");
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
// publishing the progress....
publishProgress((int)(total*100/lenghtOfFile));
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
} catch (Exception e) {}
return null;
}
EDIT: manifest permission:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
You can do it this way:
try {
MediaPlayer player = new MediaPlayer();
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
player.setDataSource("http://xty/MRESC/images/test/xy.mp3");
player.prepare();
player.start();
} catch (Exception e) {
// TODO: handle exception
}
Manifest permission:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
Use this method
private void beginDownload(){
/*
Create a DownloadManager.Request with all the information necessary to start the download
*/
DownloadManager.Request request=new DownloadManager.Request(Uri.parse("http://examplewebsite.com/aaa.mp3"))
.setTitle("Dummy File")// Title of the Download Notification
.setDescription("Downloading")// Description of the Download Notification
.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE)// Visibility of the download Notification
// Uri of the destination file
.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "" + System.currentTimeMillis());
.setRequiresCharging(false)// Set if charging is required to begin the download
.setAllowedOverMetered(true)// Set if download is allowed on Mobile network
.setAllowedOverRoaming(true);// Set if download is allowed on roaming network
DownloadManager downloadManager= (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
downloadID = downloadManager.enqueue(request);// enqueue puts the download request in the queue.
}
Here is the full code
private Button btnDownload;
private long downloadID;
private BroadcastReceiver onDownloadComplete = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
//Fetching the download id received with the broadcast
long id = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
//Checking if the received broadcast is for our enqueued download by matching download id
if (downloadID == id) {
Toast.makeText(MainActivity.this, "Download Completed", Toast.LENGTH_SHORT).show();
}
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button=findViewById(R.id.download);
registerReceiver(onDownloadComplete,new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
btnDownload.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
beginDownload();
}
});
}
#Override
public void onDestroy() {
super.onDestroy();
unregisterReceiver(onDownloadComplete);
}
private void beginDownload(){
File file=new File(getExternalFilesDir(null),"Dummy");
/*
Create a DownloadManager.Request with all the information necessary to start the download
*/
DownloadManager.Request request=new DownloadManager.Request(Uri.parse("http://speedtest.ftp.otenet.gr/files/test10Mb.db"))
.setTitle("Dummy File")// Title of the Download Notification
.setDescription("Downloading")// Description of the Download Notification
.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE)// Visibility of the download Notification
.setDestinationUri(Uri.fromFile(file))// Uri of the destination file
.setRequiresCharging(false)// Set if charging is required to begin the download
.setAllowedOverMetered(true)// Set if download is allowed on Mobile network
.setAllowedOverRoaming(true);// Set if download is allowed on roaming network
DownloadManager downloadManager= (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
downloadID = downloadManager.enqueue(request);// enqueue puts the download request in the queue.

Categories

Resources