Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Regular Contributor
workingHard
Posts: 54
Registered: ‎04-24-2010
My Device: 8900
My Carrier: simulator
Accepted Solution

Unable to read SDCard data

Hi,

I am trying to read a bmp files from SDCard

 

String mybmp = file:///fs-sdcard-root/BlackBerry/BMPImages/myimage.bmp
img = Bitmap.getBitmapResource(mybmp);

 

i get img as NULL.

 

The similator setting includes:

/fs-sdcard=true /fs-sdcard-root="C:\SimSDCard"

 

The location of file in my system is:  C:\SimSDCard\BlackBerry\BMPImages\myimage.bmp

 

Please tell me what i am missing.

 

Thanks!

Please use plain text.
Developer
Ted_Hopp
Posts: 1,305
Registered: ‎01-21-2009
My Device: Not Specified

Re: Unable to read SDCard data

The name of the file SDCard root isn't "fs-sdcard-root", it's "SDCard". I don't think that setting the simulator to use the PC file system for the SDCard changes that. So try:

 

String mybmp = "file:///SDCard/BlackBerry/BMPImages/myimage.bmp";

 

and things should work better. Also take a look at FileSystemRegistry.listRoots().




Solved? click "Accept as solution". Helpful? give kudos by clicking on the star.
Please use plain text.
Regular Contributor
workingHard
Posts: 54
Registered: ‎04-24-2010
My Device: 8900
My Carrier: simulator

Re: Unable to read SDCard data

I tried this. It still gives error :smileysad:

 

FRIDG: could not find file:///SDCard/BlackBerry/BMPImages/mybmp.bmp

 

Do i have specify the size of SD Card? I think its default 32MB. My data is 15MB.

How can i browse to SDCard location in Blackberry Simulator? I need to ensure that file exists in SDCard.

 

Please help!

 

 

 

 

 

Please use plain text.
Regular Contributor
workingHard
Posts: 54
Registered: ‎04-24-2010
My Device: 8900
My Carrier: simulator

Re: Unable to read SDCard data

Ok. in my simulator, i can see my files in Media->pictures-> All Pictures and Picture Folder.

So, where can it go wrong?

Please use plain text.
Developer
JCarty
Posts: 1,055
Registered: ‎01-25-2009
My Device: BlackBerry Bold 9650/BlackBerry Torch 9800

Re: Unable to read SDCard data

[ Edited ]

If you are doing this within the simulator, you have to mount an "SD Card".

 

Be sure to simulate one by going to Simulate > Change SD Card.

 

Add Directory .. is the easiest setup to use and it works.

 

You can also do this from within your launch configuration by going to either Run or Debug Configurations from the Run menu.

 

Click over to Simulator then Memory tab and scroll down until you see "Use PC file System for SD Card files".

 

However, not all devices have an SD Card slot (most new ones do but its better to be sure).

 

Try checking FileSystemRegistry.listRoots() as Ted has already mentioned.

Jerome Carty - Follow jcarty on Twitter@jcarty | #webworks-dev / #BlackberryDev on irc.freenode.net | My Apps
Click "Accept as Solution" if post solved your original issue.. Give like/thumbs up if you feel post is helpful
Please use plain text.
Regular Contributor
workingHard
Posts: 54
Registered: ‎04-24-2010
My Device: 8900
My Carrier: simulator

Re: Unable to read SDCard data

Already followed these steps. I can see my files in simulator. I think the way i am accessing files is not right.

Please use plain text.
Developer
JCarty
Posts: 1,055
Registered: ‎01-25-2009
My Device: BlackBerry Bold 9650/BlackBerry Torch 9800

Re: Unable to read SDCard data

[ Edited ]

What are you trying to do exactly (besides access a file on the SD Card)?

 

I assume you are attempting to read a file on the SD Card and use the graphic inside the application?

 

What code are you using for that?

Jerome Carty - Follow jcarty on Twitter@jcarty | #webworks-dev / #BlackberryDev on irc.freenode.net | My Apps
Click "Accept as Solution" if post solved your original issue.. Give like/thumbs up if you feel post is helpful
Please use plain text.
Regular Contributor
workingHard
Posts: 54
Registered: ‎04-24-2010
My Device: 8900
My Carrier: simulator

Re: Unable to read SDCard data

Yes, i am trying to read bmp files from SDcard and display on screen

Code is:

String mybmp = "file:///SDCard/BlackBerry/pictures/mybmp.bmp";

InputStream is = getClass().getResourceAsStream(mybmp);

 try {
  byte[] data = new byte[is.available()];
  is.read(data);
  img = Bitmap.createBitmapFromBytes(data, 0, -1, 1);
 } catch (Exception ex) {}

 

I also tried this code:

 try {
  FileConnection fconn = (FileConnection)  Connector.open"file:///SDCard/BlackBerry/pictures/looxcie98.bmp");

 is = fconn.openInputStream();
 byte[] data = new byte[is.available()];
 int i = is.read(data);
 img = Bitmap.createBitmapFromBytes(data, 0, -1, 1);     
} catch (IOException e) {}

 

Always get img as null

Please use plain text.
Developer
Ted_Hopp
Posts: 1,305
Registered: ‎01-21-2009
My Device: Not Specified

Re: Unable to read SDCard data

This code:

 

 

String mybmp = "file:///SDCard/BlackBerry/pictures/mybmp.bmp";
InputStream is = getClass().getResourceAsStream(mybmp);

 

 

cannot possibly succeed. getResourceAsStream looks for a resource packaged with your application in the .cod file, not on the SDCard.

 

With the second code, it would be useful to know where it is failing. But before doing that, you can verify that the image file is actually on the (simulated) SDCard. Do this by opening Media, then selecting "Explore" from the menu. You should see two roots: "Media Card" and "Device Memory". If you don't then you have a simulator configuration issue as Jerome Carty suggested. If "Media Card" is present, you should be able to verify that your image file is present. Again, if it isn't, you have a simulator configuration issue.

 

Assuming the simulator is configured properly as above, try this version of your code, which provides more error checking and diagnostic information:

 

 

String url = "file:///SDCard/BlackBerry/pictures/looxcie98.bmp";
FileConnection fconn = null;
InputStream is = null;
try { fconn = (FileConnection) Connector.open(url, Connector.READ); if (!fconn.exists()) { throw new IOException("No such file!"); } is = fconn.openInputStream(); NoCopyByteArrayOutputStream bos = new NoCopyByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len; while ((len = is.read(buffer)) > 0) { bos.append(buffer, 0, len); } img = Bitmap.createBitmapFromBytes(bos.getByteArray(), 0, bos.size(), 1); } catch (IOException e) { System.out.println(e.toString()); } finally {
if (is != null) {
try { is.close(); } catch (IOException ignored) {}
}
if (fconn != null) {
try { fconn.close(); } catch (IOException ignored) {}
}
}

 

 




Solved? click "Accept as solution". Helpful? give kudos by clicking on the star.
Please use plain text.
Regular Contributor
workingHard
Posts: 54
Registered: ‎04-24-2010
My Device: 8900
My Carrier: simulator

Re: Unable to read SDCard data

Hi Ted & Jrome,

 

This code works perfrctly! (just changed bos.append to bos.write) . Learned many things with this exercise.  Thanks a ton for helping out and being patient.

 

Kudos for both of you!

Please use plain text.