Kevik
Kevik

Reputation: 9371

simple write file code works in java but not on android device

I tested this code and it works fine in java and writes a test file on my notebook computer, however can't get this to work on android device.

I put a note to show where the null pointer exception is.

When i run this on several different android devices the result is the same. It crashes with null pointer in the same line of code.

What would cause this to work with no errors on a java desktop application and get null pointer on an android device?

any ideas?

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        File makeDirectory = new File("/mnt/testDirectory/");

        // File makeDirectory = new File("C:/testDirectory/"); // when used on
        // java desktop app

        makeDirectory.mkdir();

        File file = new File("/mnt/testDirectory/testfile.txt");

        // File file = new File("C:/testDirectory/testfile.txt"); // when used
        // on java desktop app

        try {
            file.createNewFile();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

        PrintStream output = null;
        try {
            output = new PrintStream(new FileOutputStream(file));
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        output.print(true); // <----- NULL POINTER EXCEPTION THIS LINE
        output.print((int) 123);
        output.print((float) 123.456);

        output.printf(Locale.UK, "Text + data: %1$", 123);

        output.close();

    }
}

permissions from the manifest xml file

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

EDIT: I just found some additional information, so I am not afraid of using getExternalStorageDirectory() because if i am correct. all Android devices have some external storage directory on them, even if not SD card is used, example from the other question i found,

"Android will always report one mounted hardware memory (if any available) as External Storage.

That memory can be:

fitted inside the device by manufacturer (internal memory)
can be an sd card (external memory)

From this question:

Device claims external storage, yet I have no SD card

Upvotes: 0

Views: 184

Answers (2)

MMss
MMss

Reputation: 324

Ahem, '/mnt'? As per all the tutorials i've come across and also the android developers website; You need to use:

Environment.getExternalStorageDirectory() method which returns a File object containing a reference to the root storage directory. From there on; if you're a good java programmer I think you can handle the rest ;)

Upvotes: 1

hasan
hasan

Reputation: 24195

This how you get a file from external storage:

File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath(), filename);

Note:

may be if your device is rooted. mnt could work. I have no idea. but, it is a wrong approach.

Upvotes: 1

Related Questions