image - BlackBerry 从路径创建位图

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

谢谢!

最佳答案

试试这个代码:

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

关于image - BlackBerry 从路径创建位图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8928297/

相关文章:

android - Android、iPhone、BlackBerry常用加解密算法

java - 如何检测黑莓应用程序何时处于 Activity 状态?

html - 如何将图像居中,使其充当链接并具有鼠标悬停命令?

java - 如何使用 mvc 在数据库中存储图像或任何类型的文档

android - 我可以使用 phoneGap 部署到多平台吗?

java - 字符串格式化 + 黑莓 + java

python - 将 PIL 图像转换为 skimage?

html - 使用 CSS 将图片替换为另一张图片

python - 删除图像中小于 n 大小(噪声)的像素 - 打开 CV python

user-interface - 如何在 Blackberry Storm 中设置抗锯齿?