Android 7.1 写入文本文件

Android 7.1 Write to text file

来自 Jelly bean 的牛轧糖新手试图向 sdcard 写入一个文本文件我知道我现在必须请求权限但找不到任何有效的代码

尝试了以下

        StringBuilder bodyStr=new StringBuilder();
                 bodyStr.append(data1Str.toString()).append(",").append(data2Str.toString()).append(",").append(data3Str.toString()).append(",").append(data4Str.toString()).append(",").append(data5Str.toString()).append(",").append(data22Str.toString()).append(",").append(data23Str.toString()).append(lineSep);; 
             String bodytextStr=bodyStr.toString();

                boolean hasPermission = (ContextCompat.checkSelfPermission(data_entry.this,
                        Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED);
                if (!hasPermission) {
                    ActivityCompat.requestPermissions(data_entry.this,
                            new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                            REQUEST_WRITE_STORAGE);
                }


                try {

                    File myFile = new File(fileName);
                    myFile.createNewFile();
                    FileOutputStream fOut = new FileOutputStream(myFile);
                    OutputStreamWriter myOutWriter =
                            new OutputStreamWriter(fOut);
                    myOutWriter.append(bodytextStr);
                    myOutWriter.close();
                    fOut.close();
                    Toast.makeText(getBaseContext(),
                            "Done writing SD 'mysdfile.txt'",
                            Toast.LENGTH_SHORT).show();
                } catch (Exception e) {
                    Toast.makeText(getBaseContext(), e.getMessage(),
                            Toast.LENGTH_SHORT).show();
                }

回来[权限被拒绝]

已在清单中设置常用权限

任何我出错的想法

感谢任何帮助

马克

这是您需要的:https://developer.android.com/training/articles/scoped-directory-access.html#accessing

您必须要求用户授予访问权限。

要么你有权限并且可以在写入文件的地方执行 try catch 块,要么你必须在 onRequestPermissionsResult 中执行。

类似于:

public void yourMethod(){
    boolean hasPermission = (ContextCompat.checkSelfPermission(data_entry.this,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED);
    if(hasPermission){
      // write
    }else{
      // ask the permission
      ActivityCompat.requestPermissions(data_entry.this,
                        new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                        REQUEST_WRITE_STORAGE);
      // You have to put nothing here (you can't write here since you don't
      // have the permission yet and requestPermissions is called asynchronously)
   }
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
                                       @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    // The result of the popup opened with the requestPermissions() method
    // is in that method, you need to check that your application comes here
    if (requestCode == REQUEST_WRITE_STORAGE) {
        if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            // write
        }
    }
}

我建议你检查这个 link : https://developer.android.com/training/permissions/requesting.html