Question

I want to show a image from the SD card in a BitmapField. How to do that? Can anyone give me some sample code for that?

Was it helpful?

Solution

This may be Help full.

public Bitmap getImage(){
    Bitmap bitmapImage=null;
    try{
        InputStream input;
        FileConnection fconn = (FileConnection) Connector.open("file:///store/home/user/dirname/imgname.png", Connector.READ_WRITE);
        input = fconn.openInputStream();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        int j = 0;
        while((j=input.read()) != -1) {
             baos.write(j);
        }
        byte[] byteArray = baos.toByteArray();

        bitmapImage = Bitmap.createBitmapFromBytes(byteArray,0,byteArray.length,1);


    }catch(Exception ioe){
        System.out.println(ioe);
    }
    return bitmapImage;
}

Enjoy..

OTHER TIPS

Hi Guys above code is useful for BB OS >= 5.0 I'm using a code which can used for OS 4.2 or higher.

private Bitmap resizeBitmap(Bitmap image, int width, int height)
    {   

        int rgb[] = new int[image.getWidth()*image.getHeight()];
        image.getARGB(rgb, 0, image.getWidth(), 0, 0, image.getWidth(), image.getHeight());
        int rgb2[] = rescaleArray(rgb, image.getWidth(), image.getHeight(), width, height);
        Bitmap temp2 = new Bitmap(width, height);
        temp2.setARGB(rgb2, 0, width, 0, 0, width, height);
        return temp2;
    }

    private int[] rescaleArray(int[] ini, int x, int y, int x2, int y2)
    {
        int out[] = new int[x2*y2];
        for (int yy = 0; yy < y2; yy++)
        {
            int dy = yy * y / y2;
            for (int xx = 0; xx < x2; xx++)
            {
                int dx = xx * x / x2;
                out[(x2 * yy) + xx] = ini[(x * dy) + dx];
            }
        }
        return out;
    }

Try this sample code:

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())//sdcard available then               
         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;
}
}

If you have any doubts refer this Blog: Get Image From SDcard

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top