user1709803
user1709803

Reputation:

Create a file locally on android(not on SD card)

I am having an issue where I am trying to create a file locally on the android emulator however when I test it the file exists, it doesn't. I do not have access to a physical android device so am using the emulator. Please note I do not want to save the file on the SD card. I am not very familiar with android's file structure so forgive me if my code doesn't make sense. This is the code I am currently using and it doesn't work :(

EditText editText = (EditText) findViewById(R.id.editTextName);
        String  sName = editText.getText().toString();

        editText = (EditText) findViewById(R.id.editTextEmail);
        String  sEmail = editText.getText().toString();

        editText = (EditText) findViewById(R.id.editTextPostal);
        String  sPostal = editText.getText().toString();

File file = new File("/storage/new/test.txt");
        FileOutputStream fos;
        byte[] data = new String("Name: " + sName + " Subject: " + sEmail + " Question: " + sPostal ).getBytes();

        OutputStream myOutput;
            try {
            myOutput = new BufferedOutputStream(new FileOutputStream(file,true));
            myOutput.write(data);
            myOutput.flush();
            myOutput.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

        if(file.exists())
        {
            finish();
        }

Could anyone experienced enough in the area of android development provide me with some sample code or point me in the right direction so I can get this bad boy working? EDIT: When I say it doesn't work, I mean the file never gets created.

Upvotes: 0

Views: 182

Answers (1)

tishu
tishu

Reputation: 1008

If you are new to file storage in Android I suggest you read trough this piece of documentation to get started - it should answer your questions with examples.

Upvotes: 1

Related Questions