BlackBerry Forums Support Community
              

Closed Thread
 
Thread Tools
Old 04-04-2008, 06:11 PM   #1
Happe79
Knows Where the Search Button Is
 
Join Date: Apr 2008
Model: 8800
PIN: N/A
Carrier: IT
Posts: 21
Default Access on BB store

Please Login to Remove!

HI,

I am new in developing mobile applications and I hope you can help me for my problem. Currently, I am working on a kind of File Explorer for BB. That means the application enables to browse through the BB Store folders as well as the SD card - copy, rename, delete etc. files and directories.

In order to do this I use the FileConnection class. It works fine on the SD card. Howerver, if I want to edit files or directories (e.g. creating an new directory using FileConnection.mkdir() or only reading an image e.g. "file:///store/samples/pictures/image.jpg") in the BB store I always get a FileIOException.

I wonder if there are some special permissions for the BB store? How can I set the permission (FileConnection.setWriteable does not seem to work)?

Does anybody has any experiences storing content on the BB store?

Here my code:
Code:
//creating a directory

 FileConnection fconn = (FileConnection)Connector.open(localPath);
  try
  {
  	 fconn.mkdir();
    	 fconn.setReadable(true);
    	 fconn.setWritable(true);
  }
  catch(Exception ex)
{
System.out.println(ex);
}

// copy a file

 FileConnection fconnFrom = (FileConnection)Connector.open(fromPath);
    				 
 try
{
   if (fconnFrom.exists())
   {
	fconnFrom.setReadable(true);
	fconnFrom.setWritable(true);
	int fileSize = (int) fconnFrom.fileSize();
        DataInputStream dataIn = fconnFrom.openDataInputStream();
        data = new byte[fileSize];
	dataIn.read(data, 0, fileSize);
	dataIn.close();
    }
    fconnFrom.close();
    	    		

    FileConnection fconnTo = (FileConnection)Connector.open(toPath);
    if (!fconnTo.exists());
    connTo.create();  // create the file if it doesn't exist
    fconnTo.setReadable(true);
    fconnTo.setWritable(true);
    DataOutputStream dataOut = fconnTo.openDataOutputStream();
    dataOut.write(data, 0, fileSize);
    dataOut.close();
    fconnTo.close();
}
catch(Exception ex)
 {
      System.out.print(ex);
}
THX for help in advance.

Happe

P.S. sorry for my bad english, I hope you got my problem.
Offline  
Old 04-08-2008, 02:25 AM   #2
yeresera
New Member
 
Join Date: Apr 2008
Model: 8800
PIN: N/A
Carrier: SMART
Posts: 11
Default

im also having the same problem here. how can you instantiate the fileconnection?
FileConnection rootFolder = (FileConnection)Connector.open(root);
what should i put in the variable root to access the root folder(bb store or sd card)
Offline  
Old 04-08-2008, 02:43 AM   #3
pa4o85
Thumbs Must Hurt
 
Join Date: May 2007
Location: Bulgaria
Model: none
PIN: N/A
Carrier: Mtel
Posts: 150
Default

Hi guys. If the exception is thrown when you are trying to get an image, so maybe the problem is that you tried to access an image with stream. Why don't you just try to get the image that way:

Code:
String image = "file:///store/samples/pictures/image.jpg";
Bitmap imageBitmap = Bitmap.getBitmapResource(image);
If the exception is thrown any time you tried to get some system resource maybe the problem is anywhere esle - like the path is incorrect. I tried once to get the system sounds and the path was that "file:///store/samples/ringtones/", so maybe the path you specified is right. That is how i can help you !
Offline  
Old 04-08-2008, 03:36 AM   #4
Happe79
Knows Where the Search Button Is
 
Join Date: Apr 2008
Model: 8800
PIN: N/A
Carrier: IT
Posts: 21
Default

@yeresera

try this code in order to get the root directories like "file:///store" or "file:///SDCard"

Code:
import java.util.Enumeration;
import javax.microedition.io.file.*;
import javax.microedition.io.Connector;


//get local drives
Enumeration drives = FileSystemRegistry.listRoots();

while(drives.hasMoreElements()) 
{
   String drive = (String) drives.nextElement();

   // show content of the root directories
   FileConnection fc = (FileConnection)Connector.open("file:///" + drive);
   Enumeration files = fc.list();
   fc.close();

   while(files.hasMoreElements()) 
   {
         String file = (String) files.nextElement();
         ...
         FileConnection subFC = (FileConnection)Connector.open("file:///" + drive + "/" + file);
  	 if (subFC.isDirectory())
    	 {
             // it is a directory
         }
         else
         {
             // it is a file
         }
   }
}

