0

I'm trying to parse a simple xls File from my device, here is the code i wrote until now, but I can't figure it out why my file is not found by my app .

here the code i wrote until now

In the resultActivity:

File sdcard = Environment.getExternalStorageDirectory();
                    File file = new File(sdcard, data.getData().getPath());
                    ParseFileAggiornamentoAsyncTask asyncTaskLetturaFile = new ParseFileAggiornamentoAsyncTask(MainActivity.this,file);
                    asyncTaskLetturaFile.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);

except other things, in the asynctask there is a control like file.exist() which return always false, so i'm not able to read it and other things.

What i'm doing wrong?

UPDATE FROM COMMENTS

here is the code that trigger my startActivityForResult

if (checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
                requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
                        MainConstant.MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE);
            }else {
                Intent fileintent = new Intent(Intent.ACTION_GET_CONTENT);
                fileintent.setType("*/*");
                fileintent.addCategory(Intent.CATEGORY_OPENABLE);
                try {
                    startActivityForResult(Intent.createChooser(fileintent, "Select a File to Upload"), PICKFILE_RESULT_CODE);
                } catch (ActivityNotFoundException e) {
                    Toast.makeText(MainActivity.this, MainActivity.this.getString(R.string.generic_error), Toast.LENGTH_LONG).show();
                    Log.e("tag", "No activity can handle picking a file. Showing alternatives.");
                }
                return true;
            }
Anon
  • 502
  • 5
  • 19
  • you have the `Uri uri = data.getData()` - use it for getting the `InputStream` - for more read `ContentResolver` documentation – pskink May 05 '19 at 10:25
  • Where *exactly* is `data` coming from? How did you get this value? If this is from `onActivityResult()`, what is the `startActivityForResult()` that triggered it? – CommonsWare May 05 '19 at 10:56
  • @CommonsWare details in update – Anon May 05 '19 at 11:22
  • A `Uri` is not a file. Use `ContentResolver` and methods like `openInputStream()` to work with the `Uri`. – CommonsWare May 05 '19 at 11:30

1 Answers1

0

Probably manifest associated, see https://developer.android.com/training/data-storage/files

You probably need the following lines in your Manifest

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

If you already have those then working with a file is the same as in other posts.

See How do I check if a file exists in Java?

Jay
  • 3,276
  • 1
  • 28
  • 38
  • i do have these lines in my manifest, and i let my user decide to have or not my app access external storage, but still the path returned in my data.getData().getPath() seems to be not the real path of the file – Anon May 05 '19 at 10:44