ARKhoshghalb
ARKhoshghalb

Reputation: 95

FileNotFoundException when writing to raw file

I'm trying to write in file "numbers.txt". It's located in raw folder.
I'm using this function to write in file:

public void writeToFile(String data) {
    try {
        Uri url = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.numbers);
        String Path = url.toString();
        File f = new File(Path);
        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(new FileOutputStream(f));
        outputStreamWriter.write(data);
        outputStreamWriter.close();
    }
    catch (IOException e) {
        Log.e("Exception", "File write failed: " + e.toString());
    }
}

When I run the function the Log says :

File write failed: java.io.FileNotFoundException: android.resource:/com.sakt.emergencymessage/2131099648: open failed: ENOENT (No such file or directory)

But the file exists and I can read from it.
And yes, I have written the permission WRITE_EXTERNAL_STORAGE in Manifest.

Thanks for your help.

Upvotes: 0

Views: 538

Answers (2)

ganeshvjy
ganeshvjy

Reputation: 407

You need to use the external storage on the device. You can't and shouldn't be writing to the resources. Instead, copy the resource to the external storage, and then modify it there.

Upvotes: 0

Simas
Simas

Reputation: 44118

You cannot write to resource files. They are read-only.

Upvotes: 2

Related Questions