Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android write to file - can't find file

Tags:

android

I am a newbie in Android development. I try to write something to a file. I am using the code:

try {
                    FileOutputStream fOut = openFileOutput(
                            "out.txt", MODE_WORLD_READABLE);

                    OutputStreamWriter osw = new OutputStreamWriter(fOut);
                    osw.write("something");
                    osw.flush();
                    osw.close();
                    fOut.close();

                } catch (MalformedURLException e) {
                    Log.e("FILE WRITE ERROR", e.getMessage());
                } catch (IOException e) {
                    Log.e("FILE WRITE ERROR", e.getMessage());
                }

Everything it's ok but I can't find the file in DDMS FIle Explorer. Could you help me and tell me where Android saves the file? It isn't in "/data/data/my_project_package". Thank you.

like image 305
Tibanu Avatar asked Jul 22 '10 11:07

Tibanu


2 Answers

1) be sure to have this in your manifest:

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

2) It makes a difference, either testing with the emulator or the device. In case of the latter, be sure to unmount the device from your PC (Pick: "Only charging"). Debugging will still work that way. It's just that you cannot write and watch at the same time.

This method works:

public void writeToExternalStoragePublic(String filename, int content) {
    String packageName = this.getPackageName();
    String path = Environment.getExternalStorageDirectory().getAbsolutePath()+ "/Android/data/" + packageName + "/files/";
    if (isExternalStorageAvailable() && !isExternalStorageReadOnly()) {
        try {
            boolean exists = (new File(path)).exists();
            if (!exists) {
                new File(path).mkdirs();
            }
            // Open output stream
            FileOutputStream fOut = new FileOutputStream(path + filename,true);
            // write integers as separated ascii's
            fOut.write((Integer.valueOf(content).toString() + " ").getBytes());
            fOut.write((Integer.valueOf(content).toString() + " ").getBytes());
            // Close output stream
            fOut.flush();
            fOut.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
like image 84
Huck Finn Avatar answered Sep 29 '22 11:09

Huck Finn


There seems to be a lot of questions on this topic and the problem often seems to boil down to i) not requesting permissions in the AndroidManifest.xml ii) The device's storage being mounted to a computer at the time the app was being run or iii) emulator settings.

In the hope that others who have tried all these solutions and failed, I submit iv) computer decides to arbitrarily ignore your permission setting in AndroidManifest.xml after pulling code from a source repository that was written on another computer. In my case I'd just written some code on Computer A that wrote to external storage. I checked said code into source control and then pulled said code to Computer B. When I reinstalled the app from Computer B onto the device, I started getting the Permission denied exception.

The fix: make any change to the AndroidManifest.xml file on Computer B (e.g. insert a newline somewhere). This fixed the problem for me. I don't know why. It's probably one of the rarer cases, but more frustrating ones.

like image 25
Bryce Thomas Avatar answered Sep 29 '22 11:09

Bryce Thomas