How to store file in Internal storage in android and access them from android phone

时间:2017-06-15 10:03:10

标签: java android file android-internal-storage

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.

2 个答案:

答案 0 :(得分: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.

答案 1 :(得分: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();
}