权限被拒绝但是在清单中设置了权限

时间:2016-08-18 06:52:22

标签: android permissions

我收到此错误:

  

删除失败:ENOENT(没有这样的文件或目录):/ storage / emulated/0 / screenShot.jpg
  FileNotFoundException:/storage/emulated/0/screenShot.jpg:open failed:EACCES(Permission denied)

在运行此屏幕截图方法时(错误指向从imageFile创建的FileOutputStream,在“else”之后):

public void shareScreenshot(View v){
    Date now = new Date();
    android.text.format.DateFormat.format("yyyy-MM-dd_hh-mm-ss", now);

    CharSequence nowStr = DateFormat.format("yyyy-MM-dd_hh-mm-ss", now);

    try {
        // image naming and path  to include sd card  appending name you choose for file
        String mPath = Environment.getExternalStorageDirectory().toString() + "/" + "screenShot" + ".jpg";

        // create bitmap screen capture
        View v1 = getWindow().getDecorView().getRootView();
        v1.setDrawingCacheEnabled(true);
        Bitmap bitmap = Bitmap.createBitmap(v1.getDrawingCache());
        v1.setDrawingCacheEnabled(false);

        File imageFile = new File(mPath);
        if(imageFile.delete()){

            FileOutputStream outputStream = new FileOutputStream(imageFile);
            int quality = 100;
            bitmap.compress(Bitmap.CompressFormat.JPEG, quality, outputStream);
            outputStream.flush();
            outputStream.close();

            startShareIntent(imageFile);

        }else{

            FileOutputStream outputStream = new FileOutputStream(imageFile);
            int quality = 100;
            bitmap.compress(Bitmap.CompressFormat.JPEG, quality, outputStream);
            outputStream.flush();
            outputStream.close();

            startShareIntent(imageFile);
        }


    } catch (Throwable e) {
        // Several error may come out with file handling or OOM
        e.printStackTrace();
        Toast.makeText(this,"Something went wrong, try again.", Toast.LENGTH_SHORT).show();

    }
}

这是我的清单:

<?xml version="1.0" encoding="utf-8"?>

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="com.android.vending.BILLING" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/Theme.AppCompat.Light.NoActionBar" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

3 个答案:

答案 0 :(得分:2)

要在运行时请求权限,请使用此功能检查上面的版本23以及下面的

public  boolean isPermissionGrantedForStorage() {
    if (Build.VERSION.SDK_INT >= 23) {
        if (checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
                == PackageManager.PERMISSION_GRANTED) {
            Log.v(TAG,"Permission is granted");
            return true;
        } else {

            Log.v(TAG,"Permission is revoked");
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
            return false;
        }
    }
    else { 
         //permission is automatically granted on sdk<23 upon installation
        Log.v(TAG,"Permission is granted");
        return true;
    }
}

确保您的活动实施OnRequestPermissionResult

现在回调将是

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if(grantResults[0]== PackageManager.PERMISSION_GRANTED){
        Log.v(TAG,"Permission: "+permissions[0]+ "was "+grantResults[0]);
        //resume tasks needing this permission
    }
}

更多的是你没有向FileOutputStream写任何东西。没有写下你怎么能期望存储图像!

理想情况下应该是这样的

ByteArrayOutputStream bytearrayoutputstream;
File file;
FileOutputStream fileoutputstream;

@Override
 protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      bytearrayoutputstream = new ByteArrayOutputStream();
      // you code to access image and other logic ...
      Bitmap bitmap = Bitmap.createBitmap(v1.getDrawingCache());
      bitmap.compress(Bitmap.CompressFormat.PNG, 60, bytearrayoutputstream);
      file = new File( Environment.getExternalStorageDirectory() + "/SampleImage.png");
     try 
      {
      file.createNewFile();
      fileoutputstream = new FileOutputStream(file);
      fileoutputstream.write(bytearrayoutputstream.toByteArray()); 
      fileoutputstream.close();
      } 
      catch (Exception e) 
      {
           e.printStackTrace();
      } 
}

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

一起

答案 1 :(得分:1)

从API级别23开始,需要在运行时请求更高的相机权限

将onCreate代码更改为:

if (PackageManager.PERMISSION_GRANTED == ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CAMERA)) { // asks primission to use the devices camera

 //what ever you want to do ..

} else {
    requestWritePermission(MainActivity.this);
}

并添加功能

private static void requestWritePermission(final Context context) {
    if (ActivityCompat.shouldShowRequestPermissionRationale((Activity) context, Manifest.permission.CAMERA)) {
        new AlertDialog.Builder(context)
                .setMessage("This app needs permission to use The phone Camera in order to activate the Scanner")
                .setPositiveButton("Allow", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        ActivityCompat.requestPermissions((Activity) context, new String[]{Manifest.permission.CAMERA}, 1);
                    }
                }).show();

    } else {
        ActivityCompat.requestPermissions((Activity) context, new String[]{Manifest.permission.CAMERA}, 1);
    }
}

添加这些行并重试。 另外,如需更多输入,请阅读流畅的语言:

https://developer.android.com/training/permissions/requesting.html

答案 2 :(得分:-1)

它显示此错误,因为此代码中指定的文件夹中没有此类文件。您指定的文件名可能有问题。在截取屏幕截图时,它会以应用程序名称保存。