我有这个看似简单的代码
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 上。
最佳答案
将此权限添加到您的 AndroidManifest.xml
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
当targetSdkVersion为23或以上(Android 6)时,将其放在AndroidManifest.xml中是不够的,您必须在运行时请求权限。
关于android - ContentResolver openInputStream() 在现有文件上抛出 FileNotFound,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36829963/