RPeti
RPeti

Reputation: 43

How to create user-accessible file on Android phone?

I am trying to save data from my app to a file, and than save this file on my PC. I've read many tutorials about file creation on external storage. Following this guides, i created the code below. The phone has no SD card, the external storage is on the internal memory. So there is my problem: the file exsist after using the "writeFunc", but it is empty. No any exception is thrown. I tryed this part with all of the existing flags, sadly with no result:

FileOutputStream fOut = openFileOutput( fileStruct.getName(), MODE_WORLD_READABLE);

The setReadable and setWritable functions return with "false", but no exception.

What should i do otherwise?

package com.track.fos;
//imports are okay

public class MainActivity extends AppCompatActivity {

public TextView textBox;
private File fileStruct;

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

    textBox = (TextView) findViewById(R.id.textView);
    textBox.append(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).toString());

    fileStruct = new File( Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM), "lufiTestFile.txt" );
}

public void writeFunc(View view) {

    textBox.append("\nFilestruct to string: " + fileStruct.toString());
    Log.d("External storage state", Environment.getExternalStorageState(fileStruct));

    try {
        boolean stateInfo = fileStruct.exists();
        textBox.append("\nexist: " + stateInfo);

        if( !stateInfo ){
            //stateInfo = fileStruct.mkdirs();
            //textBox.append("\nmake dirs: " + stateInfo);
            stateInfo = fileStruct.createNewFile();
            textBox.append("\ncreateNewFile: " + stateInfo);
        }

        stateInfo = fileStruct.setReadable( true , false);
        textBox.append("\nsetReadable: " + stateInfo);
        stateInfo = fileStruct.setWritable( true, false );
        textBox.append("\nsetWritable: " + stateInfo);

        FileOutputStream fOut = openFileOutput( fileStruct.getName(), MODE_WORLD_READABLE);
        fOut.write("Test text hopefuly inside the file.".getBytes());
        fOut.flush();
        fOut.close();
        textBox.append("\nFile written");
    } catch (Exception e) {
        e.printStackTrace();
        Log.d("File write throws",e.getCause().toString());
        textBox.append("\nFile write failed");
    }
}

public void readFunc(View view) {

    try {
        FileInputStream fin = openFileInput( fileStruct.getName() );
        int c;
        String temp = "";
        while ((c = fin.read()) != -1) {
            temp = temp + Character.toString((char) c);
        }
        textBox.append("\n" + temp);
    } catch (Exception e) {
        e.printStackTrace();
        textBox.append("\nFile read failed");
    }
}
}

Upvotes: 1

Views: 461

Answers (1)

CommonsWare
CommonsWare

Reputation: 1006644

FileOutputStream fOut = openFileOutput( fileStruct.getName(), MODE_WORLD_READABLE);

That will not open a FileOutputStream for a file on external storage. openFileOutput() writes to internal storage.

Replace that with:

FileOutputStream fOut = new FileOutputStream(fileStruct);

You will also need to ensure that the file gets indexed, so other apps and desktop OSes can see it.

Upvotes: 1

Related Questions