01-04-2010 05:59 AM
Hi guys, I'm using the 4.6.1 JDE. I want to store a downloaded image on the file system and retrieve it later. Something like a caching option. Everything works perfectly on the simulator but when I install on the device it doesn't work. Here's the code,
private StringBuffer Imagedirectory = new StringBuffer(System.getProperty("fileconn.dir.phot os"));
FileConnection fc = (FileConnection)Connector.open(Imagedirectory.toSt ring(),Connector.READ_WRITE);
if(!fc.exists()){
Util.getWebData(url, this);
}else{
byte[] data = getData(fc);
bitmap = EncodedImage.createEncodedImage(data, 0,
data.length);
if(this.width !=0 && this.height !=0){
bitmap = ScaleImageToSize(bitmap, this.width,this.height);
}
setImage(bitmap);
}this is where I get the data from the file,
private byte[] getData(FileConnection file)
{
byte[] data = new byte[0];
try
{
InputStream input = file.openInputStream();
//////////
int SIZE = 100000;
ByteArrayOutputStream byteArrayOutputStream = new
ByteArrayOutputStream();
byte[] buffer = new byte[SIZE];
while (true)
{
int bytesRead = input.read( buffer, 0, SIZE );
if (bytesRead == -1) break;
byteArrayOutputStream.write( buffer, 0, bytesRead );
}
data = byteArrayOutputStream.toByteArray();
byteArrayOutputStream.flush();
byteArrayOutputStream.close();
}
catch(Exception e)
{
System.out.println(e.toString());
}
return data;
} this is where I write the image to a file,
private void writeFile(byte[] data, String fileName)
{
FileConnection fconn = null;
try {
fconn = (FileConnection) Connector.open(fileName,
Connector.READ_WRITE);
} catch (IOException e) {
System.out.print("Error opening file");
}
if (fconn.exists())
try {
fconn.delete();
} catch (IOException e) {
System.out.print("Error deleting file");
}
try {
fconn.create();
} catch (IOException e) {
System.out.print("Error creating file");
}
OutputStream out = null;
try {
out = fconn.openOutputStream();
} catch (IOException e) {
System.out.print("Error opening output stream");
}
try {
out.write(data);
out.flush();
} catch (IOException e) {
System.out.print("Error writing to output stream");
}
try {
fconn.close();
} catch (IOException e) {
System.out.print("Error closing file");
}
}
Please help.
Solved! Go to Solution.
01-04-2010 07:06 AM
Are you getting an exception (or the println output specifying and error) or does everything run perfect and you just can't find the file?
01-04-2010 07:12 AM
And what doesn't work?
01-04-2010 07:13 AM - edited 01-04-2010 07:19 AM
Thank you so much for replying.
I found the problem, the byte array which I was parsing was not proper, so I first created the image and after that used the byte information from that to save the image. Here's the solution.
public void callback(final String data)
{
if (data.startsWith("Exception")) return;
try
{
byte[] dataArray = data.getBytes();
bitmap = EncodedImage.createEncodedImage(dataArray, 0,
dataArray.length);
if(this.width !=0 && this.height !=0){
bitmap = ScaleImageToSize(bitmap, this.width,this.height);
}
setImage(bitmap);
if(isWriteImage()){
System.out.println("Before writing the image");
writeFile(bitmap.getData(), Imagedirectory.toString());
System.out.println(Imagedirectory);
System.out.println("Image written - Operation Successful");
}
}
catch (final Exception e){
System.out.println("Image Creation Failed");
}
}
The problem was because I was parsing the byte array from the string variable data. Any idea why this caused an issue?