0

I received a file from an intent like so:

Uri incoming = getIntent().getParcelableExtra(Intent.EXTRA_STREAM);
File toStream = new File(incoming.getPath());

I cannot read from it, and toStream.canRead() returns false.

I received this from the share button in the Gallery app, and other apps seem to be receiving this file just fine. Any ideas?

rgrasell
  • 99
  • 2
  • 11
  • getIntent().getExtras().getParcelable(Intent.EXTRA_STREAM); – KOTIOS Aug 02 '13 at 07:51
  • Where do you have the code - toStream.canRead(), because in the doc here "http://developer.android.com/reference/java/io/File.html#canRead()" it's said - "Indicates whether the current context is allowed to read from this file." so the context could be the problem. – g00dy Aug 02 '13 at 07:53
  • Stacks28's code does the same thing as mine as far as I can tell. Same results. – rgrasell Aug 02 '13 at 07:55
  • I read about that too, but what could the context change? I have the read external storage permission set in the manifest. – rgrasell Aug 02 '13 at 07:56
  • goody: I have the .canRead() code running inside the onCreate() of a FragmentActivity. – rgrasell Aug 02 '13 at 08:10

3 Answers3

1

There is a new issue in SDK 23+ with permissions, which can cause your read/write to fail even though you fixed it in the manifest. It is discussed under permissions even though you first encounter it when you try read/write. Link to thread where it is discussed

Community
  • 1
  • 1
darai
  • 11
  • 1
1

I had same problem with Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);. The path which was returned, was something like "/document/primary:Download/a.c" . When I create File object with that path, it was showing exists() and canRead() as false. What I did was, took the path after ":", that is "Download/a.c" and merged that with "/storage/emulated/0/". So final path is "/storage/emulated/0/Download/a.c" . Worked fine with internal storage.

In case of only permission problem make use of this.

ActivityCompat.requestPermissions(MainActivity.this, new String[]{READ_EXTERNAL_STORAGE, WRITE_EXTERNAL_STORAGE},THIS_REQUEST_CODE);

and override this

public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults)
{
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
} 
YakovL
  • 7,557
  • 12
  • 62
  • 102
0

try adding permission to your app READ_EXTERNAL_STORAGE

bongo
  • 733
  • 4
  • 12