I am trying to take a screenshot inside my android application. I am using code that I found on the Internet since I am new to java. Here is what I still have.
public void screenshot(View view){
View v = view.getRootView();
v.setDrawingCacheEnabled(true);
Bitmap b = v.getDrawingCache();
String extr = Environment.getExternalStorageDirectory().toString();
File myPath = new File(extr, getString(R.string.new_image)+".jpg");
FileOutputStream fos = null;
try {
fos = new FileOutputStream(myPath);
b.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
MediaStore.Images.Media.insertImage( getContentResolver(), b,
"Screen", "screen");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
When I click on the button that calls my method, this is what the log says:
W / System.err: java.io.FileNotFoundException: /storage/emulated/0/newimg.jpg: open failed: EACCES (Permission denied)
these are the permissions that I have in my manifest:
<uses-permission
android:required="true"
android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission
android:required="true"
android:name="android.permission.CHANGE_WIFI_STATE"/>
<uses-permission
android:required="true"
android:name="android.permission.INTERNET"/>
<uses-permission
android:required="true"
android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission
android:required="true"
android:name="android.permission.READ_EXTERNAL_STORAGE"/>
Also, if someone would like to help me further, I would also like to know how to take a screenshot of only a certain part of the screen, for example, my VideoView widget instead of the entire screen (or, perhaps, a way to crop the screenshot before saving it in the / sd gallery )
Any helpful tips.