Accessing the 'Media' directory of a Blackberry within the JDK - blackberry

Trying to use JSR 75 to access media saved under the '/home/video/' directory on the device. Using Blackbery JDK 4.6.1. Single line of code throws a 'FileSystem IO Error' Exception. Which is, as usual, unhelpful in the extreme.
fconn = (FileConnection)Connector.open("file:///home/user/videos/"+name, Connector.READ);
Has anyone tried to do this? I can open files within my jar, but can't seem to access the media folder. I have the javax.microedition.io.Connector.file.read permission set and my appplication is signed.

There are two kind of filesystems on BlackBerry - SDCard and store. You have to use one of them, defining it in the path. Standard directory on SDCard where video, music etc stored is "file:///SDCard/BlackBerry".
String standardPath = "file:///SDCard/BlackBerry";
String videoDir = System.getProperty("fileconn.dir.videos.name");
String fileName = "video.txt";
String path = standardPath+"/"+videoDir+"/"+fileName;
String content = "";
FileConnection fconn = null;
DataInputStream is = null;
ByteVector bytes = new ByteVector();
try {
fconn = (FileConnection) Connector.open(path, Connector.READ);
is = fconn.openDataInputStream();
int c = is.read();
while(-1 != c)
{
bytes.addElement((byte) (c));
c = is.read();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
content = new String(bytes.toArray());
add(new RichTextField(content));
See also
SUN Dev Network - Getting Started with the FileConnection APIs
RIM Forum - Some questions about FileConnection/JSR 75
Use System.getProperty("fileconn.dir.memorycard") to check if SDCard available
How to save & delete a Bitmap image in Blackberry Storm?

Related

Blackberry "File Not found" when accessing SD Card (Cordova 2.3.0 + Plugin)

I've written an app using PhoneGap/Cordova that downloads content to the local file system for later viewing, it does this using the Cordova libraries for access in the file system and transferring files.
Some of the data comes in the form of a zip archive, so I need to unzip the content.
There isn't a standard plugin for extracting ZIP files in Cordova, but I took the existing plugin for iOS that we were already using, kept the same interface, and reimplemented in Java wrapping ZipME for the BB platform.
My problem is that then I try to access my file to unzip it, I get a "File not found" exception being thrown.
This is the code:
public void extract(String sourceFile, String targetDirectory) throws IOException {
byte[] buff = new byte[1024];
InputStream inStream = null;
OutputStream outStream = null;
try {
FileConnection fcIn = (FileConnection) Connector.open(sourceFile, Connector.READ_WRITE);
FileConnection fcOut;
inStream = fcIn.openInputStream();
ZipInputStream zipIS = new ZipInputStream(inStream);
ZipEntry zipEntry = zipIS.getNextEntry();
while (zipEntry != null) {
String fileName = zipEntry.getName();
fcOut = (FileConnection) Connector.open(targetDirectory + fileName, Connector.READ_WRITE);
outStream = fcOut.openOutputStream();
int readLength;
while ((readLength = inStream.read(buff)) > 0) {
outStream.write(buff, 0, readLength);
}
outStream.flush();
outStream.close();
zipEntry = zipIS.getNextEntry();
}
} catch (RuntimeException e) {
Logger.error(TAG + "Unzip failed: " + e.getMessage());
throw new ZipException("Unable to unzip file: " + e.getMessage());
} finally {
// Cleanup code
if (inStream != null) {
inStream.close();
}
if (outStream != null) {
outStream.close();
}
}
}
Debugging confirms that I am passing in the following parameters:
Source file: file:///SDCard/downloadable/B3/B3.zip
Dest Dir: file:///SDCard/downloadable/B3/
And examining the SD card on a test device, and looking at the synthetic SD card on a simulator both confirm the following file structure:
root
- BlackBerry
- Etc
- temp
- downloadable
- B1
- B2
- B2.zip
- B3
- B3.zip
- B4
- B5
- B6
I'm running the tests on BB 7.0 and 7.1, Cordova is V2.3.0, and the test devices are a 9800, 9810, and simulators for 9800 and 9810 running either the BB 7.0 or 7.1 OS.

How to compress the files in Blackberry?

In my application I used html template and images for browser field and saved in the sdcard . Now I want to compress that html,image files and send to the PHP server. How can I compress that files and send to server? Provide me some samples that may help lot.
i tried this way... my code is
EDIT:
private void zipthefile() {
String out_path = "file:///SDCard/" + "newtemplate.zip";
String in_path = "file:///SDCard/" + "newtemplate.html";
InputStream inputStream = null;
GZIPOutputStream os = null;
try {
FileConnection fileConnection = (FileConnection) Connector
.open(in_path);//read the file from path
if (fileConnection.exists()) {
inputStream = fileConnection.openInputStream();
}
byte[] buffer = new byte[1024];
FileConnection path = (FileConnection) Connector
.open(out_path,
Connector.READ_WRITE);//create the out put file path
if (!path.exists()) {
path.create();
}
os = new GZIPOutputStream(path.openOutputStream());// for create the gzip file
int c;
while ((c = inputStream.read()) != -1) {
os.write(c);
}
} catch (Exception e) {
Dialog.alert("" + e.toString());
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
Dialog.alert("" + e.toString());
}
}
if (os != null) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
Dialog.alert("" + e.toString());
}
}
}
}
this code working fine for single file but i want to compress all the file(more the one file)in the folder .
In case you are not familiar with them, I can tell you that in Java the stream classes follow the Decorator Pattern. These are meant to be piped to other streams to perform additional tasks. For instance, a FileOutputStream allows you to write bytes to a file, if you decorate it with a BufferedOutputStream then you get also buffering (big chunks of data are stored in RAM before being finally written to disc). Or if you decorate it with a GZIPOutputStream then you get also compression.
Example:
//To read compressed file:
InputStream is = new GZIPInputStream(new FileInputStream("full_compressed_file_path_here"));
//To write to a compressed file:
OutputStream os = new GZIPOutputStream(new FileOutputStream("full_compressed_file_path_here"));
This is a good tutorial covering basic I/O . Despite being written for JavaSE, you'll find it useful since most things work the same in BlackBerry.
In the API you have these classes available:
GZIPInputStream
GZIPOutputStream
ZLibInputStream
ZLibOutputStream
If you need to convert between streams and byte array use IOUtilities class or ByteArrayOutputStream and ByteArrayInputStream.

