简体   繁体   English

拒绝打开我创建的屏幕截图

[英]Permission denied for opening a screen shot I created

I'm using a rooted phone to take screen shots using shell and I want to open the image I took (it is visible in file explorer and it exists since I check it with File.exists() ). 我正在使用带根的电话使用Shell拍摄屏幕快照,我想打开我拍摄的图像(在文件资源管理器中可见,并且由于我使用File.exists()检查它而存在)。 I asked for this permissions: 我要求此权限:

AndroidManifest.xml AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="package.name.example">

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

    <application
        android:allowBackup="true"
...

In code I check if permissions are granted: 在代码中,我检查是否授予了权限:

MainActivity.java MainActivity.java

    //Check permissions
    if ((ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
            == PackageManager.PERMISSION_GRANTED) && (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE)
            == PackageManager.PERMISSION_GRANTED)) {
        // permissions already given
    } else {
        // request permission
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE}, 101);
    }

So on button click I take a screen shot using shell: 因此,在单击按钮时,我使用shell进行了截屏:

long startMiliseconds = System.currentTimeMillis();
Process captureScreen = Runtime.getRuntime().exec("su",null,null);
OutputStream os = captureScreen.getOutputStream();
os.write(("/system/bin/screencap -p "+"/sdcard/screencaps/ss"+ startMiliseconds +".png").getBytes("ASCII"));
os.flush();
os.close();

This will ask me for super user permission and take a screen shot of screen. 这将要求我获得超级用户许可,并进行屏幕截图。 When I try to access created image programaticly (I first check if file exists and it does): 当我尝试以编程方式访问创建的图像时(我首先检查文件是否存在并且存在):

String filePath = "/sdcard/screencaps/ss"+ startMiliseconds +".png";
Bitmap bMap = BitmapFactory.decodeFile(filePath);
img.setImageBitmap(bMap);

(img is an ImageView label) I get this error: (img是一个ImageView标签)我收到此错误:

E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: /sdcard/screencaps/ss1515070630835.png: open failed: EACCES (Permission denied)

EDIT: Seems it is permission related. 编辑:似乎与权限有关。 I have changed min SDK and target SDK to 23. I changed handling of permission request to: 我已将min SDK和目标SDK更改为23。我将权限请求的处理更改为:

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    switch (requestCode) {
        case 101:

            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                //granted permission
                System.out.println("INFO: 1");
            } else {
                //not granted permission
                System.out.println("INFO: 2");
            }

            if (grantResults.length > 0 && grantResults[1] == PackageManager.PERMISSION_GRANTED) {
                //granted permission
                System.out.println("INFO: 3");
            } else {
                //not granted permission
                System.out.println("INFO: 4");
            }
            break;
        default:
            super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }
}

And even if I press Allow in popup I get INFO: 2 and INFO: 4 which means that my permissions are not accepted. 即使我在弹出窗口中按“允许”,我也会得到INFO:2和INFO:4,这意味着我的权限未被接受。 Sorry for not using loging. 很抱歉不使用日志记录。

EDIT 2: Seems like the problem is error I get after permission popup: Screen overlay detected 编辑2:似乎问题出在权限弹出窗口后出现错误:检测到屏幕覆盖

Hello your assuming sdcard is always /sdcard... Yes I know that sounds stupid. 您好,您假设sdcard始终是/ sdcard ...是的,我知道这听起来很愚蠢。 Try 尝试

Environment.getExternalStorageDirectory()+File.separator+"/screencaps/ss"

On small edit: 在小的编辑:

You haven't said if you have a popup appear on your screen? 您还没有说屏幕上是否出现弹出窗口? That might be you issue as well. 那也可能是您的问题。 User has to give app permission. 用户必须授予应用权限。

If so read this and look at the examples https://developer.android.com/training/permissions/requesting.html 如果是这样,请阅读并查看示例https://developer.android.com/training/permissions/requesting.html

**Edit : ** This is a huge shot in the dark but here is something you could put in your getting image method, where you parse the image to imageview **编辑:**这是黑暗中的大镜头,但是您可以在获取图像的方法中放入一些内容,将图像解析为imageview

galleryIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
startActivityForResult(galleryIntent, PICK_FROM_GALLERY);

Setting up a gallery intent. 设置画廊意图。 Or maybe adding 或者也许添加

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM