我将为您翻译以下内容:
它一直给出以下错误:
我正在尝试通过我的Android Studio应用程序创建一个.txt文件。 SDK版本10,我的模拟器是Galaxy S7(我不确定这是否有任何影响)。
我编写了以下类,但它一直给我文件无异常错误。我已经搜索了很多谷歌和论坛,但没有一个有效的解决方法。
请帮帮我。
我的类..
public static void StoreData(String input) throws IOException {
File Root = Environment.getExternalStorageDirectory();
Log.d("hello",Root.getAbsolutePath());
File Dir = new File(Root.getAbsoluteFile() + "/testfolder");
if (!Dir.exists()) {
Log.d("hello","doesn't exists "+Root.getAbsolutePath());
Dir.mkdir();
}
File file = new File(Dir, "Contacts_Google_Sheets.txt");
FileOutputStream fileoutputstream = new FileOutputStream(file);
fileoutputstream.write(input.getBytes());
fileoutputstream.close();
Log.d("hello", "writing complete");
}
它一直给出以下错误:
W/System.err: java.io.FileNotFoundException: /storage/emulated/0/testfolder/Contacts_Google_Sheets.txt (No such file or directory)
W/System.err: at java.io.FileOutputStream.open(Native Method)
W/System.err: at java.io.FileOutputStream.<init>(FileOutputStream.java:221)
W/System.err: at java.io.FileOutputStream.<init>(FileOutputStream.java:169)
W/System.err: at com.therock.app3_contact_list_googlesheet_public.FileOperations.StoreData(FileOperations.java:55)
W/System.err: at com.therock.app3_contact_list_googlesheet_public.GetDataAsyncTask.doInBackground(GetDataAsyncTask.java:64)
W/System.err: at com.therock.app3_contact_list_googlesheet_public.GetDataAsyncTask.doInBackground(GetDataAsyncTask.java:33)
W/System.err: at android.os.AsyncTask$2.call(AsyncTask.java:305)
W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:237)
W/System.err: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243)
W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
W/System.err: at java.lang.Thread.run(Thread.java:761)
E/EGL_emulation: tid 3410: eglSurfaceAttrib(1174): error 0x3009 (EGL_BAD_MATCH)
W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0x8fe424c0, error=EGL_BAD_MATCH
WRITE_EXTERNAL_STORAGE
权限,包括在运行时请求它。 - CommonsWareDir.mkdir();
。检查返回值,如果无法创建目录,则返回false。在这种情况下,向用户显示一个toast,告诉他们无法创建目录。然后返回。因为在不存在的目录中尝试创建文件没有意义。 - greenapps