I am loading multiple photos from Dropbox using the Core API in Android and when making requests they are serialized.
I am getting the thumbnails to show in a grid view and when the user clicks on one of them it goes to another activity it fetches the full res version from the server.
It does this fine, but the high res version only starts to be downloaded when all the others async tasks fetching the thumbnails are finished.
So what I want to know is, is this a limitation of the Dropbox Core API? Or is there a way to make the high res async task have priority over others so it is immediately downloaded and then the others can resume.
This is due to how async tasks actually work. They don't run in parallel on 3.0+ (they used to until 3.0), because too many newbie developers weren't able to program in parallel without errors, so Google decided to change it. Instead, async tasks run on a single thread in FIFO order.
You can override this however. Instead of calling asynctask.execute(), call asynctask.executeOnExecutor() and use a THREAD_POOL_EXECUTOR. This will execute it in parallel on its own thread. I believe there's a thread cap, but it will at least make several run in parallel.
If the thread cap becomes an issue, you can always drop down to using threads instead of using async tasks. You need to do some work yourself to do an onPostExecute, but it isn't that hard. And if you're creeating your own threads you can make up to the OS limit.
Related
right now i am uploading image in single thread using asynctask. but it is not efficient and it's takes lot of time to upload. so i am refering this link to download image using ThreadPoolExecuter. can i use this strategy while uploading image also ?
is this good practice to upload image?
is this good practice to upload image?
YES that could be an option if you don't worry about tracking of each execution (i.e when thread completed its task ).
See what google doc says
they usually provide improved performance when executing large numbers
of asynchronous tasks, due to reduced per-task invocation overhead,
and they provide a means of bounding and managing the resources,
including threads, consumed when executing a collection of tasks.
You can down- as well as up-load an image in a separate thread, that's perfectly fine.
If you want to use several threads for that, it would make no sense due to limited bandwidth (several threads would not speed-up the process) and partitioning issues (on the server).
i am developing an android app, and i want user to download the file(images) after installation and the store them on sd card ,then aply them in my app ,there will be a default app before user download the images and the these images will be applied. I know little bit about DownloadManager class but don't know how to store them and retrieving them on run time.
any tutorial or any sample source code.. ??
I personally have never used the download manager, so I cannot say anything on that. Your best bet would probably be to check for the files' existances using File.exists() and downloading / applying these images in an AsyncTask<Param, Progress, Result> or thread. AsyncTask is typically the way people go.
Whichever route you take, be wary of what is happening in the background and what is happening on the main thread. UI updates must happen on the main thread. All network code must happen in the background. Any deviation from that will crash.
I'm trying to develop an android app that takes some data from a txt file (which is about 7 million lines long close to 32 MB) processes it according to some conditions places them in an SQL database and retrieves them. As far as the database is concerned it works well since I have tested it in alternative way, but for some reason it has a hard time dealing with the file, and my code becomes very unresponsive. What is an efficient way of doing this?
If time-consuming part is the file input may be using an async task to to do the work (read the file and prepare the DB) in the background and then notify the user when it's done. This way the user can keep using the app. Here are some links:
AsyncTask
AsyncTask example
According to the documentation the asyncTask should only be used for not so long operations (a few seconds only):
AsyncTasks should ideally be used for short operations (a few seconds
at the most.) If you need to keep threads running for long periods of
time, it is highly recommended you use the various APIs provided by
the java.util.concurrent pacakge such as Executor, ThreadPoolExecutor
and FutureTask.
run the code in separate thread (async task)
instead of loading whole file at once, process it line by line. this would help you to reduce memory consumption
I am trying to process multiple file writing commands (to seperate files) without hanging the UI.
To put my question in context, imagine the following
My main application looks like a file manager. It currently sees 10 files each of about 5MB in size. (Don't worry about how this list works etc.)
When I select one file item, I want it to immediately start copying/duplicating the file onto another location on the SD card. Typically this should take a few seconds
I want to be able to select a second, or a third etc. file immediately after the first. At the end of everything, all the files which I selected will be duplicated. So I could, for example click 5 files within 5 seconds, but all the copying actions takes a minute.
At this moment two options have come to my mind:
The first is to simply put the file writing commands of each file in a seperate thread. Pseudocode will look like this
Onclick
new Thread()
write file
If this works, there can be scenarios where I have 10 threads running simultaneously, writing to 10 seperate files. I would like to know if anyone has done this before, and what I should be looking out for
The second option, of course, is if there is already a certain data structure/known methods that can address this problem. Some sort of a pending queue system that gets larger as I add requests, but gets smaller as the system writes the data away.
I'm not absolutely sure how the SD-card works, but I can tell you that trying to write in parallel to a single hard disk is a bad idea because it will actually slow down the performance compared to a sequential write.
You may want to try using an ExecutorService and measuring the performance with different thread counts but I'm afraid you will end up having to implement a queue with a single thread taking the queued files and writing them one by one.
I would create an AsyncTask class that simply copies a given file over. Each time a file is selected, create a new instance of that class for the selected file. The thread management built into Android for AsyncTask is well balanced and should handle this use case nicely. It will be easy to provide feedback for progress and completion using the built-in AsyncTask methods.
I think the classes in java.util.concurrent are what you need; specifically the Executors class to create a ThreadPoolEecutor. It has the benefit of accepting as many tasks as the user clicks but limiting the number of threads to some limit you specify. (Spawning threads without limit can be a problem, slowing down not only each other but the UI as well.)
After some googling, I selected various sources and started to use a separate thread to download images to make the UI responsive. It actually worked like a charm. But after a few minutes it would freeze the emulator. Initially I had assumed various reasons but finally I figured out that if this threading code is removed it works without freezing the emulator.
The code was adapted from another Stackoverflow question from the answer given by a certain Fedor. For the sake of simplicity I had removed the HashMap part and directly download the image each time a request comes from the list adapter. Also, I assumed that since the image is very small (< 1 KiB) it can actually be downloaded again rather than storing it in memory.
I am not sure if this is the right way to handle asynchronous image download, but any help in preventing the emulator freeze would be much appreciated. I can copy paste the code if needed.
Have a look at this url
about downloading images from remote server using asynchronous task and threadpool.