BlackBerry从路径创建位图

时间:2012-01-19 15:05:01

标签: image blackberry

在我的应用程序中拍照后,我想要获取图像(按照它的路径)。

我看到很多关于如何获取图像的示例,唯一的问题是它不再可能通过使用Connector.open()来实现,因为现在不推荐使用Connector。

反而使用什么?

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() );                                                                                
}

谢谢!

1 个答案:

答案 0 :(得分:4)

试试这段代码:

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;
  } 
}

我在下面有这样的图片:

Get Image from SDCard

相关问题