Loading and Displaying Images in Android

Take the following steps to start a project and add some code that demonstrates loading and displaying an image in an Android application.

  1. Start Eclipse IDE.

  2. From the menu, choose File -> New -> Android Application Project....

  3. In the New Android Application dialog box, specify the project name by typing "Android LoadDisplay" in the Application Name field, and type "leadtools.androidloaddisplay" in the Package Name field. Then keep clicking on the Next button until you have finished creating the project.

  4. In the Project Explorer window, right-click on the AndroidLoadDisplay project and select Properties... from the context menu. In the Project Properties dialog box, select the Java Build Path node, then select the Libraries tab, and click Add External JARs. Select the following .jar files:

    • leadtools.jar
    • leadtools.codecs.jar
    • leadtools.controls.jar

    In the Order and Export tab, make sure all the check boxes for the jar files that have been added are selected.

  5. In the Project Explorer window, right-click on the AndroidLoadDisplay project and select New -> Folder... from the context menu. Then type "libs" in the Folder name and click OK.

  6. Create subfolders under libs. The ones created depend on the target CPU, as shown in the following table:

    CPU Folder Name
    Arm armeabi
    Arm-v7a armeabi-v7a
    Mips mips
    X86 x86

    Note: adding multiple CPU support in one project is allowed.

  7. Copy the following libs to the target CPU folders:

    • libleadtools.so
    • libleadtools.codecs.so
    • libleadtools.codecs.bmp.so
    • libleadtools.codecs.cmp.so
    • libleadtools.codecs.fax.so
    • libleadtools.codecs.gif.so
    • libleadtools.codecs.j2k.so
    • libleadtools.codecs.jb2.so
    • libleadtools.codecs.jbg.so
    • libleadtools.codecs.jls.so
    • libleadtools.codecs.jxr.so
    • libleadtools.codecs.png.so
    • libleadtools.codecs.pcx.so
    • libleadtools.codecs.psd.so
    • libleadtools.codecs.tif.so
    • libleadtools.colorconversion.so
    • libleadtools.imageprocessing.color.so
    • libleadtools.imageprocessing.core.so
    • libleadtools.imageprocessing.effects.so
    • libleadtools.imageprocessing.utilities.so
  8. In the Project Explorer window, select the res/layout/*activity_main.xml* file, and replace its content with the following:


    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
             android:text="Select Image From Gallery" 

  9. In the Project Explorer window, edit the src/leadtools.androidloaddisplay/*MainActivity.java* file, and add the following lines before the Main activity class:


    import android.view.View; 
          import android.widget.Toast; 
          import leadtools.ILeadStream; 
          import leadtools.LeadStreamFactory; 
          import leadtools.RasterImage; 
          import leadtools.RasterSupport; 
          import leadtools.codecs.RasterCodecs; 
          import leadtools.controls.ImageViewerPanZoomInteractiveMode; 
          import leadtools.controls.ImageViewer; 

  10. Add the following member variables to the MainActivity class:


    private static final int IMAGE_GALLERY = 0x0001; 
          private ImageViewer mViewer; 

  11. Update the onCreate() function as shown below:


          // RasterSupport 
          // Init viewer 
          mViewer = (ImageViewer) findViewById(R.id.imageviewer); 
          mViewer.setTouchInteractiveMode(new ImageViewerPanZoomInteractiveMode()); 

  12. Add the following functions:


    public void onSelectImage(View v) 
             Intent gallery = new Intent(Intent.ACTION_PICK, Media.EXTERNAL_CONTENT_URI); 
             startActivityForResult(gallery, IMAGE_GALLERY); 
          protected void onActivityResult(int requestCode, int resultCode, Intent data) 
             if (resultCode == RESULT_OK) 
                if(requestCode == IMAGE_GALLERY) 
                   Uri imageUri = data.getData(); 
                      // Create Lead Stream 
                      ILeadStream stream = LeadStreamFactory.create(getContentResolver().openInputStream(imageUri), true); 
                      // Create RasterCodecs 
                      RasterCodecs codecs = new RasterCodecs(String.format("%s/lib/", getApplicationInfo().dataDir)); 
                      // Load image 
                      RasterImage image = codecs.load(stream); 
                      // Set the loaded image in the viewer 
                   catch(Exception ex) 
                      Toast.makeText(this, ex.getMessage(), Toast.LENGTH_LONG).show(); 

  13. Build and run the program.

Help Version 19.0.2017.10.27
Products | Support | Contact Us | Copyright Notices
© 1991-2017 LEAD Technologies, Inc. All Rights Reserved.
LEADTOOLS Imaging, Medical, and Document