How to save data to the internal memory of an Android device?

Asked

Viewed 12,503 times

3

I am writing files in the external memory of the device, I am doing it as follows:

File arquivo = new File(Environment.getExternalStorageDirectory()+"/"+ nomeArquivo.getText().toString() + ".txt");

I am writing the file in the external memory of the right mobile? So if the mobile doesn’t have memory card I won’t be able to save my data. What to do in this case to save the data in the device’s internal memory?

2 answers

8


First of all, it’s worth a read in the official documentation of Android on How to Save Files.

To save in internal memory, we use the method openFileOutput() passing as parameters the file name and the mode/permission.

Permissions can be private and public.

FileOutputStream fOut = openFileOutput("nome do arquivo",MODE_WORLD_READABLE)

The méotodo openFileOutput() returns an instance of Fileoutputstream, soon, you receive a Fileinputstream object. After that, you can call the writing métpdp to write the data into the file:

String str = "data";
fOut.write(str.getBytes());
fOut.close();

To read the file:

FileInputStream fin = openFileInput('nome do arquivo');

Example class:

package com.example.storage;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {

   private EditText et;
   private String data;
   private String file = "arquivo";
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      et = (EditText)(findViewById(R.id.editText1));

   }

   public void save(View view){
      data = et.getText().toString();
      try {
         FileOutputStream fOut = openFileOutput(file,MODE_WORLD_READABLE);
         fOut.write(data.getBytes());
         fOut.close();
         Toast.makeText(getBaseContext(),"file saved",
         Toast.LENGTH_SHORT).show();
      } catch (Exception e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      }
   }
   public void read(View view){
      try{
         FileInputStream fin = openFileInput(file);
         int c;
         String temp="";
         while( (c = fin.read()) != -1){
            temp = temp + Character.toString((char)c);
         }
         et.setText(temp);
         Toast.makeText(getBaseContext(),"file read",
         Toast.LENGTH_SHORT).show();

      }catch(Exception e){

      }
   }
   @Override
   public boolean onCreateOptionsMenu(Menu menu) {
      // Inflate the menu; this adds items to the action bar if it is present.
      getMenuInflater().inflate(R.menu.main, menu);
      return true;
   }

}
  • 3

    The absolute path of a file in the internal memory can be given by: String caminhoAbsolutoDoArquivo = getApplicationContext().getFilesDir().getPath() + "/" + nomeDoArquivo;. Then you use for example in: FileOutputStream fileOut = new FileOutputStream(caminhoAbsolutoDoArquivo);

  • Okay, thank you Piovezan!

  • 1

    Victor Mendonça worked right here! Thank you!

1

  • Ah yes! I understand Clebao, thank you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.