权限被拒绝,但我有权限访问

问题描述:

我已经对我的清单仍然没有保存权限访问。我昨天试了这个,它保存了我的图像,现在我不能。我不知道这里有什么问题。 logcat的说java.io.IOException异常:打开失败:EACCES(拒绝)权限被拒绝,但我有权限访问

下面

是代码

save.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     RelativeLayout content = (RelativeLayout) findViewById(R.id.dashBoard); 
     content.setDrawingCacheEnabled(true); 
     Bitmap bitmap = content.getDrawingCache(); 
     File file = new File(Environment.getExternalStorageDirectory().getPath()+ imagename+ ".png"); 

     try { 
      if (!file.exists()) { 
       file.createNewFile(); 

      } 
      FileOutputStream ostream = new FileOutputStream(file); 
      bitmap.compress(Bitmap.CompressFormat.PNG, 10, ostream); 
      ostream.close(); 
      content.invalidate(); 
      Toast.makeText(getApplicationContext(), "SAVED", Toast.LENGTH_SHORT).show(); 
     } catch (Exception e) { 
       e.printStackTrace(); 

     } finally { 
      content.setDrawingCacheEnabled(false); 
     } 
    } 
}); 

这里是全logcat的 https://www.dropbox.com/s/dlb6p5i1nd1kxua/Logcat.txt?dl=0

+0

检查targetSdkversion应小于23 – Anil

+0

如果大于23接受运行时间许可来自用户 – Anil

+0

我应该使用什么? –

你需要使用实时许可后SDK需要API 23。

你可以问这样的代码权限。

if (Build.VERSION.SDK_INT >= 23) { 
        if (checkWritePermission()) { 
         // your code 
        } else { 
         requestStoragePermission(); 
        } 
       } else { 
        // your code 
       } 

您需要检查权限并请求放入此方法。

private boolean checkWritePermission() { 
     int result = ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE); 
     if (result == PackageManager.PERMISSION_GRANTED) { 
      return true; 
     } else { 
      return false; 
     } 
    } 





private void requestStoragePermission() { 

     if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) { 
      Toast.makeText(MainActivity.this, "EXTERNAL STORAGE permission allows us to save image/video. Please allow this permission in App Settings.", Toast.LENGTH_LONG).show(); 
     } else { 
      ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, PERMISSION_STORAGE_REQUEST_CODE); 
     } 
    } 

使用onRequestPermissionsResult的手柄许可结果的方法

不要忘了把许可清单上

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
+0

我使用API​​ 22我真的需要这个吗? –

+0

我用你的答案。仍然没有工作和相同的logcat –

+0

你运行应用程序意味着真正的设备或模拟器。 –