Wednesday, August 19, 2009

Read file to base 64 String in java blackberry device

public String readFileToBase64(String path)
{
String str = null;
try
{
FileConnection fc = (FileConnection)Connector.open(path, Connector.READ);
if(!fc.exists())
{
System.out.println("File doesn't exist!");
}
else
{
int size = (int)fc.fileSize();
InputStream is = fc.openInputStream();
byte bytes[] = new byte[size];
is.read(bytes, 0, size);
Base64 bb=new Base64();
byte bytes1[]=Base64.encodeBase64(bytes);
str=new String(bytes1);
Dialog.alert(" Length of String="+str.length());
}

}
catch (IOException ioe)
{
System.out.println("IOException: "+ioe.getMessage());
}
return str;
}

You have to import
org.apache.commons.codec.binary.Base64
find this and save in your project area

No comments:

Post a Comment