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