Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7024487
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:50:46+00:00 2026-05-27T23:50:46+00:00

Hi i made one small application to show all sdcard images should display in

  • 0

Hi i made one small application to show all sdcard images should display in my application as Thumbnail view .my program is like following

Here problem is that in simulator it loads fast and accurate where as phone it take lots of time. it is loaded some of 30MB around not getting problem. whare it is 80MB around it is not showing images on my screen.

Note: This is perfectly working in simulator but Not device
and my images path is “file:///SDCard/images/”
My code is:

this is my application starter class

public class StartUp extends UiApplication{

    public static void main(String[] args) {
        StartUp start=new StartUp();
        start.enterEventDispatcher();
    }
    public StartUp() {
        pushScreen(new ViewerMainScreen());
    }
}

this is my ViewerMainScreen.java

public class ViewerMainScreen extends MainScreen implements FieldChangeListener{

    private ObjectListField fileList;
    private String currentPath = "file:///";
    public VerticalFieldManager vert_manager=null;
    private BitmapField before[]=null,next[]=null;
    private int size=0;Border b1,b2;
    TableLayoutManager colFMgr=null;
    private Vector img_data=null;
    private int count=0;
    public ViewerMainScreen() {
        super();
        setTitle("Browse to image...");
        initGUI();
    }

