将文本文件保存到外部存储

时间:2018-04-02 19:48:54

标签: java android save external accelerometer

我正在尝试创建一个获取加速度计数据的应用程序,然后将其保存在设备上的文本文件中。但由于某种原因,它似乎并没有节省外部存储。它为文件创建文件夹,但不创建文件,也不处理任何数据。以下是我正在处理的方法我错过了什么明显的东西?

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
    }

}

1 个答案:

答案 0 :(得分:0)

  1. 向Manifest添加权限:

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

  2. 检查预先

    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!
             }
           }
    
  3. 处理回调

    @覆盖 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();
        }
     }
    

    }