简体   繁体   English

无法在android上写入文件

[英]Writing a file on android is not possible

I am using the following code to write some stuff into an external file: 我正在使用以下代码将一些内容写入外部文件:

public class DiskWriter {

    public static String TAG = "DiskWriter";

    public static void writeStringToDisk(String jsonString) {
        File root = android.os.Environment.getExternalStorageDirectory();
        File dir = new File (root.getAbsolutePath() + "/JSONFolder");
        dir.mkdirs();
        DateFormat df = new SimpleDateFormat("yyyyMMdd-HHmmss.SSS");
        File file = new File( dir, "/json-" + df.format(new Date(System.currentTimeMillis())) + ".txt");
        Log.e(TAG, "Writing file to " + file.getAbsolutePath());
        try {
            file.createNewFile();
            FileOutputStream f = new FileOutputStream(file);
            PrintWriter pw = new PrintWriter(f);
            pw.println(jsonString);
            pw.flush();
            pw.close();
            f.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Also, im my Manifest I added the following permission: 另外,在清单中,我添加了以下权限:

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

I also tried several variations of this code. 我还尝试了此代码的几种变体。 However, it just never works. 但是,它永远都行不通。 This is the output: 这是输出:

E/DiskWriter: Writing file to /storage/emulated/0/JSONFolder/json-20190306-190419.289.txt
W/System.err: java.io.IOException: No such file or directory
W/System.err:     at java.io.UnixFileSystem.createFileExclusively0(Native Method)
W/System.err:     at java.io.UnixFileSystem.createFileExclusively(UnixFileSystem.java:281)
W/System.err:     at java.io.File.createNewFile(File.java:1345)
W/System.err:     at app.generic.logic.DiskWriter.writeStringToDisk(DiskWriter.java:28)
W/System.err:     at app.generic.logic.JSONParser.doInBackground(JSONParser.java:48)
W/System.err:     at app.generic.logic.JSONParser.doInBackground(JSONParser.java:36)
W/System.err:     at android.os.AsyncTask$2.call(AsyncTask.java:333)
W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:266)
W/System.err:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245)
W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
W/System.err:     at java.lang.Thread.run(Thread.java:764)

I am using Android 9 on my device (Oneplus 6T) and it is not rooted or anything like that. 我在设备(Oneplus 6T)上使用的是Android 9,但它没有root或类似的名称。

You need to request the permission to write on the external storage. 您需要请求写入外部存储的权限。 Just listing it in the manifest is not enough. 仅将其列出在清单中是不够的。

See the explanation of the permissions system here: https://developer.android.com/guide/topics/permissions/overview 请参阅此处的权限系统说明: https : //developer.android.com/guide/topics/permissions/overview

and for information about how to request permissions see https://developer.android.com/training/permissions/requesting.html 有关如何请求权限的信息,请参见https://developer.android.com/training/permissions/requesting.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM