Read/Write Files from the Content Provider

Posted by drum on Stack Overflow See other posts from Stack Overflow or by drum
Published on 2012-03-21T22:14:11Z Indexed on 2012/03/21 23:30 UTC
Read the original article Hit count: 198

Filed under:
|
|
|

I want to be able to create a file from the Content Provider, however I get the following error:

java.io.Filenotfoundexception: /0: open file failed: erofs (read-only file system)

What I am trying to do is create a file whenever an application calls the insert method from my Provider. This is the excerpt of the code that does the file creation:

FileWriter fstream = new FileWriter(valueKey);
BufferedWriter out = new BufferedWriter(fstream); 
out.write(valueContent);
out.close();

Originally I wanted to use openFileOutput() but the function appears to be undefined.

Anyone has a workaround to this problem?

EDIT: I found out that I had to specify the directory as well. Here is a more complete snippet of the code:

File file = new File("/data/data/Project.Package.Structure/files/"+valueKey);
file.createNewFile();
FileWriter fstream = new FileWriter(file);
BufferedWriter out = new BufferedWriter(fstream);
out.write(valueContent);
out.close();    

I also enabled the permission

<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />

This time I got an error saying:

java.io.IOException: open failed: ENOENT (No such file or directory)

© Stack Overflow or respective owner

Related posts about android

Related posts about file