Get The Image From SDCard In Blackberry

For this we have to first check that “is SDCard available or not?”

For checking the SDCARD availability we have to use the following code:

Source code of SDCARD Checking: SDCardTest.java

import java.util.Enumeration;
import javax.microedition.io.file.FileSystemRegistry;
public class SDCardTest
{
 public static boolean isSDCardAvailable()
 {
 boolean sdCardPresent = false;
 String root = null;
 Enumeration e = FileSystemRegistry.listRoots();

 while (e.hasMoreElements())
 {
 root = (String)e.nextElement();
 if(root.equalsIgnoreCase("sdcard/"))
 {
 sdCardPresent = true;
 }
 }
 return sdCardPresent;
 }
}

and the code for Get the Image from SDCARD : LoadingScreen.java

import java.io.IOException;
import java.io.InputStream;

import javax.microedition.io.Connector;
import javax.microedition.io.file.FileConnection;

import net.rim.device.api.io.IOUtilities;
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.component.BitmapField;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;
import net.rim.device.api.ui.picker.FilePicker;
import net.rim.device.api.ui.picker.FilePicker.Listener;

import com.hiddenbrains.image.common.SDCardTest;

public class LoadingScreen extends MainScreen implements FieldChangeListener
{
 private VerticalFieldManager ver;
 private ButtonField showImage;
 private BitmapField bitmapField;

 public LoadingScreen()
 {
        ver=new VerticalFieldManager(USE_ALL_WIDTH);
        showImage=new ButtonField("Show Image",Field.FIELD_HCENTER);
        showImage.setChangeListener(this);
        ver.add(showImage);

        bitmapField=new BitmapField(null,Field.FIELD_HCENTER);
        bitmapField.setPadding(10, 0, 10, 0);
       ver.add(bitmapField);

      add(ver);
 }
 public void fieldChanged(Field field, int context)
 {
 if(field==showImage)
 {
 selectImageFromSDCARD();
 }
 }

 private void selectImageFromSDCARD()
 {
 String PATH="";
 if(SDCardTest.isSDCardAvailable())
 PATH = System.getProperty("fileconn.dir.memorycard.photos");//The default stored Images Path;
 else
 PATH = System.getProperty("fileconn.dir.photos");//The default stored Images Path;

 FilePicker filePicker=FilePicker.getInstance();
 filePicker.setPath(PATH);
 filePicker.setListener(new Listener()
 {
 public void selectionDone(String url)
 {
 System.out.println("======================URL: "+url);
 try
 {
 FileConnection file = (FileConnection)Connector.open(url);
 if(file.exists())
 {
 InputStream inputStream = file.openInputStream();
 byte[] data=new byte[inputStream.available()];
 data=IOUtilities.streamToBytes(inputStream);
 Bitmap bitmap=Bitmap.createBitmapFromBytes(data, 0, data.length,1);//Here we get the Image;

 Bitmap scaleBitmap=new Bitmap(400, 300);//Now we are scaling that image;
 bitmap.scaleInto(scaleBitmap, Bitmap.FILTER_LANCZOS);
 bitmapField.setBitmap(scaleBitmap);
 }
 else
 {
 bitmapField.setBitmap(Bitmap.getBitmapResource("icon.png"));
 }
 }
 catch (IOException e)
 {
 bitmapField.setBitmap(Bitmap.getBitmapResource("icon.png"));
 }
 }
 });
 filePicker.show();
 }

 protected boolean onSavePrompt() //It doesn't show the "Save","Discard","Cancel" POPUP;
 {
 return true;
 }

 public boolean onMenu(int instance) //It doesn't show the Menu;
 {
 return true;
 }
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: