我有这种方法可以显示从SD卡到Android工作室的一张图像:
public void showImage(){
File imgFile = new File(Environment.getExternalStorageDirectory().getPath()+PATH+"Foto.jpg");
if(imgFile.exists()){
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
ivProfileImage.setImageBitmap(myBitmap);
Log.i("Image","The image exists!");
}
}
其中ivProfileImage是ImageView,而PATH是正确的。我收到了这条消息:
E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: /storage/2673-1D16/ProfilePhotos/Foto.jpg: open failed: EACCES (Permission denied)
I/Image: The image exists!
我的AndroidManifest.xml拥有权限:
我一直在寻找答案,但没有人适合我。有什么想法吗?
提前致谢。
答案 0 :(得分:4)
您必须检查运行时的权限。这是Android 6中的新功能。Look here.
答案 1 :(得分:1)
private String[] apppermissions =new String[] {
Manifest.permission.CAMERA,
Manifest.permission.CALL_PHONE,
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.INTERNET,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.RECORD_AUDIO
};
private boolean checkAndRequestPermission() {
StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy(builder.build());
java.util.List<String> listPermissionsNeeded = new ArrayList<>();
for (String perm : apppermissions)
{
if (ContextCompat.checkSelfPermission(activity, perm) != PackageManager.PERMISSION_GRANTED)
{
listPermissionsNeeded.add(perm);
}
}
if (!listPermissionsNeeded.isEmpty())
{
ActivityCompat.requestPermissions(activity, listPermissionsNeeded.toArray(new String[listPermissionsNeeded.size()]),
PERMISSION_REQUEST_CODE);
return false;
}
return true;
}