I am trying to download multiple file using AsyncTask.
I start each download in one AsyncTask with progress bar in notification bar but i face many problems.
If i download 4 or 5 files at the same time the one or more files
interrupted without any reason.
If i download 4 or 5 images the one or more images corrupted.
this is the code i used.
private class DownloadFile extends AsyncTask<String, Integer, Long>
{
int nID = 0;
int nDownloadCounter = 0;
public DownloadFile(String sFileName, int nNotificationID) {
this.nID = nNotificationID;
this.NotificationName = sFileName;
this.nDownloadCounter = 0;
}
protected void onPreExecute(){
this.sDownloadPath = sFilePathWithSubDir;
notification = new Notification(R.drawable.app_icon, "Download File", System.currentTimeMillis());
RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.custom_notification);
Intent notificationIntent = new Intent();
File file = new File(this.sDownloadPath + this.NotificationName);
sMediaDataType = GSUtilities.sGetFileMIMEType(this.NotificationName);
notificationIntent.setAction(android.content.Intent.ACTION_VIEW);
notificationIntent.setDataAndType(Uri.fromFile(file), sMediaDataType);
PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), 0, notificationIntent, 0);
contentView.setTextViewText(R.id.status_percent, "0%");
contentView.setTextViewText(R.id.status_fileName, this.NotificationName);
contentView.setProgressBar(R.id.status_progress, 100, 0, false);
notification.contentView = contentView;
notification.contentIntent = contentIntent;
mNotificationManager.notify(nID, notification);
}
protected Long doInBackground(String... urls) {
long retData = 0;
OutputStream fosXSPDatabse = null;
InputStream inServerResponse = null;
URLConnection oURLConnection = null;
URL oURL = null;
try
{
oURL = new URL(oFileManager.sExpiryLink);
oURLConnection = oURL.openConnection();
if (!(oURLConnection instanceof HttpURLConnection))
throw new IOException("Not an HTTP connection");
oURLConnection.connect();
inServerResponse = new BufferedInputStream(oURL.openStream());
if (inServerResponse != null)
{
File fDirectory = new File(oFileManager.sAppFlesDirectory);
if (!fDirectory.exists())
{
if (!fDirectory.mkdir())
{}
}
fosXSPDatabse = new FileOutputStream(oFileManager.sAppFlesDirectory + "/" + oFileInfo.getFileName());
byte data[] = new byte[BUFFER_SIZE];
int nCount = 0;
long lTotalDownloaded = 0;
int nTotalSize = oURLConnection.getContentLength();
while ((nCount = inServerResponse.read(data)) != -1)
{
Log.d(String.valueOf(nID) + " - DoInBackground", String.valueOf(dNotificationbarProgress));
nDownloadCounter ++;
lTotalDownloaded += nCount;
dNotificationbarProgress = lTotalDownloaded * 100.0 / nTotalSize;
if (this.nDownloadCounter == 20 || dNotificationbarProgress == 100) {
publishProgress(Integer.parseInt(new DecimalFormat("#.##").format(dNotificationbarProgress).split("\\.")[0]));
nDownloadCounter = 0;
}
fosXSPDatabse.write(data, 0, nCount);
}
inServerResponse.close();
fosXSPDatabse.flush();
fosXSPDatabse.close();
}
}
catch (MalformedURLException e)
{}
catch (IOException e)
{}
catch (Exception e)
{}
finally
{
try
{
inServerResponse.close();
fosXSPDatabse.flush();
fosXSPDatabse.close();
}
catch (IOException e)
{}
}
return retData;
}
protected void onProgressUpdate(Integer... progress) {
try
{
Log.d(String.valueOf(nID),"onProgressUpdate");
notification.contentView.setProgressBar(R.id.status_progress, 100, progress[0], false);
notification.contentView.setTextViewText(R.id.status_fileName, this.NotificationName);
notification.contentView.setTextViewText(R.id.status_percent, String.valueOf(progress[0]) + "%");
Intent notificationIntent = new Intent();
File file = new File(this.sDownloadPath + this.NotificationName);
sMediaDataType = GSUtilities.sGetFileMIMEType(this.NotificationName);
notificationIntent.setAction(android.content.Intent.ACTION_VIEW);
notificationIntent.setDataAndType(Uri.fromFile(file), sMediaDataType);
PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), 0, notificationIntent, 0);
notification.contentIntent = contentIntent;
mNotificationManager.notify(this.nID, notification);
}
catch(Exception e)
{}
}
protected void onPostExecute(Long result) {
notification.contentView.setTextViewText(R.id.status_fileName, "Successfully installed " + this.NotificationName);
notification.contentView.setTextViewText(R.id.status_percent, "100%");
mNotificationManager.notify(this.nID, notification);
}
}
First, it's never a good idea to silently ignore Exceptions. You should at minumum log them to logcat. It think this is the problem.
Second, you're closing the streams multiple times.
Where are you calling the AsyncTask from?
If it's from an activity and the user navigates away from it, that might be terminated any time the OS wants to free up memory.
If it's from a service, that could be terminated too, but at least it is restarted later. If the service was started from a different process, such as a Sync Adapter, you should also post the PreExecute and PublishProgress and PostExecute code to the UI thread with the application looper handler as a message.
In adition, try limiting the number of concurrent downloads to max 3. Otherwise, an OutOfMemory might occur. You can do this with a BlockingQueue. If concurrency is not important, consider using an IntentService instead of an AsyncTask.
The Best way to handle download multiple files is to use the Service.
Related
I have an android application which updates itself from a server, if new version of the apk is present.
The problem is, it is now not working on devices running android 10 and higher. The root cause of the problem is that ACTION_VIEW is deprecated, and no longer supported since android 10. Here is the legacy code which is not working:
private void installapk(File file) {
Log.w(TAG, "Installing new version...");
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
// Otherwise will throw exception, have to install it in new task
intent.setFlags(intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
So after searching for a while now, it seems the only option I have left is to use packagemanager class to do the job. I found this example, which compiles and runs without giving any error message, but it seems like it does nothing.
public static void install(Context context, String packageName, File file) {
InputStream in = null;
OutputStream out = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
PackageInstaller.Session session = null;
try {
in = new FileInputStream(file);
PackageInstaller packageInstaller = context.getPackageManager().getPackageInstaller();
PackageInstaller.SessionParams params = new PackageInstaller.SessionParams(
PackageInstaller.SessionParams.MODE_FULL_INSTALL);
//
params.setAppPackageName(packageName);
// set params
int sessionId = packageInstaller.createSession(params);
session = packageInstaller.openSession(sessionId);
out = session.openWrite(file.getName(), 0, -1);
final byte[] buffer = new byte[65536];
int bytes_read;
while((bytes_read = in.read(buffer)) != -1){
out.write(buffer, 0, bytes_read);
}
session.fsync(out);
in.close();
out.close();
session.commit(createIntentSender(context, sessionId, ACTION_INSTALL_COMPLETE));
} catch (IOException e) {
Log.w(TAG, e.getMessage());
} finally {
;
}
}
}
private static IntentSender createIntentSender(Context context, int sessionId, String action) {
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, sessionId, new Intent(action), 0);
return pendingIntent.getIntentSender();
}
This code runs fine, but as I said, nothing happens as a result. What am I doing wrong?
I'm having a very weird problem with AsyncTask which I use to download a zip file in my android application. It was working flawlessly until I decided to use strings.xml resource for every string linked to this task.
when I click on the download button inside my app, the progressbar of the AsyncTask shows for a second or less then dismisses itself and the task goes to the onPostExecute() state.
I tried debuging the app on my test device and there is no error about the task. I even added some stubs with Log.d tag, I've included the logcat results:
275-15524/xmc.androidexpert35.com.xtrememusicchecker D/ANDRO_ASYNC: path set
2019-04-04 20:19:22.484 15275-15524/xmc.androidexpert35.com.xtrememusicchecker D/ANDRO_ASYNC: Try block
2019-04-04 20:19:22.487 15275-15524/xmc.androidexpert35.com.xtrememusicchecker D/ANDRO_ASYNC: file url got
2019-04-04 20:19:22.490 15275-15524/xmc.androidexpert35.com.xtrememusicchecker D/ANDRO_ASYNC: opening connection
2019-04-04 20:19:22.515 495-528/? D/SurfaceFlinger: duplicate layer name: changing xmc.androidexpert35.com.xtrememusicchecker/xmc.androidexpert35.com.xtrememusicchecker.SettingsActivity to xmc.androidexpert35.com.xtrememusicchecker/xmc.androidexpert35.com.xtrememusicchecker.SettingsActivity#1
2019-04-04 20:19:22.585 495-794/? D/SurfaceFlinger: duplicate layer name: changing Surface(name=cafcbf6 xmc.androidexpert35.com.xtrememusicchecker/xmc.androidexpert35.com.xtrememusicchecker.SettingsActivity)/#0x3604cd - animation-leash to Surface(name=cafcbf6 xmc.androidexpert35.com.xtrememusicchecker/xmc.androidexpert35.com.xtrememusicchecker.SettingsActivity)/#0x3604cd - animation-leash#1
2019-04-04 20:19:22.614 15275-15275/xmc.androidexpert35.com.xtrememusicchecker I/ViewRootImpl: CPU Rendering VSync enable = true
2019-04-04 20:19:22.672 495-528/? W/SurfaceFlinger: Attempting to set client state on removed layer: xmc.androidexpert35.com.xtrememusicchecker/xmc.androidexpert35.com.xtrememusicchecker.SettingsActivity#1
2019-04-04 20:19:22.672 495-528/? W/SurfaceFlinger: Attempting to destroy on removed layer: xmc.androidexpert35.com.xtrememusicchecker/xmc.androidexpert35.com.xtrememusicchecker.SettingsActivity#1
2019-04-04 20:19:24.758 15275-15314/xmc.androidexpert35.com.xtrememusicchecker D/FA: Logging event (FE): user_engagement(_e), Bundle[{firebase_event_origin(_o)=auto, engagement_time_msec(_et)=3635, firebase_screen_class(_sc)=SettingsActivity, firebase_screen_id(_si)=-6495914915605520780}]
2019-04-04 20:19:26.125 829-3715/? W/NotificationService: Toast already killed. pkg=xmc.androidexpert35.com.xtrememusicchecker callback=android.app.ITransientNotification$Stub$Proxy#3b51651
This is my AsyncTask code, if anyone can help me find the issue? or suggest a useful debug solution to discover it?
Thanks, any help is very appreciated!
public class DownloadFile extends AsyncTask<String, String, String> {
private static String file_url;
private Context context;
private ProgressDialog dialog;
private String dialogString;
private File path;
private String xmpath;
private boolean canceled = false;
public DownloadFile(Context cxt) {
context = cxt;
dialog = new ProgressDialog(context);
}
#Override
protected void onPreExecute() {
dialog.setMessage(context.getString(R.string.xm_downloading));
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
dialog.setCancelable(false);
dialog.setButton(DialogInterface.BUTTON_NEGATIVE, context.getString(R.string.xm_cancel), new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
path.delete();
canceled = true;
dialog.dismiss();
}
});
dialog.show();
super.onPreExecute();
}
#Override
protected String doInBackground(String... aurl) {
int count;
if (SettingsActivity.isMagisk){
file_url = "http://androidexpert35developer.altervista.org/Xtrememusic-versions/XTREMEMusic_MAGISK_OFICIAL_By_androidexpert35.zip";
path= new File(Environment.getExternalStorageDirectory() + "/XTREMEMusic_Download/XTREMEMusic_Magisk.zip");
}else{
file_url = "http://androidexpert35developer.altervista.org/Xtrememusic-versions/XTREMEMusic_OFFICIAL_By_androidexpert35.zip";
path = new File(Environment.getExternalStorageDirectory() + "/XTREMEMusic_Download/XTREMEMusic.zip");
Log.d("ANDRO_ASYNC","path set");
}
try {
Log.d("ANDRO_ASYNC","Try block");
URL url = new URL(file_url);
Log.d("ANDRO_ASYNC","file url got");
URLConnection conexion = url.openConnection();
Log.d("ANDRO_ASYNC","opening connection");
conexion.connect();
Log.d("ANDRO_ASYNC","Connected");
int lenghtOfFile = conexion.getContentLength();
InputStream is = url.openStream();
File testDirectory = new File(Environment.getExternalStorageDirectory() + "/XTREMEMusic_Download");
Log.d("ANDRO_ASYNC","making directory");
if (!testDirectory.exists()) {
testDirectory.mkdir();
}
FileOutputStream fos;
Log.d("ANDRO_ASYNC","Stream");
if(SettingsActivity.isMagisk) {
fos = new FileOutputStream(testDirectory + "/" + ("XTREMEMusic_Magisk") + ".zip");
Log.d("ANDRO_ASYNC","Downloading");
}else{
fos = new FileOutputStream(testDirectory + "/" + ("XTREMEMusic") + ".zip");
Log.d("ANDRO_ASYNC","Downloading");
}
byte data[] = new byte[1024];
long total = 0;
int progress = 0;
while ((count = is.read(data)) != -1) {
total += count;
int progress_temp = (int) total * 100 / lenghtOfFile;
publishProgress(""+ progress_temp);
if (progress_temp % 10 == 0 && progress != progress_temp) {
progress = progress_temp;
}
fos.write(data, 0, count);
}
is.close();
fos.close();
} catch (Exception e) {}
return null;
}
protected void onProgressUpdate(String... progress) {
Log.d("ANDRO_ASYNC",progress[0]);
dialog.setProgress(Integer.parseInt(progress[0]));
}
#Override
protected void onPostExecute(String unused) {
dialog.dismiss();
if(SettingsActivity.isInstall) {
installer();
}else if (canceled) {
Toast.makeText(context, R.string.xm_cancelled, Toast.LENGTH_LONG).show();
} else{
xmpath = path.toString();
Toast.makeText(context, context.getString(R.string.xm_downloaded, xmpath), Toast.LENGTH_LONG).show();
}
}
FileNotFoundException occur when capture the screen and save the screenshot into SDCard after power off the cellular phone, It seems that the server to flash the buffer into disk has disconnected during shutdown, but anyone can help me explain more detail? The output error message is:
02-24 14:03:28.180 27412 27412 D TakeScreenshotService:
#isFloatingBallVisible() ,visible = false
02-24 14:03:29.402 27412 10176 E SaveImageInBackgroundTask: error in
SaveImageInBackgroundData,
exception:java.io.FileNotFoundException:
/storage/emulated/0/Pictures/Screenshots/Screenshot_20170224-140329.jpg:
open failed: ENOTCONN (Transport endpoint is not connected)
02-24 14:03:29.526 27412 27412 D TakeScreenshotService: onUnbind,
isMultiScrenshot:false intent:Intent {
cmp=com.android.systemui/.screenshot.TakeScreenshotService }
The corresponding codes are:
#Override
protected SaveImageInBackgroundData doInBackground(SaveImageInBackgroundData... params) {
Log.d(TAG, "doInBackground:");
if (params.length != 1) return null;
if (isCancelled()) {
params[0].clearImage();
params[0].clearContext();
return null;
}
// By default, AsyncTask sets the worker thread to have background thread priority, so bump
// it back up so that we save a little quicker.
Process.setThreadPriority(Process.THREAD_PRIORITY_FOREGROUND);
Context context = params[0].context;
Bitmap image = params[0].image;
Resources r = context.getResources();
try {
// Create screenshot directory if it doesn't exist
mScreenshotDir.mkdirs();
// media provider uses seconds for DATE_MODIFIED and DATE_ADDED, but milliseconds
// for DATE_TAKEN
long dateSeconds = mImageTime / 1000;
// Save
boolean compressRet = true;
OutputStream out = new FileOutputStream(mImageFilePath);
if(PhoneStatusBar.LEUI_ENABLE) {
compressRet = image.compress(Bitmap.CompressFormat.JPEG, 100, out);
} else {
compressRet = image.compress(Bitmap.CompressFormat.PNG, 100, out);
}
out.flush();
out.close();
if(!compressRet){
//When storage is full screenshot image will compress failed, so we delete the file
File f = new File(mImageFilePath);
if(f.exists()){
f.delete();
Log.d(TAG,"screenshot " + mImageFilePath + " compress failed, so we delete it");
}
params[0].clearImage();
params[0].result = 1;
}else {
// Save the screenshot to the MediaStore
ContentValues values = new ContentValues();
ContentResolver resolver = context.getContentResolver();
values.put(MediaStore.Images.ImageColumns.DATA, mImageFilePath);
values.put(MediaStore.Images.ImageColumns.TITLE, mImageFileName);
values.put(MediaStore.Images.ImageColumns.DISPLAY_NAME, mImageFileName);
values.put(MediaStore.Images.ImageColumns.DATE_TAKEN, mImageTime);
values.put(MediaStore.Images.ImageColumns.DATE_ADDED, dateSeconds);
values.put(MediaStore.Images.ImageColumns.DATE_MODIFIED, dateSeconds);
if(PhoneStatusBar.LEUI_ENABLE) {
values.put(MediaStore.Images.ImageColumns.MIME_TYPE, "image/jpeg");
} else {
values.put(MediaStore.Images.ImageColumns.MIME_TYPE, "image/png");
}
values.put(MediaStore.Images.ImageColumns.WIDTH, mImageWidth);
values.put(MediaStore.Images.ImageColumns.HEIGHT, mImageHeight);
values.put(MediaStore.Images.ImageColumns.SIZE, new File(mImageFilePath).length());
Uri uri = resolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
// Create a share intent
String subjectDate = DateFormat.getDateTimeInstance().format(new Date(mImageTime));
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
if(PhoneStatusBar.LEUI_ENABLE) {
sharingIntent.setType("image/jpeg");
} else {
sharingIntent.setType("image/png");
}
sharingIntent.putExtra(Intent.EXTRA_STREAM, uri);
sharingIntent.putExtra(Intent.EXTRA_SUBJECT, mImageFileName);
// Create a share action for the notification
final PendingIntent callback = PendingIntent.getBroadcast(context, 0,
new Intent(context, GlobalScreenshot.TargetChosenReceiver.class)
.putExtra(GlobalScreenshot.CANCEL_ID, mNotificationId),
PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent.FLAG_ONE_SHOT);
Intent chooserIntent = Intent.createChooser(sharingIntent, null,
callback.getIntentSender());
chooserIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK
| Intent.FLAG_ACTIVITY_NEW_TASK);
mNotificationBuilder.addAction(R.drawable.ic_screenshot_share,
r.getString(com.android.internal.R.string.share),
PendingIntent.getActivity(context, 0, chooserIntent,
PendingIntent.FLAG_CANCEL_CURRENT));
// Create a delete action for the notification
final PendingIntent deleteAction = PendingIntent.getBroadcast(context, 0,
new Intent(context, GlobalScreenshot.DeleteScreenshotReceiver.class)
.putExtra(GlobalScreenshot.CANCEL_ID, mNotificationId)
.putExtra(GlobalScreenshot.SCREENSHOT_URI_ID, uri.toString()),
PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent.FLAG_ONE_SHOT);
mNotificationBuilder.addAction(R.drawable.ic_screenshot_delete,
r.getString(com.android.internal.R.string.delete), deleteAction);
params[0].imageUri = uri;
params[0].image = null;
params[0].result = 0;
}
} catch (Exception e) {
// IOException/UnsupportedOperationException may be thrown if external storage is not
// mounted
Log.e(TAG, "error in SaveImageInBackgroundData, exception:" + e);
params[0].clearImage();
params[0].result = 1;
}
// Recycle the bitmap data
if (image != null) {
image.recycle();
}
return params[0];
}
The requirements: ensure that the PDF document is deleted from the device after the user has left the PDF viewing screen
The problem: on certain devices (Samsung 4.4.2 and Samsung 4.1.2 for sure, but not Asus 4.2.1) only the first time that the PDF is requested after restarting the application an error message is displayed stating "This document cannot be opened". Thereafter the PDF will load normally. I'm thinking this is a timing issue due to processes that need to be started the first time, but are running after the first attempted load.
The code: note that createFile() is called first, then startActivityForIntentResult()
private File file;
private ArrayList<Uri> uriList = new ArrayList<Uri>();
private void createFile() {
int fileNameLength = pdfFileName[0].length();
String fileName = pdfFileName[0].substring(0, fileNameLength - 4) + DateTime.now();
String fileExtension = pdfFileName[0].substring(fileNameLength - 4, fileNameLength);
byte[] content = Base64.decodeBase64(pdfData[0].getBytes());
BufferedOutputStream outputStream = null;
try {
File path = new File(getExternalFilesDir(null).getAbsolutePath(), "temp");
if (!path.exists()) {
path.mkdirs();
}
file = new File(path, fileName + fileExtension);
outputStream = new BufferedOutputStream(new FileOutputStream(file));
outputStream.write(content);
file.deleteOnExit();
uriList.add(Uri.fromFile(file));
}
catch (FileNotFoundException ex) {
ex.printStackTrace();
}
catch (IOException ex) {
ex.printStackTrace();
}
finally {
try {
if (outputStream != null) {
outputStream.flush();
outputStream.close();
}
}
catch (IOException ex) {
ex.printStackTrace();
}
}
}
private static int REQUEST_CODE = 1;
private Intent intent;
private void startActivityForIntentResult() {
if (file.exists()) {
Uri targetUri = uriList.get(0);
intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(targetUri, "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
try {
startActivityForResult(intent, REQUEST_CODE);
}
catch (ActivityNotFoundException e) {
toastTitle = "Error Displaying PDF";
toastMessage = "Please make sure you have an application for viewing PDFs installed on your device and try again.";
toast = new GenericCustomToast();
toast.show(toastTitle, toastMessage, QueryForPDF.this);
}
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (resultCode == RESULT_CANCELED && requestCode == REQUEST_CODE) {
if(!file.delete()) {
file.delete();
}
}
searchAgain();
}
#Override
public void onBackPressed() {
super.onBackPressed();
if(!file.delete()) {
file.delete();
}
searchAgain();
}
#Override
public void onStop() {
super.onStop();
if(!file.delete()) {
file.delete();
}
}
#Override
public void onDestroy() {
super.onDestroy();
if(!file.delete()) {
file.delete();
}
}
EDIT: I have also tried implementing a callback to be absolutely certain that createFile() has finished it's work. I even tried adding delays (of different time increments) as well as adding (the completely unnecessary) flags for Intent.FLAG_GRANT_READ_URI_PERMISSION, Intent.FLAG_GRANT_WRITE_URI_PERMISSION, and Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION.
I still don't know why this works, but here's the solution in case anyone else runs into this issue:
It's the directory where the file is created. For some reason on the two Samsung devices there was something different in how the files were either accessed or created versus the Asus device. So File path = new File(getExternalFilesDir(null).getAbsolutePath(), "temp"); becomes File path = new File(getExternalCacheDir().getAbsolutePath()); and the problem goes away.
I am trying to use my download code that works perfectly fine in my other app, but it is acting very strange in this new app I am trying to make.
public class DownloadFile extends AsyncTask<Void, Integer, String> {
String SDCardRoot;
String mixtapeurl = "http://xxxxxx.com/xxxxxxxxxxxx/Mixtapes/NCredible/J.%20Cole%20-%20Cole%20World.zip";
NotificationManager notificationManager;
Notification notification2;
ProgressBar progressBar;
#Override
protected void onPreExecute() {
// configure the intent
Intent intent = new Intent();
final PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, 0);
// configure the notification
notification2 = new Notification(R.drawable.download, "DOWNLOADING: " + mixtapeurl, System
.currentTimeMillis());
notification2.flags = notification2.flags | Notification.FLAG_ONGOING_EVENT;
notification2.contentView = new RemoteViews(getApplicationContext().getPackageName(), R.layout.download_progress);
notification2.contentIntent = pendingIntent;
notification2.contentView.setTextViewText(R.id.percentage, 0 + "%" );
notification2.contentView.setTextViewText(R.id.status_text, "DOWNLOADING: " + mixtapeurl);
notification2.contentView.setProgressBar(R.id.status_progress, 100, 0, false);
getApplicationContext();
notificationManager = (NotificationManager) getApplicationContext().getSystemService(
Context.NOTIFICATION_SERVICE);
notificationManager.notify(2, notification2);
}
#Override
protected String doInBackground(Void... params) {
try {
//set the download URL, a url that points to a file on the internet
//this is the file to be downloaded
URL url = new URL(mixtapeurl);
//create the new connection
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
//set up some things on the connection
urlConnection.setRequestMethod("GET");
urlConnection.setDoOutput(true);
//and connect!
urlConnection.connect();
//set the path where we want to save the file
//in this case, going to save it on the root directory of the
//sd card.
SDCardRoot = Environment.getExternalStorageDirectory() + "/download/";
//create a new file, specifying the path, and the filename
//which we want to save the file as.
File file = new File(SDCardRoot,mixtapeurl);
//this will be used to write the downloaded data into the file we created
FileOutputStream fileOutput = new FileOutputStream(file);
//this will be used in reading the data from the internet
InputStream inputStream = urlConnection.getInputStream();
//this is the total size of the file
int totalSize = urlConnection.getContentLength();
//variable to store total downloaded bytes
int downloadedSize = 0;
int myProgress;
//create a buffer...
byte[] buffer = new byte[1024];
int bufferLength = 0; //used to store a temporary size of the buffer
int previousProgress = 0;
//now, read through the input buffer and write the contents to the file
while ( (bufferLength = inputStream.read(buffer)) > 0 ) {
//add the data in the buffer to the file in the file output stream (the file on the sd card
fileOutput.write(buffer, 0, bufferLength);
//add up the size so we know how much is downloaded
downloadedSize += bufferLength;
//this is where you would do something to report the prgress, like this maybe
//updateProgress(downloadedSize, totalSize);
//publishProgress((int)(total*100/lenghtOfFile));
myProgress = (int) ((downloadedSize/(double)totalSize) * (double)100);
if ((myProgress) > 1 && (myProgress) > ((previousProgress ))) {
previousProgress= myProgress;
publishProgress(myProgress);
}
}
//close the output stream when done
fileOutput.close();
//catch some possible errors...
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return "success";}
#Override
protected void onProgressUpdate(Integer... progression) {
String percent;
percent = progression[0].toString();
notification2.contentView.setProgressBar(R.id.status_progress, 100, progression[0], false);
notification2.contentView.setTextViewText(R.id.percentage, percent + "%" );
notificationManager.notify(2, notification2);
}
#Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
Toast toast = Toast.makeText(getApplicationContext(), mixtapeurl + " downloaded to: " + SDCardRoot, Toast.LENGTH_LONG);
toast.show();
}
}
Here is how I implement it:
new DownloadFile().execute();
And what i get is "http://xxxxxx.com/xxxxxxxxxxxx/Mixtapes/NCredible/J.%20Cole%20-%20Cole%20World.zip download to null" for my toast right after I click the button and the file does not download at all.
This sounds like a very common issue: Forgot to set permission in manifest.
<uses-permission android:name="android.permission.INTERNET" />