第一次發(fā)博客
這個問題是我在制作一款app時發(fā)現(xiàn)的,我本來想在一個textview中加入一個圖片
String picturePath = "your.jpg";
Bitmap bitmap = BitmapFactory.decodeFile(picturePath);
Drawable d = new BitmapDrawable(getResources(), bitmap);
textview.setCompoundDrawablesWithIntrinsicBounds(null, d, null, null);
但是總是報錯
E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: /storage/emulated/0/DCIM/Camera/IMG_20230129_123518.jpg: open failed: EACCES (Permission denied)
我在瀏覽了大多數(shù)關(guān)于這個問題的討論和官方文檔之后,他們無非就是給了這幾種辦法:
1.在Manfest.xml中添加這個
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE"/>
結(jié)論:按你的需求添加讀寫權(quán)限,但manage是必須的
2.“在你的xml文件中添加一句話,所有問題都解決了”
android:requestLegacyExternalStorage="true"
這句話本身是片面的,如果這個應(yīng)用所在系統(tǒng)是在android10及以下,那么你加上這句話的確能解決問題,在android10以后,系統(tǒng)加了新的分區(qū)機(jī)制,具體可以看看官方的文檔
這句代碼的意思就是說將這個新的分區(qū)機(jī)制換成舊的,那么這個權(quán)限問題就不存在了,這顯然是治標(biāo)不治本
3.還有各種在java文件中申請權(quán)限的
String[] permission = {
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE
};
ActivityCompat.requestPermissions(Mainactivity.this, permission, 1);
這種呢是屬于申請了一部分權(quán)限
4.我是小米手機(jī),有的打開了系統(tǒng)的“所有訪問權(quán)限”頁面,但是那里面沒有我的應(yīng)用,我也不知道為什么
5.解決方案文章來源:http://www.zghlxwxcb.cn/news/detail-470016.html
if (Environment.isExternalStorageManager()) {
tv.setText("666");
String picturePath = "/storage/emulated/0/DCIM/Camera/IMG_20230129_123518.jpg";
Bitmap bitmap = BitmapFactory.decodeFile(picturePath);
Drawable d = new BitmapDrawable(getResources(), bitmap);
textview.setCompoundDrawablesWithIntrinsicBounds(null, d, null, null);
}else {
Intent intent = new Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION);
intent.setData(Uri.parse("package:" + this.getPackageName()));
startActivity(intent);
}
這樣直接跳轉(zhuǎn)到應(yīng)用的所有訪問權(quán)限頁面,同意就大功告成了文章來源地址http://www.zghlxwxcb.cn/news/detail-470016.html
到了這里,關(guān)于關(guān)于android11,12權(quán)限問題Unable to decode stream: open failed: EACCES (Permission denied)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!