Android 7.1写入文本文件 [英] Android 7.1 Write to text file

查看:109
本文介绍了Android 7.1写入文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

牛轧糖的新手来自果冻豆尝试写入sdcard文本文件我知道我现在必须请求权限,但找不到任何有效的代码

Guys New to Nougat come from Jelly bean Trying to write to sdcard a text file I know I now have to request permissions but cant find any code that works

尝试了以下

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

回来[权限被拒绝]

已在清单中设置了常规权限

have set usual permissions in manifest

我要去哪里的任何想法

任何帮助都感激

标记

推荐答案

您要么拥有权限,要么可以在尝试写入文件的位置执行try catch块,或者必须在onRequestPermissionsResult中进行操作.

Either you have the permission and can do the try catch block where you write into the file or you have to do it in the 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
        }
    }
}

我建议您检查以下链接: https://developer.android.com /training/permissions/requesting.html

I suggest that you check this link : https://developer.android.com/training/permissions/requesting.html

这篇关于Android 7.1写入文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