简体   繁体   中英

BlackBerry create bitmap from path

After taking a picture in my app I want to get the image (by it's path).

I see lots of examples on how to get an image, the only problem is that it no longer is possible doing it by using Connector.open() since Connector is deprecated now.

What to use instead?

conn = (FileConnection)Connector.open("file:///SDCard/BlackBerry/pictures/" + picture);

try {
         InputStream input = null;
         input = fconn.openInputStream();

         int available = 0;
         available = input.available();
         int fSz = (int)fconn.fileSize();
         byte[] data = new byte[fSz];

         input.read(data, 0, fSz);
         image = EncodedImage.createEncodedImage(data,0,data.length);
         bkBitmap = image.getBitmap();                
} catch(ControlledAccessException e) { 
         pLog = "*** Problem Accessing image file:" + e;
         EventLogger.logEvent( GUID, pLog.getBytes() );                                                                                
}

Thanks!

Try this code:

public class LoadingScreen extends MainScreen
{           
public LoadingScreen()
{   
    setTitle("Loading Screen");
    createGUI();
}

private void createGUI() 
{           
    BitmapField bitmapField=new BitmapField(getTheImage());
    add(bitmapField);
}

private Bitmap getTheImage() 
{
    Bitmap bitmap=null,scaleBitmap=null;
    InputStream inputStream=null;
    FileConnection fileConnection=null;     
    try
    {
        fileConnection=(FileConnection) Connector.open("file:///SDCard/BlackBerry/pictures/"+"background.png");
        if(fileConnection.exists())
        {
            inputStream=fileConnection.openInputStream();           
            byte[] data=new byte[(int)fileConnection.fileSize()];           
            data=IOUtilities.streamToBytes(inputStream);
            inputStream.close();
            fileConnection.close();
            bitmap=Bitmap.createBitmapFromBytes(data,0,data.length,1);

            //You can return this bitmap otherwise, after this you can scale it according to your requirement; like...
            scaleBitmap=new Bitmap(150, 150);
            bitmap.scaleInto(scaleBitmap, Bitmap.FILTER_LANCZOS);
        }
        else
        {
            scaleBitmap=Bitmap.getBitmapResource("noimage.png");//Otherwise, Give a Dialog here;
        }
    }
    catch (Exception e) 
    {
        try 
        {
            if(inputStream!=null)
            {
                inputStream.close();                
            }
            if(fileConnection!=null)
            {
                fileConnection.close();
            }
        } 
        catch (Exception exp) 
        {

        }
        scaleBitmap=Bitmap.getBitmapResource("noimage.png");//Your known Image;     
    }
    return scaleBitmap;//return the scale Bitmap not the original bitmap;
  } 
}

I got like this below Image:

从SDCard获取图像

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM