I am trying to create a .txt file through my app in android studio. SDK version 10, my emulator is galaxy S7 (I am not sure if this matters at all).
我在android studio中尝试通过我的app创建一个。txt文件。SDK版本10,我的模拟器是galaxy S7(我不确定这是否重要)。
I have written the following class and it keeps on giving me file no exception error. I have googled a lot and searched this forum but none of them is working.
我已经写了下面的类,并且它一直给我文件,没有例外错误。我用谷歌搜索了很多次,搜索了这个论坛,但是没有一个有效。
Please help.
请帮助。
My Class..
我的课. .
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");
}
It keeps on giving following error:
它不断给出如下错误:
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
2 个解决方案
#1
1
You have to check the permission from runtime. If you want to just test your code right away, go to device's settings, apps, your_app, then choose permissions from there. Allow the permissions. And you're good to go
您必须检查运行时的权限。如果你想马上测试你的代码,去设备的设置,应用程序,your_app,然后从那里选择权限。允许的权限。你可以走了
#2
0
Make sure you have the WRITE_EXTERNAL_STORAGE permission, including requesting it at runtime. –
确保您拥有WRITE_EXTERNAL_STORAGE权限,包括在运行时请求它。- - - - - -
#1
1
You have to check the permission from runtime. If you want to just test your code right away, go to device's settings, apps, your_app, then choose permissions from there. Allow the permissions. And you're good to go
您必须检查运行时的权限。如果你想马上测试你的代码,去设备的设置,应用程序,your_app,然后从那里选择权限。允许的权限。你可以走了
#2
0
Make sure you have the WRITE_EXTERNAL_STORAGE permission, including requesting it at runtime. –
确保您拥有WRITE_EXTERNAL_STORAGE权限,包括在运行时请求它。- - - - - -