saving file in internal storage android
I'm not too sure which example you are referring to but I have two working samples here of which at least one of them should suit your needs.I tested these on an X10 running Build number 2.1.A.0.435, one Xperia T running Build number 7.0.A.1.303 and one Nexus S running Build number JZO54K
Example 1
String filename = "myfile"; String outputString = "Hello world!"; try { FileOutputStream outputStream = openFileOutput(filename, Context.MODE_PRIVATE); outputStream.write(outputString.getBytes()); outputStream.close(); } catch (Exception e) { e.printStackTrace(); } try { FileInputStream inputStream = openFileInput(filename); BufferedReader r = new BufferedReader(new InputStreamReader(inputStream)); StringBuilder total = new StringBuilder(); String line; while ((line = r.readLine()) != null) { total.append(line); } r.close(); inputStream.close(); Log.d("File", "File contents: " + total); } catch (Exception e) { e.printStackTrace(); }
Example 2
String filename = "mysecondfile"; String outputString = "Hello world!"; File myDir = getFilesDir(); try { File secondFile = new File(myDir + "/text/", filename); if (secondFile.getParentFile().mkdirs()) { secondFile.createNewFile(); FileOutputStream fos = new FileOutputStream(secondFile); fos.write(outputString.getBytes()); fos.flush(); fos.close(); } } catch (Exception e) { e.printStackTrace(); } try { File secondInputFile = new File(myDir + "/text/", filename); InputStream secondInputStream = new BufferedInputStream(new FileInputStream(secondInputFile)); BufferedReader r = new BufferedReader(new InputStreamReader(secondInputStream)); StringBuilder total = new StringBuilder(); String line; while ((line = r.readLine()) != null) { total.append(line); } r.close(); secondInputStream.close(); Log.d("File", "File contents: " + total); } catch (Exception e) { e.printStackTrace(); }
I had "Permission denied" issue with Samsung Galaxy S7 even though I had given read and write permissions in manifest file. I solved it by going to phone settings >> application >> [my app] and allowed "Storage" under permission. Worked fine after that.