How to read a file from the phone’s internal memory in Android?

I downloaded the file from HttpConnectionusing FileOutputStreamin android and now it is being written to the phone’s internal memory along the way since I found it inFile Explorer

/data/data/com.example.packagename/files/123.ics

Now I want to open and read the contents of the file from the phone’s internal memory to the user interface. I tried to do this with FileInputStream, I just gave the name of the file with the extension to open it, but I'm not sure how to specify the path to the file in the internal memory, as it causes the application to close.

Any suggestions?


This is what I do:

try
{               
  FileInputStream fileIn;       
  fileIn = openFileInput("123.ics");
  InputStream in = null;
  EditText Userid = (EditText) findViewById(R.id.user_id);
  byte[] buffer = new byte[1024];
  int len = 0;
  while ( (len = in.read(buffer)) > 0 )         
  {     
     Userid.setText(fileIn.read(buffer, 0, len));
  }                    
  fileIn.close();                       
} catch (FileNotFoundException e)
{
   e.printStackTrace();
}
catch (IOException e)
{   
    e.printStackTrace();
}
+3
source share
3 answers
String filePath = context.getFilesDir().getAbsolutePath();//returns current directory.
File file = new File(filePath, fileName);

Similar post here 

read file from phone memory

+6
source

, , com.example.packagename, openFileInput("123.ics"); FileInputStream , .

getFilesDir(), File, /data/data/com.example.packagename/files, .

+4

I use this code to open a file in internal storage. I think I could help.

File str = new File("/data/data/com.xlabz.FlagTest/files/","hello_file.xml");
-2
source

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


All Articles