Hope it helps?

@pa4o85

Yes, the stream is the problem. Every time I Open an inputstream, the IOException occurs. For displaying the image, your code works fine, but what can I do wenn I want to copy the file into another directory?

THX Happe
Offline  
Old 04-08-2008, 05:04 AM   #5
pa4o85
Thumbs Must Hurt
 
Join Date: May 2007
Location: Bulgaria
Model: none
PIN: N/A
Carrier: Mtel
Posts: 150
Default

I see. For file operations i use this class and it works fine for me till now.
Code:
import java.io.DataInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;

import javax.microedition.io.file.FileConnection;
import javax.microedition.io.Connector;

import net.rim.device.api.ui.component.Dialog;

import org.w3c.dom.Document;
import org.w3c.dom.Node; 

import com.mycompany.framework.utils.Xml;

/**
 * Makes operations with files. The static fields in the class holds the names 
 * of the files that are used by the application.
 * 
 * @author Plamen Dimitrov
 * @version 1.0
 */
public class FileOperations {
  
  /** 
   * FileConnection object. It makes a connection with all the xml files
   * that holds all the databases.
   */
  private FileConnection fconn = null;
  
  /**
   * Constructor
   */
  public FileOperations() {
  }

  /**
   * Gets the information from the xml file with a name fileName argument. Holds 
   * this information in Document object.
   * 
   * @param fileName - String
   * @return doc - Document
   */
  public Document readInputStream(String fileName) {
    InputStream is = null;
    Document doc = null;
    try {
      fconn = (FileConnection) Connector.open(fileName);
      if (!fconn.exists()) {
      }
      is = fconn.openInputStream();
      doc = Xml.parse(is);
      is.close();
    } catch (Exception e) {
      Dialog.alert("Exception: " + e);
    }
    return doc;
  }
  
  /**
   * Close currently set connection.
   */
  public void closeConnection() {
    if (fconn != null)
      try {
        fconn.close();
      } catch (Exception e) {
        Dialog.alert("Exception: " + e);
      }
  }
  
  /**
   * Write a xml node to a file with a specified name (fileName). If the file 
   * does not exist, a new file with that name is created.
   * 
   * @param node - Node
   * @param fileName - String
   */
  public void writeNodeToFile(Node node, String fileName) {
    try {
      fconn = (FileConnection) Connector.open(fileName);
      if (!fconn.exists()) {
        fconn.create();
      }
      fconn.truncate(0);
      OutputStream os = fconn.openOutputStream();
      PrintStream ps = new PrintStream(os);
      XMLDocumentWriter dw = new XMLDocumentWriter(ps);
      dw.write(node);
      dw.close();
      ps.flush();
      ps.close();
      os.close();
    } catch (Exception e) {
      Dialog.alert("Exception: " + e);
    }
  }
  
  /**
   * Read file and gets the information as byte array.
   * 
   * @param fileName - String
   * @return content - byte array
   */
  public byte[] readFile(String fileName) {
    byte[] content = null;
    try {
      fconn = (FileConnection) Connector.open(fileName);
      if (!fconn.exists()) {
        return content;
      }
      DataInputStream is = fconn.openDataInputStream();
      content = new byte[(int) fconn.fileSize()];
      is.readFully(content, 0, content.length);
      is.close();
    } catch (Exception e) {
      Dialog.alert("Exception: " + e);
    }
    return content;
  }
  
  /**
   * Gets the file size of a file with the name given in the argument.
   * 
   * @param fileName - String
   * @return size - long
   */
  public long getFileSize(String fileName) {
    long size = 0;
    try {
      fconn = (FileConnection) Connector.open(fileName);
      if (!fconn.exists()) {
      }
      size = fconn.fileSize();
    } catch (Exception e) {
      Dialog.alert("Exception: " + e);
    }
    return size;
  }
  
}
In fact i uses only xml files to access data, but you can use this for all kinds of files i think. Or maybe the system files are not available to be accessed, but i accessed the system ringtones, so the images could be available too.
Offline  
Old 04-08-2008, 11:06 AM   #6
Happe79
Knows Where the Search Button Is
 
Join Date: Apr 2008
Model: 8800
PIN: N/A
Carrier: IT
Posts: 21
Default

@pa4o85

Maybe I'm wrong, but I can't see a real difference between your and my code regarding to the file operations.

