android.content.Context API constraints

All your problems with Audio, Video and Images.

android.content.Context API constraints

Postby pablonc » Tue Jul 21, 2009 10:19 am

Hello everyone, I've been reading a few days here and I'm starting to migrate some MIDP apps I built into Android ones and I am experiencing some problems with Data Storage. I want to have an Activity from which I store some images requested via HTTP. I want those images to be private to the application and I want to organize them un subdirectories.

I try the context.openFileInput(FILE_NAME); but as the JavaDoc says, a path cannot be specify... why such a limitation? in this case it is clear that I need those files from within the same Activity... or well... I don't mind having them organize in folders at an Application level, but either case seams to be impossible.

I try also using java.io package straight, with the following code:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.                  try {         
  2.                   File file= new File("MyFile.txt");
  3.                   if(!file.exists()){
  4.                      file.createNewFile();
  5.                      System.out.println("Nuevo archivo creado...");
  6.                   }
  7.                   fos = new FileOutputStream(file);
  8.                   dos=new DataOutputStream(fos);
  9.                   dos.writeInt(2333);
  10.                   dos.writeChars("holamundo...");              
  11.                 } catch (IOException e) {
  12.                   e.printStackTrace();
  13.                 }
  14.  
Parsed in 0.032 seconds, using GeSHi 1.0.8.4


but throws

07-21 08:43:44.035: WARN/System.err(1338): java.io.IOException: Parent directory of file is not writable: MyFile.txt

And I don't know what else to prove :cry:
User avatar
pablonc
Freshman
Freshman
 
Posts: 8
Joined: Mon Jul 13, 2009 1:14 pm

Top

Postby pablonc » Tue Jul 21, 2009 11:22 am

I'm going to reply to myself...

The problem was that I need to point to the package directory I am working with. One have to write in the package in which the app is running. My save file code is as follows:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. protected void saveFileText() {
  3.  
  4.                 FileOutputStream fos;
  5.  
  6.                 DataOutputStream dos;
  7.  
  8.                
  9.  
  10.                 try {          
  11.  
  12.                        
  13.  
  14.                   boolean success = new File("/data/data/PACKAGE_NAME/files/subdir").mkdir();
  15.  
  16.  
  17.  
  18.                   if (!success) {
  19.  
  20.                          Log.i(this.toString(), "no success");
  21.  
  22.                   }
  23.  
  24.                   File file =  new File("/data/data/PACKAGE_NAME/files/subdir/MyFile.txt");
  25.  
  26.                   file.createNewFile();
  27.  
  28.                   if(!file.exists()){
  29.  
  30.                      file.createNewFile();
  31.  
  32.                      Log.i(this.toString(), "File created...");
  33.  
  34.                   }
  35.  
  36.                   fos = new FileOutputStream(file);
  37.  
  38.                   dos=new DataOutputStream(fos);
  39.  
  40.                   dos.writeChars("helloworld...");             
  41.  
  42.                 } catch (IOException e) {
  43.  
  44.                   e.printStackTrace();
  45.  
  46.                 }
  47.  
  48.     }
  49.  
  50.  
Parsed in 0.034 seconds, using GeSHi 1.0.8.4


I hope it helps somebody else.
User avatar
pablonc
Freshman
Freshman
 
Posts: 8
Joined: Mon Jul 13, 2009 1:14 pm

Postby TheChosen » Tue Jul 21, 2009 3:10 pm

Shouldn't you work with the Context.openFileInput() or Context.openFileOutput() methods as mentioned in the developer documentation?
TheChosen
Experienced Developer
Experienced Developer
 
Posts: 62
Joined: Tue Jan 06, 2009 7:18 pm
Location: Germany

Postby pablonc » Tue Jul 21, 2009 3:18 pm

TheChosen wrote:Shouldn't you work with the Context.openFileInput() or Context.openFileOutput() methods as mentioned in the developer documentation?

Nope, because you cannot specify a path, so unless there is a way to create and navigate through the storage folders I cannot use Context.openFileInput() method.
Vi veri veriversum vivus vici
http://www.androiditis.com - Inflamados por el marciano!
User avatar
pablonc
Freshman
Freshman
 
Posts: 8
Joined: Mon Jul 13, 2009 1:14 pm

Postby TheChosen » Tue Jul 21, 2009 3:28 pm

It seems you're right. Even though they write
To write to a file, call Context.openFileOutput() with the name and path.

it is impossible to specify a path. Following the Context.openFileOutput() API:
Open a private file associated with this Context's application package for writing. Creates the file if it doesn't already exist.
Parameters
name The name of the file to open; can not contain path separators.

So how shall you specify the path when the parameter shall not contain path separators? Weird API...
TheChosen
Experienced Developer
Experienced Developer
 
Posts: 62
Joined: Tue Jan 06, 2009 7:18 pm
Location: Germany

Postby pablonc » Tue Jul 21, 2009 4:03 pm

It's kind of incredible but true... even I still believe that there must be another way... the API spec is clear..

Imagine that you request a image via http and you want to cache it, then you request another image from another location but with the same name... that is my problem, you can either rename the second image and create and index table to now the original name or the solution I want to accomplish, just save them in different folders.

At least I found a solution :D
Vi veri veriversum vivus vici
http://www.androiditis.com - Inflamados por el marciano!
User avatar
pablonc
Freshman
Freshman
 
Posts: 8
Joined: Mon Jul 13, 2009 1:14 pm

Top

Creating files

Postby milkround » Sat Aug 08, 2009 9:09 am

Hi,

Thanks a lot for this discussion - it has help me alot! - I guess the situation is still the same in R3??

Steve
milkround
Developer
Developer
 
Posts: 34
Joined: Sat May 02, 2009 2:29 pm
Location: UK & Malta

Postby Safy » Wed Oct 07, 2009 11:55 am

As per the post of pablonc, we can create a file within the package name.

Basically I want to know when the application is uninstalled will that file get deleted.




Thanks...
Safy..
Safy
Freshman
Freshman
 
Posts: 2
Joined: Wed Oct 07, 2009 11:35 am
Location: India

Postby bytes » Wed Dec 09, 2009 11:10 am

Yes, after uninstalling our application, all these files will be deleted.

The path: /data/data/<pkg name>/files

On uninstallation, <pkg name> in the above path will be deleted.

cheers
-Bytes
bytes
Freshman
Freshman
 
Posts: 2
Joined: Wed Dec 09, 2009 10:02 am

Top

Return to Multimedia Problems

Who is online

Users browsing this forum: No registered users and 10 guests