0

How to store files in Internal storage of android and how to access them in android phone. I googled a lot and found that we can store file but that can be accessed by only emulator or phone must be rooted.

What is solution for unrooted phones?

And how to access the internal storage files by giving its path? what will be the path?

Kindly help. Thanks in advance and examples will be highly appreciated.

CodingLover
  • 47
  • 2
  • 11

2 Answers2

0

Just use a file explorer app. Most phones come with one pre-installed. If you don't have one, however, you can always download one of your choice. I use ES File explorer.

From then it a matter of navigating to the file you want to access.

Hope that answers your question.

  • I want to make my own file explorer... for that purpose I want to save the files in internal storage of application and then retrieve to show that file. – CodingLover Jun 15 '17 at 10:23
0

You can do this by FileOutput stream:

    String fileName = "file.txt";
    String content = "hello world";

    FileOutputStream outputStream = null;
    try {
       outputStream = openFileOutput(fileName, Context.MODE_PRIVATE);
       outputStream.write(content.getBytes());
       outputStream.close();
} catch (Exception e) {
    e.printStackTrace();
}

Note: This is the method to write into the internal storage only, MODE_PRIVATE means other apps can't access your file. You also need to grant WRITE_EXTRENAL_STORAGE permission in the manifest xml.

This will store the file in: data/data/app/files/file.txt

To get a list of files saved in storage,

File dirFiles = mContext.getFilesDir();
for (String fname: dirFiles.list())
{
   Toast.makeText(mContext,fname,Toast.LENGTH_LONG).show();
}
  • It stores file in data/data//files.. How to I retrieve them, when I want to show them in listview ? and does it work for phones that are not rooted ? – CodingLover Jun 15 '17 at 10:27
  • dude, are you serious, with this sample code you can retrieve the file easily, StackOverflow is not a free coding service. – Maxime Liege Jun 15 '17 at 10:30
  • Check the edit, you can get a list of files stored, you can then show them in a listview. None of this requires a rooted phone – Shubham Singhal Jun 15 '17 at 10:32
  • @ShubhamSinghal Thanks very much. It is successfully working on android emulator, but not working on android phone... Any Idea ? – CodingLover Jun 15 '17 at 10:49
  • On android versions above 6.0 you need to ask for write permissions. Try this: https://stackoverflow.com/questions/33139754/android-6-0-marshmallow-cannot-write-to-sd-card – Shubham Singhal Jun 15 '17 at 10:55