我如何从Blackberry的画廊中获取图像?

时间:2012-01-24 06:34:22

标签: blackberry

我正在Blackberry中开发具有照片功能的应用程序。我想从图库中获取照片并在Bitmap字段中显示。有没有想法的人请发给我 提前谢谢..

2 个答案:

答案 0 :(得分:3)

  

FilePicker 是主要概念;我正在拍摄SDCard图像。所以,在测试之前在模拟器中设置sdcard;

试试这个示例代码:

import net.rim.device.api.ui.picker.FilePicker;
import net.rim.device.api.ui.picker.FilePicker.Listener;

public class FilePickerScreen extends MainScreen implements FieldChangeListener
{       
Bitmap bitmap;
ButtonField click;
BitmapField bitmapField;
public FilePickerScreen()
{   
    setTitle("FilePicker Screen");
    createGUI();
}

private void createGUI() 
{   
    add(new LabelField("Click to select the image", Field.FIELD_HCENTER));
    click=new ButtonField("Click");
    click.setChangeListener(this);
    add(click);         
    bitmapField=new BitmapField();
    add(bitmapField);
}   

private Bitmap getTheImage(String url) 
{
    Bitmap bitmap=null,scaleBitmap=null;
    InputStream inputStream=null;
    FileConnection fileConnection=null;     
    try
    {
        fileConnection=(FileConnection) Connector.open(url);
        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);           
    }
    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;
}

public void fieldChanged(Field field, int context)
{
    if(field==click)
    {
        try 
        {
            FilePicker filePicker;
            filePicker=FilePicker.getInstance();
            filePicker.setPath("file:///SDCard/BlackBerry/pictures/");
            filePicker.setListener(new Listener() 
            {       
                public void selectionDone(String path)
                {
                    bitmapField.setBitmap(getTheImage(path));
                }
            });
            filePicker.show();//it show what ever you select.
        } 
        catch (Exception e) 
        {
            StartUp.exceptionHandling(e.getMessage());
        }
    }       
}   
}

我认为这可能是你的要求;

答案 1 :(得分:2)

这取决于您的目标操作系统版本。我建议使用FilePicker API for 6.0+(实际上它可以从5.0操作系统获得,但是在一些次要操作系统版本中存在问题)。对于较低操作系统版本,我建议您使用此article来实现自己的实现。