0

I want to open a file in android using the following code:

 FileInputStream fis = openFileInput("examplelist.txt");
 InputStreamReader isr = new InputStreamReader(fis);
 BufferedReader bufferedReader = new BufferedReader(isr);
 StringBuilder sb = new StringBuilder();
 String line;
 while ((line = bufferedReader.readLine()) != null) {
     sb.append(line);
 }

but where to put the file examplelist.txt? I found some contradicting sources, like this and this? How to do it correctly?

Update:

I want to be able to replace this file at anytime after the app has been installed. So this file is NOT part of the source code or anything. It contains dynamical data...

I would have used a simple 'File Selector', so the user can navigate to the file anywhere on the android phone, but this looks terribly complicated and cumbersome. So, for now, I just want to open a file I can put somewhere on the phone in the most possible simple manner...

Community
  • 1
  • 1
Alex
  • 41,580
  • 88
  • 260
  • 469
  • Most apps I use seem to create a folder in the root of the internal card, then use that to contain any files they need. – Carcigenicate Jan 12 '17 at 20:29
  • And where is that root? – Alex Jan 12 '17 at 20:30
  • On my phone, it's in `storage/emulated/0`, but I think the API exposes a method that returns the internal and external card paths. – Carcigenicate Jan 12 '17 at 20:32
  • Another option if you want a "File Picker" http://stackoverflow.com/questions/8945531/pick-any-kind-file-via-an-intent-on-android – OneCricketeer Jan 12 '17 at 20:35
  • @cricket_007: That's an answer I refer to. I actually have no file `data/data/` ... – Alex Jan 12 '17 at 20:36
  • You are asking about just reading from the Android SD card(s), which are addressed in enough detail in the documentation. Whether you don't have a specific directory shouldn't matter, I don't think https://developer.android.com/guide/topics/data/data-storage.html – OneCricketeer Jan 12 '17 at 20:39
  • @cricket_007: The you can surely tell me at which my code is wrong! – Alex Jan 12 '17 at 20:40
  • I don't understand why you think it is wrong. `/data` is on the root partition, not a folder that you'd be able to see immediately on the device. – OneCricketeer Jan 12 '17 at 20:42
  • I put the file in the following directory: `/storage/emulated/0/Android/data/com.example.alexander.learn/files/examplelist.txt`. This is, to the best of my knowledge, as explained in the posts I have linked, the location I try to access in the code I have presented. However, an exception is thrown. The files does not to be present at the location tryied to be opened in the code given above. – Alex Jan 12 '17 at 20:45
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/133032/discussion-between-alex-and-cricket-007). – Alex Jan 12 '17 at 20:54
  • You could comment below on CommonsWare comprehensive answer for any clarification – OneCricketeer Jan 12 '17 at 21:12
  • @cricket_007: I don't understand the answer provided by CommonsWare. Also, it seems incomplete, describing just general suggestions (like 'google it'), which is no help at all! – Alex Jan 12 '17 at 21:18
  • That's why you have the ability to comment on it – OneCricketeer Jan 12 '17 at 21:19
  • @cricket_007: Yes you are right. I have added a comment – Alex Jan 12 '17 at 21:21

3 Answers3

0

In order to be able to access a file, you should place it in /app/src/main/assets/examplelist.txt. You can access this directory in the Project explorer in Android Studio.

To open the file from java code, you can create an InputStream object by calling getAssets().open("examplelist.txt")

Sman25
  • 1,605
  • 13
  • 17
0

One possible way to solve this problem is the following:

  1. You put the file in the kind-of-root folder, which might have the following path: /storage/emulated/0 (this is the location you can see when you connect the device e.g. to Linux and open it in the file explorer).

  2. You can open the content of the file using the following code:

    String path = Environment.getExternalStorageDirectory().toString();
    Log.d("Files", "Path: " + path);
    
    String joinedPath = new File(path, "examplelist.txt").toString();
    try {
        FileInputStream fis =new FileInputStream(new File(joinedPath));
        InputStreamReader isr = new InputStreamReader(fis);
        BufferedReader bufferedReader = new BufferedReader(isr);
        StringBuilder sb = new StringBuilder();
        String line;
        while ((line = bufferedReader.readLine()) != null) {
            sb.append(line);
        }
        Log.d("Text", sb.toString());
     } catch (IOException e) {
        Toast.makeText(getBaseContext(), "File does not exist", Toast.LENGTH_LONG).show();
     }
    
  3. VERY IMPORTANT, and which I forget EVERY single time: You have to set permission to the app to access the storage. You have to put the permission tag in the AndroidMainfest.xml

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        ...
    
Alex
  • 41,580
  • 88
  • 260
  • 469
-1

but where to put the file examplelist.txt?

Your app would create the file itself. openFileInput() is part of internal storage, to which ordinary users have no access.

I would have used a simple 'File Selector', so the user can navigate to the file anywhere on the android phone, but this looks terribly complicated and cumbersome.

Step #1: Call startActivityForResult() with an ACTION_OPEN_DOCUMENT or ACTION_GET_CONTENT Intent (the former is for Android 4.4+)

Step #2: In onActivityResult(), if you get RESULT_OK as a result, get the Uri to the user's chosen content via getData() on the Intent passed into onActivityResult()

Step #3: Call openInputStream() on a ContentResolver to get an InputStream to use for reading the content

It's about 10-15 lines of code over what you have in your question, plus the code that your question needs but does not show (e.g., background thread for I/O).

Or, use a file chooser library.

I just want to open a file I can put somewhere on the phone in the most possible simple manner...

Use external storage, such as getExternalFilesDir().

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • I dont understand this answer. Why should I use `Intents` and `RESULT_OK` items, when I just want to open a file? Example in python: `open("filename")`. I just want to do the same in android. – Alex Jan 12 '17 at 21:20
  • @Alex: "Why should I use Intents and RESULT_OK items, when I just want to open a file?" -- you wrote in your question "I would have used a simple 'File Selector'...". Those three steps are how you implement a simple file selector. If you did not want to implement a simple file selector, you should not have included that in your question. – CommonsWare Jan 12 '17 at 21:24
  • Maybe you are right in order to provide vague steps to use a file selector. But you did not provide example code; I also do not agree that this is 'simple'. Simple is something like this: `String filename = FileSelector();`, or something like this. But the way you have described does not look simple to me. But complex and way too complicated. Maybe there is no simple solution in android. – Alex Jan 12 '17 at 21:26