BlackBerry Forums Support Community               

Closed Thread
 
LinkBack Thread Tools
Old 08-13-2007, 02:36 AM   #1 (permalink)
Thumbs Must Hurt
 
Join Date: Jun 2007
Model: 8800
PIN: N/A
Carrier: GPRS
Posts: 68
Post Thanks: 0
Thanked 0 Times in 0 Posts
Question A problem of read file

Please Login to Remove!

Hi, everyone:

I'm learning to develop a program to read a picture from SDCard, and I faile in read it correctly by InputStream.read(), the program is as follow:
try
{
FileConnection fconn = (FileConnection)Connector.open("file:///SDCard/BlackBerry/pictures/3.jpg");
if (!fconn.exists())
{
System.out.println("The file do not exist!");
}
System.out.println(fconn.fileSize()); //works well
System.out.println( fconn.getName() );//works well
System.out.println( fconn.getPath() );//works well

InputStream input = fconn.openInputStream();
int available = input.available();
//there is an error: available = 0, it should be the size value of the file.

fconn.close();
}
catch (Exception ioe)
{
System.out.println(ioe.getMessage());
}

By the debugging , I find that fconn is created successful , and it's methods fileSize/getName/getPath all works well., but the input.available() makes an error!

I'm developing on JDE 4.2.1 ,and if I change the file path to read a system picture , such as "file:///store/samples/pictures/DuskClouds.jpg", the program work well and the value of available is right! While "3.jpg" is a picture I copy to simulator 8800 by Desktop Management Software, and it's size is about 116K.

Any ideas will be appreciated , Thank you very much!

Streamh
Offline  
Old 08-13-2007, 03:13 AM   #2 (permalink)
Thumbs Must Hurt
 
Rose's Avatar
 
Join Date: Nov 2006
Location: India
Model: 9700
OS: Windows 7
Carrier: Airtel
Posts: 121
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

Try this approach


FileConnection fconn = (FileConnection)Connector.open("file:///SDCard/BlackBerry/pictures/3.jpg");
if (!fconn.exists())
{
System.out.println("The file do not exist!");
}
System.out.println(fconn.fileSize()); //works well
System.out.println( fconn.getName() );//works well
System.out.println( fconn.getPath() );//works well

InputStream input = fconn.openInputStream();
int length = (int)fconn.fileSize();//totalSize();

//allocating a temporary Buffer.
byte[] b = new byte[length];

//reading data from a InputStream
input .read(b,0,length);

fconn.close();
}
catch (Exception ioe)
{
System.out.println(ioe.getMessage());
}
Offline  
Old 08-13-2007, 06:42 AM   #3 (permalink)
Thumbs Must Hurt
 
Join Date: Jun 2007
Model: 8800
PIN: N/A
Carrier: GPRS
Posts: 68
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

Rosexxx65306;
Thank you for your replay! And just as your doing , if I get the file size by the "int length = (int)fconn.fileSize();" and the problem will be solved!

But I'm puzzled about the method InputStream.available(), according to the illuminate of "Returns the number of bytes that can be read (or skipped over) from this input stream ", the available() also will return the file size and it turned to be ture when process the system picture.
And why it occurs an error in processing the copyed file? Is there any difference between the two pictures?

Do you have any ideas?Thank you!!
Offline  
Old 08-13-2007, 08:03 AM   #4 (permalink)
Thumbs Must Hurt
 
Join Date: Jun 2007
Location: Bangladesh
Model: 8800
PIN: N/A
Carrier: EDGE
Posts: 93
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi streamh,

Quote:
Originally Posted by streamh View Post
And why it occurs an error in processing the copyed file? Is there any difference between the two pictures?
InputStream; this abstract class is the superclass of all classes representing an input stream of bytes.

Direct Known Subclasses:
Base64InputStream, ByteArrayInputStream, CMSInputStream, CryptoInputStream, DataInputStream, FileInputStream, GZIPInputStream, MIMEInputStream, SharedInputStream, ZLibInputStream

Applications that need to define a subclass of InputStream must always provide a method that returns the next byte of input.

public int available() throws IOException; The available method for class InputStream always returns 0. This method should be overridden by subclasses.

Now, hope we are in same stage!

Cheers,
ARIF
Offline  
Old 08-13-2007, 09:21 PM   #5 (permalink)
Thumbs Must Hurt
 
Join Date: Jun 2007
Model: 8800
PIN: N/A
Carrier: GPRS
Posts: 68
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

ARIF:

Thank you for your help! I read your reply carefully and it's ture in the logical!

Firstly, "The available method for class InputStream always returns 0." it's not ture, because if I read a system picture "DuskClouds.jpg",
the sentence:
InputStream input = fconn.openInputStream();
int available = input.available();
will return the file size (32769), but not for the "3.jpg";

Secondly, because "This method should be overridden by subclasses" , I ues DataInputStream instead like this:
FileConnection fconn1 = (FileConnection)Connector.open("file:///store/samples/pictures/DuskClouds.jpg");
FileConnection fconn2 = (FileConnection)Connector.open("file:///SDCard/BlackBerry/pictures/3.txt");
DataInputStream input1 = fconn1.openDataInputStream();
int available1 = input1.available(); //something wrong
DataInputStream input2 = fconn2.openDataInputStream();
int available2 = input2.available();
and find that available1=32769 , but available2 = 0
So it's strange for me!
whether the behavior of the method available() is unexpected?
Offline  
Closed Thread


Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On





Copyright 2004-2014 BlackBerryForums.com.
The names RIM and BlackBerry are registered Trademarks of BlackBerry Inc.