ContentResolver openInputStream()在现有文件上抛出FileNotFound

时间:2016-04-24 23:12:35

标签: android android-contentresolver

我有这个看似简单的代码

File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
File testfile = new File(path,"image.jpg");

Log.d(TAG,"testfile: " + testfile + " exists: " + testfile.exists());

Uri uri = Uri.fromFile(testfile);
ContentResolver cr = context.getContentResolver();
InputStream istr = cr.openInputStream(uri);

输出结果为:

testfile: /storage/emulated/0/Pictures/image.jpg exists: true

uri值为file:///storage/emulated/0/Pictures/image.jpg。看起来像是同一个文件,但是openInputStream()会抛出一个FileNotFoundException。

知道为什么吗?这是在Android 6.0.1上。

1 个答案:

答案 0 :(得分:1)

将此权限添加到a

AndroidManifest.xml

当targetSdkVersion为23或更高(Android 6)时,将其放入AndroidManifest.xml是不够的,您必须在运行时请求权限。

相关问题