    public void initGUI() {
        try {
//          add(getFileList());
            img_data=getAllList("file:///SDCard/images");
             b1=BorderFactory.createSimpleBorder(new XYEdges(3,3,3,3),new XYEdges(Color.RED, Color.RED, Color.RED, Color.RED),Border.STYLE_SOLID);
            b2=BorderFactory.createSimpleBorder(new XYEdges(0,0,0,0));
            size=img_data.size();
            before=new BitmapField[size];
            next=new BitmapField[size];
            vert_manager=new VerticalFieldManager(VERTICAL_SCROLL|VERTICAL_SCROLLBAR){
                protected void sublayout(int maxWidth, int maxHeight) {
                    super.sublayout(Display.getWidth(),Display.getHeight());
                    setExtent(Display.getWidth(),Display.getHeight());
                }
            };
            colFMgr = new TableLayoutManager(new int[] {

                 TableLayoutManager.USE_PREFERRED_SIZE,
                 TableLayoutManager.USE_PREFERRED_SIZE,
                 TableLayoutManager.USE_PREFERRED_SIZE,
                 TableLayoutManager.USE_PREFERRED_SIZE,

             }, Manager.HORIZONTAL_SCROLL|VERTICAL_SCROLL|VERTICAL_SCROLLBAR);
             for(int i= 0;i < size;i++)
             {
                EncodedImage load_img=EncodedImage.getEncodedImageResource("loading.jpg");
                    before[i] = new BitmapField(load_img.getBitmap(),Field.FOCUSABLE){
                        protected boolean navigationClick(int status,int time) {
                            fieldChangeNotify(0);
                            return super.navigationClick(status, time);
                        }
                         protected void onFocus(int direction) 
                            {
                                this.setBorder(b1);
                                 invalidate();
                            }
                            protected void onUnfocus() 
                            {
                                this.setBorder(b2);
                                invalidate();
                            }

                    };


                    colFMgr.add(before[i]);
             }
             add(colFMgr); 

            Thread t=new Thread(){
              public void run() {
              try {
                Thread.sleep(2000);

              } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                }
                callImageThread();
              }  
            };
        t.start();
        } catch (Exception e) {
            Dialog.alert(e.getMessage());
        }
    }

    private void callImageThread() {
        for(int i=0;i<size;i++)
        {
            if(count==6){
                try {
                    Thread.sleep(400);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            ImageThread th=new ImageThread(this, "file:///SDCard/images/"+img_data.elementAt(i), i);
            th.start();
            count++;

        }

    }

    public void ReplaceImage(EncodedImage img,int index)
    {

        before[index].setBitmap(img.getBitmap());
        before[index].setChangeListener(this);
        count--;
        System.out.println("Thread count: ========================"+Thread.activeCount()+"============================================="+count);
    }
    public void fieldChanged(Field field, int context) {
        for(int i=0;i<size;i++)
        {
            if(field==before[i])
            {
                synchronized (UiApplication.getEventLock()) {
                    EncodedImage img=null;
                    img=loadFile("file:///SDCard/images/"+img_data.elementAt(i));
                    int displayWidth = Fixed32.toFP(Display.getWidth()-100);
                    int imageWidth = Fixed32.toFP(img.getWidth());
                    int scalingFactorX = Fixed32.div(imageWidth, displayWidth);
                    int displayHeight = Fixed32.toFP(Display.getHeight()-100);
                    int imageHeight = Fixed32.toFP(img.getHeight());
                    int scalingFactorY = Fixed32.div(imageHeight, displayHeight);
                    EncodedImage scaledImage = img.scaleImage32(scalingFactorX, scalingFactorY);
                    UiApplication.getUiApplication().pushScreen(new ZoomScreen(scaledImage));
                }

            }

        }

    }
    public void displayMessage(final String message)
    {
        UiApplication.getUiApplication().invokeLater(new Runnable() {
            public void run() {
                Dialog.inform(message);
            }
        });
    }
    private Vector getAllList(String path){
        Vector data=new Vector();
        FileConnection fileConnection=null;
        try{
            fileConnection = (FileConnection) Connector.open(path);
            if (fileConnection.isDirectory()) {
                Enumeration directoryEnumerator = fileConnection.list();
                while (directoryEnumerator.hasMoreElements()) {
                    data.addElement(directoryEnumerator.nextElement());
                }

            }
        }catch (Exception e) {
            System.out.println(e.getMessage());
        }
        finally
        {
            if(fileConnection!=null){
                try {
                    fileConnection.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
        return data;
    }



    private EncodedImage loadFile(String path) {

        FileConnection fileConnection =null;
        InputStream inputStream =null;
        EncodedImage encodedImage=null;
        try {
            fileConnection = (FileConnection) Connector.open(path);
            if(fileConnection.exists()){
                inputStream = fileConnection.openInputStream();
                byte[] imageBytes = new byte[(int)fileConnection.fileSize()];
                inputStream.read(imageBytes);
                encodedImage = EncodedImage.createEncodedImage(imageBytes, 0, imageBytes.length);
                if(fileConnection!=null){
                    try {
                        fileConnection.close();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
                if(inputStream!=null){
                    try {
                        inputStream.close();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }

            }
        } catch (IOException e) {
            Dialog.alert("Insert an SD Card.");
        }
        return encodedImage;
    }
}

This is my ImageThread.java

public class ImageThread extends Thread{
    private ViewerMainScreen screen;
    private String path;
    private int index;
    private EncodedImage scaledImage=null;
    public ImageThread(ViewerMainScreen screen,String path,int index) {
        this.screen=screen;
        this.path=path;
        this.index=index;
    }
    public void  callMainScreen(EncodedImage eimg,int Rindex) {
        screen.ReplaceImage(eimg, Rindex);
    }
    public void run() {
        FileConnection fileConnection =null;
        InputStream inputStream =null;
        EncodedImage encodedImage=null;
        try {
            fileConnection = (FileConnection) Connector.open(path);
            if(fileConnection.exists()){
                inputStream = fileConnection.openInputStream();
                byte[] imageBytes = new byte[(int)fileConnection.fileSize()];
                inputStream.read(imageBytes);
                encodedImage = EncodedImage.createEncodedImage(imageBytes, 0, imageBytes.length);

                int displayWidth = Fixed32.toFP(100);
                int imageWidth = Fixed32.toFP(encodedImage.getWidth());
                int scalingFactorX = Fixed32.div(imageWidth, displayWidth);
                int displayHeight = Fixed32.toFP(100);
                int imageHeight = Fixed32.toFP(encodedImage.getHeight());
                int scalingFactorY = Fixed32.div(imageHeight, displayHeight);
                scaledImage = encodedImage.scaleImage32(scalingFactorX, scalingFactorY);
                callMainScreen(scaledImage, index);
            }
        } catch (IOException e) {
            Dialog.alert("Insert an SD Card.");
        }
        finally
        {
            if(fileConnection!=null){
                try {
                    fileConnection.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if(inputStream!=null){
                try {
                    inputStream.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }

}

please help me how to increase performance of my code

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T23:50:46+00:00Added an answer on May 27, 2026 at 11:50 pm
    1. I would load images only when user scrolls screen. For example load first two screens on screen start (in different thread) and load next when user is moving down. So set ScrollListener to your manager and use field postion and height to determine which is on the screen and which is going to be on screen soon.

    2. Also use limited size thread pool and queue for images loading. Unfortunately java concurrent is not par of j2me. But there are several similar pattern implementations, like ThreadPool.

    3. And It would be super if you also remember case when user scrolls fast – cancel scheduled loading if it’s not required. For every loading task remember BitmapField position and size. Before task starts file loading check if BitmapField is visible (going to be visible) with current scroll position.

    Also be aware about threads count. I see that you try to sleep main loading thread when count is 6. But there is still small possibility that 400 ms is not enough to finish work for current threads and you can easily start to create more threads.

    Also minor things – use Bitmap.scaleInto (If you target is 5.0+), use synchronization around size field, next is never used, use a little better names like nextImages, threadsCount, etc.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've made a small 3D viewer application using DirectX 9.0. Now I would like
Post-release, I have made one small change to one form in our development site
I made a small application that download pictures from a website. But when I'm
Ok, i made a small application with a small ball bouncing on the screen.
I've made a small function that I'll be reusing several times in an application.
I have made a small function which should print some InfoPath files. It is
I am working on a small windows application. The application needs to show some
I have made a small application on a school. Where there is a dropdown
a couple of weeks ago I made a small application which comes with the
I have made a demo small program that I want to deliver to my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.