I’m trying to get an image from a server and use that file as the background fro my app.
Ive already leared that I should use AsyncTask to do so but my app still crashes or freezes when I run it.
Heres the code I use:
To call the AsyncTask:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent BG = new Intent((Intent) DownloadBGTask.THREAD_POOL_EXECUTOR);
The AsyncTask:
import java.net.URL;
import com.pxr.tutorial.json.Getbackground;
import android.os.AsyncTask;
public class DownloadBGTask extends AsyncTask<URL, Integer, Long> {
protected Long doInBackground(URL... urls) {
int count = urls.length;
long totalSize = 0;
for (int i = 0; i < count; i++) {
totalSize += Getbackground.downloadFile(urls[i]);
publishProgress((int) ((i / (float) count) * 100));
// Escape early if cancel() is called
if (isCancelled()) break;
}
return totalSize;
}
protected void onProgressUpdate(Integer... progress) {
// Things to be done while execution of long running operation
}
protected void onPostExecute(Long result) {
// xecution of result of Long time consuming operation
}
}
And the Getbackground.java:
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import android.os.Environment;
public class Getbackground {
URL url;
public static long downloadFile(URL url2) {
try {
URL url = new URL ("http://oranjelan.nl/oranjelan-bg.png");
InputStream input = url.openStream();{
try {
File fileOnSD=Environment.getExternalStorageDirectory();
String storagePath = fileOnSD.getAbsolutePath();
OutputStream output = new FileOutputStream (storagePath + "/oranjelangbg.png");
try {
byte[] buffer = new byte[1000000];
int bytesRead = 0;
while ((bytesRead = input.read(buffer, 0, buffer.length)) >= 0) {
output.write(buffer, 0, bytesRead);
}
} finally {
output.close();
}
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
try {
input.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
} catch (MalformedURLException ex) {
throw new RuntimeException(ex);
} catch (IOException e) {
throw new RuntimeException(e);
}
return 0;
}
}
PS, Sorry for the crappy code, I’m really new to this so dont be supprised if there are some really stupid misakes in there.
Since your comment says it’s hanging on this line:
let’s start with that.
Since it sounds like you might be a newbie (no shame in that!), I’m guessing that you don’t necessarily need this task to execute in parallel.
If you read the Android docs on AsyncTask, they kind of try to steer you away from that:
So, if you just want to pass a url, or a list of urls to the task to execute in order (series), then just start your task like this:
Where the strings are the URLs you want to retrieve (can be 1, or many). The parameters to
execute()get passed to your task’sdoInBackground()method.Edit: Now that you seem to have gotten past starting the task, here’s a couple other suggestions:
1000000 bytes seems like a large buffer to me. I’m not saying it won’t work, but I usually use something like
byte[1024].Concerning the directory where you store your downloads. This is the code I like to use:
and later