0

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()). I asked for this permissions:

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

    //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:

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:

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:

@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. Sorry for not using loging.

EDIT 2: Seems like the problem is error I get after permission popup: Screen overlay detected

papaBart
  • 31
  • 10

1 Answers1

0

Hello your assuming sdcard is always /sdcard... Yes I know that sounds stupid. 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

**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

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" />
Scott Chambers
  • 581
  • 6
  • 22
  • Yes, I get a popup "Allow ScreenshotApp to access photos, media and files on your device? I'm not sure atm if it checks for both permissions or not. Regarding your sugestion I get: E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: /storage/emulated/0/screencaps/1515070630835.png: open failed: ENOENT (No such file or directory) This is without File.exists() check since it could not be found – papaBart Jan 04 '18 at 13:59
  • Odd? Is there no directory? Have you made a directory? 0 should be the sdcard? – Scott Chambers Jan 04 '18 at 14:06
  • Yes, it is there. If I go to Windows Explorer I can see both directory and screenshots. – papaBart Jan 04 '18 at 14:14
  • The only thing I can think of is your app is saving files as "ss" + milliseconds and your looking for just the milliseconds – Scott Chambers Jan 04 '18 at 14:28
  • You are right, I forgot to add ss part, now I get this: E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: /storage/emulated/0/screencaps/ss1515076505310.png: open failed: EACCES (Permission denied) It has to be something with permissions then probably... – papaBart Jan 04 '18 at 14:36
  • I'm not at my pc so I can't do much else but see if this link helps https://stackoverflow.com/questions/11144783/how-to-access-an-image-from-the-phones-photo-gallery – Scott Chambers Jan 04 '18 at 15:01
  • Edited my awnser. Idk if that will help. – Scott Chambers Jan 05 '18 at 14:40