how to display images present in blackberry device into my application

I am developing a blackberry application where i want to select an image present in device and display it in my application. How to do it.
UPDATE
hi I used FilePicker to get the path of the file and i am storing it in "Selection(String)"
and i am using below code to display image in my application but i am getting exception. can anybody tell me where i did mistake.
try {
FileConnection fconn = (FileConnection)Connector.open(selection,Connector.READ);
// If no exception is thrown, then the URI is valid, but the file may or may not exist.
if (fconn.exists()) {
InputStream input = fconn.openInputStream();
int available = input.available();
byte[] data = new byte[available];
input.read(data, 0, available);
EncodedImage image = EncodedImage.createEncodedImage(data,0,data.length);
Bitmap b = image.getBitmap();
BitmapField picture = new BitmapField(b);
add(picture);
add(new LabelField("Data Length:" + data.length));
}
else {
add(new LabelField("Picture does not exist"));
}
fconn.close();
}
catch (Exception ioe) {
add(new LabelField("Error"));
}
If your target OS is 6.0+ you can use RIM component FilePicker.
For lower OS versions you can use also this component: File Selection Popup

My App is not loaded in Blackberry simulator

I am using following code to create xml file -
void createxml(){
Document d = new Document();
Element root = d.createElement("","company");
Element employee = d.createElement("","employee");
employee.setAttribute("","id","1");
Element fname = d.createElement("","fname");
fname.addChild(Node.TEXT,"Vasudev");
Element lname = d.createElement("","lname");
lname.addChild(Node.TEXT,"Kamath");
Element address = d.createElement(Node.TEXT+"","address");
address.addChild(Node.TEXT,"Karkala");
employee.addChild(Node.ELEMENT,fname);
employee.addChild(Node.ELEMENT,lname);
employee.addChild(Node.ELEMENT,address);
root.addChild(Node.ELEMENT,employee);
d.addChild(Node.ELEMENT,root);
String fileName = "file:///SDCard/Blackberry/company.xml";
DataOutputStream os = null;
FileConnection fc = null;
try
{
fc = (FileConnection)Connector.open(fileName,Connector.READ_WRITE);
if (! fc.exists())
fc.create();
os = fc.openDataOutputStream();
KXmlSerializer serializer = new KXmlSerializer();
serializer.setOutput(os, "UTF-8");
d.write(serializer);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
But when I write this method, my program is not going to be load on the simulator, if I comment It loads easily. How do i solve this problem?
Do u simulate your app?
If you running in simulator, create a filder SDCard in your system and then create a sub folder Blackberry. And when you run your app take take the 'Simulate' menu > Change Sd card> Add Directory> and browse for the folder SDcard .... then run you app

How to retrieve data from a attached zip file in Blackberry application?

I am using eclipse to build application for Blackberry. I attached a zip file with my application. Please help me, I don't know how to retrieve data form the zip file in application development.
In BlackBerry we can use two compression standarts: GZip and ZLib.
Choose one, then compress your file and add to project.
Then you should be able to open it as an resource.
After that decompress it with GZIPInputStream or ZLibInputStream accordingly.
Example (uncompress and print text from test.gz attached to project):
try
{
InputStream inputStream = getClass().getResourceAsStream("test.gz");
GZIPInputStream gzis = new GZIPInputStream(inputStream);
StringBuffer sb = new StringBuffer();
int i;
while ((i = gzis.read()) != -1)
{
sb.append((char)i);
}
String data = sb.toString();
add(new RichTextField(data));
gzis.close();
}
catch(IOException ioe)
{
//do something here
}

Resources