0

I am trying to create an application that takes the accelerometer data and then save this within a text file on the device. But for some reason, it doesn't seem to be saving in external storage. It creates the folder for the file but then does not create the file nor process any of the data. Below is the method that I am working on am I missing anything obvious?

public void writeFileExternalStorage() {
    String state = Environment.getExternalStorageState();
    //external storage availability check
    if (!Environment.MEDIA_MOUNTED.equals(state)) {
        Toast.makeText(this,"NO MEDIA MOUNT",Toast.LENGTH_LONG).show();
    }
    File docsFolder = new File(Environment.getExternalStorageDirectory() + "/Documentaly");
    boolean isPresent = true;
    if (!docsFolder.exists()) {
        isPresent = docsFolder.mkdir();
        Toast.makeText(this,"Made Dir",Toast.LENGTH_SHORT).show();
    }
    if (isPresent) {
        File file = new File(docsFolder.getAbsolutePath(),"dataCollection.txt");
        Toast.makeText(this,"Made file",Toast.LENGTH_SHORT).show();
        FileOutputStream outputStream = null;
        try {
            //file.createNewFile();
            //second argument of FileOutputStream constructor indicates whether to append or create new file if one exists
            outputStream = new FileOutputStream(file, true);
            outputStream.write(entryData.getBytes());
            outputStream.flush();
            outputStream.close();
            Toast.makeText(this,"success",Toast.LENGTH_SHORT).show();
        } catch  (Exception e) {
            //Toast.makeText(this,"failure",Toast.LENGTH_SHORT).show();
            e.printStackTrace();
        }
    } else {
        // Failure
    }

}
  • https://stackoverflow.com/questions/32789157/how-to-write-files-to-external-public-storage-in-android-so-that-they-are-visibl – CommonsWare Apr 02 '18 at 19:56
  • `isPresent = docsFolder.mkdir(); Toast.makeText(this,"Made Dir",Toast.LENGTH_SHORT).show();`. Wrong code. You should only call that Toast if isPresent==true of course. And if it is false you should return. – greenapps Apr 02 '18 at 21:01

1 Answers1

0
  1. Add permission into Manifest:

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

  2. Check premission


      public static final int REQUEST_WRITE_STORAGE = 112; 

      private requestPermission(Activity context) {
      boolean hasPermission = (ContextCompat.checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED);
          if (!hasPermission) {
             ActivityCompat.requestPermissions(context,
                new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
              REQUEST_WRITE_STORAGE);
          } else {
            // You are allowed to write external storage:
            String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/new_folder";
            File storageDir = new File(path);
            if (!storageDir.exists() && !storageDir.mkdirs()) {
              // This should never happen - log handled exception!
            }
          }
  1. Handle callback

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);

    switch (requestCode)
    {
     case Preferences.REQUEST_WRITE_STORAGE: {
        if (grantResults.length > 0 && grantResults[0] ==  PackageManager.PERMISSION_GRANTED) {
          Toast.makeText(this, "The app was allowed to write to your storage!", Toast.LENGTH_LONG).show();
          // Reload the activity with permission granted or use the features what required the permission
        } else {
          Toast.makeText(this, "The app was not allowed to write to your storage. Hence, it cannot function properly. Please consider granting it this permission", Toast.LENGTH_LONG).show();
       }
    }
}