Happe
Offline  
Old 04-08-2008, 11:33 AM   #7
blackharry
New Member
 
Join Date: Apr 2008
Model: 8310
PIN: N/A
Carrier: none
Posts: 13
Default

mmhh, a filesystem-browser produces a lot of user-dialogs to confirm the filesystem-access, doesn't it?? did you find a way to get rid of those confirmations?? i set all permissions to "allow" and still the dialogs pop up!
Offline  
Old 04-08-2008, 01:31 PM   #8
Happe79
Knows Where the Search Button Is
 
Join Date: Apr 2008
Model: 8800
PIN: N/A
Carrier: IT
Posts: 21
Default

I did not get your point. Where and how do you set the permision? Do you have sample code?

THX Happe
Offline  
Old 04-09-2008, 12:18 AM   #9
yeresera
New Member
 
Join Date: Apr 2008
Model: 8800
PIN: N/A
Carrier: SMART
Posts: 11
Default

just a quick question.. how can i identify what i will put in the content type here:

SupportedAttachmentPart attachPart =
new SupportedAttachmentPart(multipart, contentType,
fileName, readFile(fileName));

I've tried to use: "image/jpeg"(I know that im attaching a jpg file). but the attachment cannot be viewed.
Offline  
Old 04-09-2008, 01:12 AM   #10
Happe79
Knows Where the Search Button Is
 
Join Date: Apr 2008
Model: 8800
PIN: N/A
Carrier: IT
Posts: 21
Default

I'm currently working on the same thing. I used different content types without success.
Offline  
Old 04-09-2008, 01:56 AM   #11
pa4o85
Thumbs Must Hurt
 
Join Date: May 2007
Location: Bulgaria
Model: none
PIN: N/A
Carrier: Mtel
Posts: 150
Default

@Happe79

I did not match your class and my, but i just wanted to share my class for IO operations that works fine for me. Thanks !
Offline  
Old 04-09-2008, 02:35 AM   #12
blackharry
New Member
 
Join Date: Apr 2008
Model: 8310
PIN: N/A
Carrier: none
Posts: 13
Default

Quote:
Originally Posted by Happe79 View Post
I did not get your point. Where and how do you set the permision? Do you have sample code?

THX Happe
what i ment was, that each time the fileconnection is used the user is promted:

"The application [..] has attempted to open local content. Would you like to allow this?"

what can be done to get rid of those confirmations??? a BB-signature does not seem to help and setting all of the applications permissions (options -> advanced options -> applications -> set permissions) to "allow" does not help either!

... must be pretty annoying using a file browser while having to confirm those messages evertime you change the directory!? or what did you to in order not to have those confirmations?
Offline  
Old 04-09-2008, 09:54 AM   #13
Happe79
Knows Where the Search Button Is
 
Join Date: Apr 2008
Model: 8800
PIN: N/A
Carrier: IT
Posts: 21
Default

This message apears only one time if I set "always allow" or soemthing like this.

The other error message only apears when I read the content of the file using the OpenInputstream.

THX Happe
Offline  
Closed Thread



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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


6500/1 A 6502 VARIENT CPU COMMODORE USE IN AMIGA KB & 1520 PLOTTER NMOS NCR NOS picture

6500/1 A 6502 VARIENT CPU COMMODORE USE IN AMIGA KB & 1520 PLOTTER NMOS NCR NOS

$5.89



[1pcs] 8520A-1 MOS 8520A-1  Commodore Amiga DIP40 USED picture

[1pcs] 8520A-1 MOS 8520A-1 Commodore Amiga DIP40 USED

$15.07



[1pcs] 8520A-1 CSG 8520A-1  Commodore Amiga DIP40 USED picture

[1pcs] 8520A-1 CSG 8520A-1 Commodore Amiga DIP40 USED

$15.57



New Greaseweazle V4.1 USB Floppy Adapter Flux Reader Writer Amiga Atari ST 1591 picture

New Greaseweazle V4.1 USB Floppy Adapter Flux Reader Writer Amiga Atari ST 1591

$28.46



1PC USED A54MA55B BC186A413G52 Mitsubishi A500/F540 Series 55KW Drive Board #CZ picture

1PC USED A54MA55B BC186A413G52 Mitsubishi A500/F540 Series 55KW Drive Board #CZ

$317.00



1PC USED A54MA30B Mitsubishi F500/A500/A540 Series 30-37KW Drive Main Board #CZ picture

1PC USED A54MA30B Mitsubishi F500/A500/A540 Series 30-37KW Drive Main Board #CZ

$255.00







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