Loading and Displaying Images in Android

Show in webframe
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: [XML]
    
                <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                   xmlns:tools="http://schemas.android.com/tools"
                   android:layout_width="match_parent"
                   android:layout_height="match_parent"
                   android:orientation="vertical"
                   android:weightSum="1.0" 
                   android:background="@android:color/black">
                   
                   <Button
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:text="Select Image From Gallery"
                      android:onClick="onSelectImage"/>
                      
                   <leadtools.controls.RasterImageViewer
                      android:id="@+id/imageviewer"
                      android:layout_width="match_parent"
                      android:layout_height="0dp"
                      android:layout_weight=".96"
                      android:background="@android:color/white"/>
                </LinearLayout>
                
    
  9. In the Project Explorer window, edit the src/leadtools.androidloaddisplay/MainActivity.java file, and add the following lines before the Main activity class: [Java]
    
                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.RasterImageViewer;
                
    
  10. Add the following member variables to the MainActivity class: [Java]
    
                private static final int IMAGE_GALLERY = 0x0001;
                private RasterImageViewer mViewer;
                
    
  11. Update the onCreate() function as shown below: [Java]
    
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
                
                // RasterSupport
                RasterSupport.initialize(this);
                
                // Init viewer
                mViewer = (RasterImageViewer) findViewById(R.id.imageviewer);
                mViewer.setTouchInteractiveMode(new ImageViewerPanZoomInteractiveMode());
                
    
  12. Add the following functions: [Java]
    
                public void onSelectImage(View v)
                {
                   Intent gallery = new Intent(Intent.ACTION_PICK, Media.EXTERNAL_CONTENT_URI);
                   startActivityForResult(gallery, IMAGE_GALLERY);
                }
                
                @Override
                protected void onActivityResult(int requestCode, int resultCode, Intent data)
                {
                   if (resultCode == RESULT_OK)
                   {
                      if(requestCode == IMAGE_GALLERY)
                      {
                         Uri imageUri = data.getData();
                         try
                         {
                            // 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
                            mViewer.setImage(image);
                         }
                         catch(Exception ex)
                         {
                            Toast.makeText(this, ex.getMessage(), Toast.LENGTH_LONG).show();
                         }
                      }
                   }
                }
                
    
  13. Build and run the program.

 

 


Products | Support | Contact Us | Copyright Notices
© 2006-2014 All Rights Reserved. LEAD Technologies, Inc.