0

The following method is called when I click the save button. However, the data is still been stored in my internal storage and not the sd card(external storage). Am I using the wrong permissions, methods?

public void save(View v)
{
    if(isExternalStorageWriteable() && checkPermission(andriod.Manifest.permission.WRITE_EXTERNAL_STORAGE)){
        
        File textfile = new File(Environment.getExternalStorageDirectory(), "Device 1 ");

        try{
            FileOutputStream fos = new FileOutputStream(textfile);
            fos.write(epc.toString().getBytes());
            fos.close();
            Log.i("State","2");
            Toast.makeText(this, "File saved.", Toast.LENGTH_LONG).show();
        }
        catch (IOException e)
        {
            e.printStackTrace();
            Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
        }
    }
    else
    {
        Log.i("State","4");
        Toast.makeText(this, "File not saved.", Toast.LENGTH_LONG).show();
    }
}

private boolean isExternalStorageWriteable()
{
    if(Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())){
        Log.i("State","Yes, it is writeable!");
        return true;
    }

    else{
        return false;
    }
}

public boolean checkPermission(String permission){
    int check = ContextCompat.checkSelfPermission(this,permission);
    return (check == PackageManager.PERMISSION_GRANTED);
}

Android Manifest

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
  • `getExternalStorageDirectory()` returns the path to the "sdcard" section. At the beginning an Android this section was located on the sdcard. Nowadays it is just the `/storage/emulated/0/`aka. `/sdcard` section. See https://stackoverflow.com/a/38760040/150978 – Robert Jul 22 '20 at 15:09
  • You can write to your app specific directory on removable micro SD card. Have a look at the second item returned by getExtenalFilesDirs(). You do not need any permission for it. – blackapps Jul 22 '20 at 15:12

0 Answers0