简体   繁体   中英

Android canRead() returns false

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?

I had same problem with Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT); . The path which was returned, was something like " /document/primary:Download/ac " . 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/ac " and merged that with " /storage/emulated/0/ ". So final path is " /storage/emulated/0/Download/ac " . 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);
} 

尝试为您的应用添加权限READ_EXTERNAL_STORAGE

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM