views:

1636

answers:

3

I want to access an image stored in Blackberry, say at location "store/home/user/image.png" .

Now can i access this image as,

String filePath = "file:///store/home/user/image.png;
Bitmap image = Bitmap.getBitmapResource(filePath);
BitmapField bitmapField = new BitmapField(image, BitmapField.FOCUSABLE);

OR

I have to access it as,

  String filePath = "file:///store/home/user/image.png;
  FileConnection fconn = (FileConnection)Connector.open(filePath, Connector.READ); 
  if (fconn.exists()) 
  {
                ........
                ........                           

     input.close();
     fconn.close();                            

  }

I am able to access the image using the second way but I want to know that can I access it using "Bitmap.getBitmapResource(filePath)" ?

A: 

What language are you writing in? Here's how I did it in C++ on Windows Mobile:

Log::GetSingleton() << "Loading sprite: " << wchar_path << "\n";

// Special magic WM bitmap loading function that isn't in the examples
// because Microsoft wants you to use resource files
HBITMAP bitmap = SHLoadDIBitmap(wchar_path);

if (!bitmap) 
{
 Error::LastError();
 Error::Explain("Failed to load bitmap.");
 return NULL;
}

HDC dc_image = CreateCompatibleDC(NULL);
if (!dc_image) 
{
 Error::LastError();
 Error::Explain("Failed to create memory device context.");
 return NULL;
}
HBITMAP other_bitmap = (HBITMAP)SelectObject(dc_image, bitmap);

wchar_path would be something like \\Storage Card\\test.bmp.

knight666
I believe its j2me with RIM Blackberry API's
Max Gontar
You should really add that to the tags next time, it will help you get better answers. ;)
knight666
yes. its j2me with RIM Blackberry API's. So how to solve this issue with "Bitmap.getBitmapResource(filePath)" ?
Shreyas
+1  A: 

Bitmap.getBitmapResource() is used for loading resources that are stored within your COD file or any COD file your application relies on. It is not for loading files that are stored on the device.

Bitmap JavaDocs

Fostah
+2  A: 

Take a look at Bitmap.getBitmapResource API reference:

public static Bitmap getBitmapResource(String name)
Creates a bitmap from provided name resource.
This method looks for the resource in the cod file that launched this process.
Parameters:
name - Name of the bitmap resource.
Returns:
New Bitmap object, or null if this method couldn't find your named resource.
Throws:
NullPointerException - If the name parameter is null.
Since:
JDE 3.6

public static Bitmap getBitmapResource(String module, String name)
Creates a bitmap from provided named resource found in module.
Parameters:
module - Name of the module containing the bitmap resource. If not specified, the name of >the calling module is used.
name - Name of the bitmap resource.
Returns:
New Bitmap object, or null if this method couldn't find your named resource.
Throws:
NullPointerException - If the name parameter is null.
Since:
JDE 3.6

This method is used to retrieve resources code modules. If you include some image into your project you will be able to retrieve it with this method.

And if you want to open some image from file system, you will have to use FileConnection, check file MIME type, read it's bytes from stream and create EncodedImage accordingly.

Max Gontar
Shreyas
You're welcome!
Max Gontar
I know I'm waking the dead with this thread, but you say "check file MIME type", forgive me for not knowing, but how is that done?
Nicholas
see http://www.blackberry.com/developers/docs/4.2api/net/rim/device/api/system/EncodedImage.html#getMIMEType() btw you can also check getImageType()
Max Gontar
and if it's simply not image, you will have to check filename extension
Max Gontar
see also http://www.blackberry.com/developers/docs/4.2api/net/rim/device/api/io/MIMETypeAssociations.html
Max Gontar