1

I want the user to choose some .txt file from device and then my app to store the copy of it inside its assets folder.

buttonChooseFile.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent txtIntent = new Intent(Intent.ACTION_GET_CONTENT);
            txtIntent.setType("text/plain");
            startActivityForResult(txtIntent, 1);
        }
    });

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == 1) {
        if (resultCode == RESULT_OK) {
            File file = new File(data.getData().getPath());
            chosenTxtFile = file;

        }
    }
}

When I run it I can indeed choose a file from my phone, but now when I want to execute the following code, nothing happens:

StringBuilder stringBuilder = new StringBuilder();
            try {
                BufferedReader br = new BufferedReader(new FileReader(chosenTxtFile));
                String line;

                while ((line = br.readLine()) != null) {
                    stringBuilder.append(line);
                    stringBuilder.append('\n');
                }
                br.close();
            }
            catch (IOException e) {
            }

            textView.setText(stringBuilder.toString());

I read here: Android: Reading txt file using implicit intent that I should use ContentResolver. However since I'm new to this, I'm not exactly sure how. Could anyone please help me write ContentResolver that would work with my code? Much appreciated!

Adam
  • 35
  • 3
  • 1
    You cannot store files in the assets 'folder' to begin with as all with assets is read only. – greenapps Mar 13 '18 at 13:43
  • `File file = new File(data.getData().getPath()); chosenTxtFile = file;` That will not work. Instead use `Uri uri = data.getData();`. Use the uri with the content resolver. You can read how to do that here every day. Every day people ask the same here. So take some time to read the pages tagged 'android' of the last week and you will see. AND... you cannot store files in assets. – greenapps Mar 13 '18 at 13:46
  • Thank you, noted. How should I permanently store my files then? Would SQLite database accept .txt files? – Adam Mar 13 '18 at 13:46
  • My files? What do you mean with my files? If you can let the user select them then they are already on the device. What is it that you want? – greenapps Mar 13 '18 at 13:47
  • To start with, you could add some logging to the ``catch (IOException e) { ... }``, maybe this will give you some hint about what's going wrong. – Lars Gendner Mar 13 '18 at 14:08

1 Answers1

1

I want the user to choose some .txt file from device and then my app to store the copy of it inside its assets folder.

First, assets are read-only at runtime. There is no "assets folder" on the device.

Second, your code assumes that ACTION_GET_CONTENT returns filesystem paths. That has not been the case for years.

Could anyone please help me write ContentResolver that would work with my code?

Step #1: Stop thinking in terms of files, as ACTION_GET_CONTENT can get content from many places, few of which involve files on the filesystem

Step #2: Use the Uri (data.getData()), not just a piece of it (data.getData().getPath())

Step #3: Pass that Uri to getContentResolver().openInputStream(), and use that and an InputStreamReader instead of a FileReader

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491