Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: how to write a file to internal storage

I am developing a simple android application and I need to write a text file in internal storage device. I know there are a lot of questions (and answers) about this matter but I really cannot understand what I am doing in the wrong way.

This is the piece of code I use in my activity in order to write the file:

public void writeAFile(){     String fileName = "myFile.txt";     String textToWrite = "This is some text!";     FileOutputStream outputStream;     try {       outputStream = openFileOutput(fileName , Context.MODE_PRIVATE);       outputStream.write(textToWrite.getBytes());       outputStream.close();    } catch (Exception e) {      e.printStackTrace();    } } 

I really cannot understand which mistake I am doing. In addition, I have tried this project on my emulator in Android Studio and my phone in order to understand where I am doing something wrong but even with that project no file is written neither on the phone or on the emulator.

EDIT: I know that no file is written to my internal storage because I try to read the content of the file, after I have written to it, with this code:

public void ReadBtn(View v) {     //reading text from file     try {         FileInputStream fileIn=openFileInput("myFile.txt");         InputStreamReader InputRead= new InputStreamReader(fileIn);          char[] inputBuffer= new char[READ_BLOCK_SIZE];         String s="";         int charRead;          while ((charRead=InputRead.read(inputBuffer))>0) {             String readstring=String.copyValueOf(inputBuffer,0,charRead);             s +=readstring;         }         InputRead.close();         textmsg.setText(s);     } catch (Exception e) {         e.printStackTrace();     } } 

Nothing is shown at all.

like image 883
delca85 Avatar asked Jun 16 '17 10:06

delca85


People also ask

How do I save files to my internal storage?

openFileOutput(): This method is used to create and save a file. Its syntax is given below: FileOutputStream fOut = openFileOutput("file name",Context. MODE_PRIVATE);

How do I use internal storage on Android?

Just open it up to browse any area of your local storage or a connected Drive account; you can either use the file type icons at the top of the screen or, if you want to look folder by folder, tap the three-dot menu icon in the upper-right corner and select "Show internal storage" — then tap the three-line menu icon in ...


2 Answers

Use the below code to write a file to internal storage:

public void writeFileOnInternalStorage(Context mcoContext, String sFileName, String sBody){           File dir = new File(mcoContext.getFilesDir(), "mydir");     if(!dir.exists()){         dir.mkdir();     }      try {         File gpxfile = new File(dir, sFileName);         FileWriter writer = new FileWriter(gpxfile);         writer.append(sBody);         writer.flush();         writer.close();     } catch (Exception e){         e.printStackTrace();     } } 

Starting in API 19, you must ask for permission to write to storage.

You can add read and write permissions by adding the following code to AndroidManifest.xml:

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

You can prompt the user for read/write permissions using:

requestPermissions(new String[]{WRITE_EXTERNAL_STORAGE,READ_EXTERNAL_STORAGE}, 1); 

and then you can handle the result of the permission request in onRequestPermissionsResult() inside activity called from it.

like image 86
Sandeep dhiman Avatar answered Sep 27 '22 17:09

Sandeep dhiman


no file is written neither on the phone or on the emulator.

Yes, there is. It is written to what the Android SDK refers to as internal storage. This is not what you as a user consider to be "internal storage", and you as a user cannot see what is in internal storage on a device (unless it is rooted).

If you want to write a file to where users can see it, use external storage.

This sort of basic Android development topic is covered in any decent book on Android app development.

like image 40
CommonsWare Avatar answered Sep 27 '22 17:09

CommonsWare