0

I am coding for an app in Android and am facing the following problem.

I have to override an inbuilt function Fragment getItem(int position) to return the Bitmap of an image, but as the image is quite large it takes lots of time to load. So, the UI is a bit slow in responding. To resolve this i think it can be resolved like in the following code. It basically returns a blank image immediately and when the real image is loaded it returns the proper image.

@Override
    public Fragment getItem(final int position) {
        pid = fork();  //NOT POSSIBLE / ERROR
        if (!pid)
            return  (code for empty image);
        return new ScreenSlidePageFragment(files[position].getAbsolutePath(), width, height);
    }

But, I do not know of a function similar to fork() in Java. Can you please help me out with this issue, Thanks

rahulg
  • 2,183
  • 3
  • 33
  • 47

3 Answers3

1

In Java you would use the Thread class for that. However, if you are developing for Android, you might consider using AsyncTask and refer the official android developer training for

Processing Bitmaps Off the UI Thread.

Azhar
  • 20,500
  • 38
  • 146
  • 211
Akash
  • 631
  • 1
  • 8
  • 16
0

Instead of forking new processes, in Java you open Threads for cases like this. See this question for details on how to do that.

Community
  • 1
  • 1
nfechner
  • 17,295
  • 7
  • 45
  • 64
0

You can define an AsynTask and when implementing his functions do something like

public class imageLoader extends AsyncTask<Void, Void, Void>{

 protected preExecute(){
  //load the blank image
  //this function is executed on the UI thread
 }

 protected doInBackground(){
  //load the real one
  //that one is not executed on the UI thread
 }

 protected postExecute(){
  //set the new image 
  //this one is executed on the UI thread
 }
}
AlexBcn
  • 2,450
  • 2
  • 17
  • 28