Reputation: 537
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.
Upvotes: 0
Views: 3899
Reputation: 31045
Since your comment says it's hanging on this line:
Intent BG = new Intent((Intent) DownloadBGTask.THREAD_POOL_EXECUTOR);
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:
Order of execution
When first introduced, AsyncTasks were executed serially on a single background thread. Starting with DONUT, this was changed to a pool of threads allowing multiple tasks to operate in parallel. Starting with HONEYCOMB, tasks are executed on a single thread to avoid common application errors caused by parallel execution.
If you truly want parallel execution, you can invoke executeOnExecutor(java.util.concurrent.Executor, Object[]) with THREAD_POOL_EXECUTOR.
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:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
DownloadBGTask downloader = new DownloadBGTask();
downloader.execute(new URL("http://www.google.com"),
new URL("http://stackoverflow.com"));
Where the strings are the URLs you want to retrieve (can be 1, or many). The parameters to execute()
get passed to your task's doInBackground()
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:
private File mDownloadRootDir;
private Context mParent; // usually this is set to the Activity using this code
private void callMeBeforeDownloading() {
// http://developer.android.com/guide/topics/data/data-storage.html#filesExternal
mDownloadRootDir = new File(Environment.getExternalStorageDirectory(),
"/Android/data/" + mParent.getPackageName() + "/files/");
if (!mDownloadRootDir.isDirectory()) {
// this must be the first time we've attempted to download files, so create the proper external directories
mDownloadRootDir.mkdirs();
}
}
and later
storagePath = mDownloadRootDir.getAbsolutePath();
Upvotes: 3
Reputation: 31466
use this utiity method to download an image from the web in your GetBackground class:
// Utiliy method to download image from the internet
static private Bitmap downloadBitmap(String url) throws IOException {
HttpUriRequest request = new HttpGet(url.toString());
HttpClient httpClient = new DefaultHttpClient();
HttpResponse response = httpClient.execute(request);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == 200) {
HttpEntity entity = response.getEntity();
byte[] bytes = EntityUtils.toByteArray(entity);
Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0,
bytes.length);
return bitmap;
} else {
throw new IOException("Download failed, HTTP response code "
+ statusCode + " - " + statusLine.getReasonPhrase());
}
}
andyour DownloadBGTask extends AsyncTask<URL, Integer, Bitmap>
URL url = new URL ("http://oranjelan.nl/oranjelan-bg.png");
DownloadBGTask task = new DownloadBGTask();
task.execute(url);
Upvotes: 1