Android write to file - cannot find file

I am new to Android development. I am trying to write something to a file. I am using the code:

try { FileOutputStream fOut = openFileOutput( "out.txt", MODE_WORLD_READABLE); OutputStreamWriter osw = new OutputStreamWriter(fOut); osw.write("something"); osw.flush(); osw.close(); fOut.close(); } catch (MalformedURLException e) { Log.e("FILE WRITE ERROR", e.getMessage()); } catch (IOException e) { Log.e("FILE WRITE ERROR", e.getMessage()); } 

Everything is fine, but I can not find the file in the DDMS FIle Explorer. Could you help me and say where Android saves the file? This is not in "/ data / data / my_project_package". Thanks.

+4
source share
3 answers

1) be sure to indicate this in your manifest:

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

2) It matters, be it testing with an emulator or device. In case of the latter, be sure to disconnect the device from the PC (select: "Charging only"). Debugging will still work that way. It is simply that you cannot write and watch at the same time.

This method works:

 public void writeToExternalStoragePublic(String filename, int content) { String packageName = this.getPackageName(); String path = Environment.getExternalStorageDirectory().getAbsolutePath()+ "/Android/data/" + packageName + "/files/"; if (isExternalStorageAvailable() && !isExternalStorageReadOnly()) { try { boolean exists = (new File(path)).exists(); if (!exists) { new File(path).mkdirs(); } // Open output stream FileOutputStream fOut = new FileOutputStream(path + filename,true); // write integers as separated ascii's fOut.write((Integer.valueOf(content).toString() + " ").getBytes()); fOut.write((Integer.valueOf(content).toString() + " ").getBytes()); // Close output stream fOut.flush(); fOut.close(); } catch (IOException e) { e.printStackTrace(); } } } 
+5
source

There seems to be a lot of questions in this thread, and the problem often boils down to the fact that I am not asking for permission in AndroidManifest.xml ii). A device storage mounted on a computer while the application was running or iii) emulator settings.

In the hope that others who have tried all these solutions and failed, I imagine iv) the computer decides to arbitrarily ignore your permission settings in AndroidManifest.xml after pulling the code from the original repository written on another computer. In my case, I just wrote the code on computer A, which I wrote on external storage. I checked the specified code on the source control and then pulled the mentioned code to computer B. When I reinstalled the application from computer B to the device, I started to get an exception that was excluded from the permission.

Bugfix: make any changes to the AndroidManifest.xml file on computer B (for example, insert a new line). This posed a problem for me. I do not know why. This is probably one of the rarest cases, but more disappointing.

+1
source

Put <uses-permission android:name="android.permission.WRITE_MEDIA_STORAGE"> in the manifest.

Here is a link to it.

+1
source

Source: https://habr.com/ru/post/1316531/


All Articles