How to take a screenshot inside my Android application

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) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        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.

+4
3

, ,

onCreate.

 Bitmap bm = screenShot(getWindow().getDecorView().findViewById(R.id.webView)); // you must change the view id that you want to take a screenshot
            Date now = new Date();
            android.text.format.DateFormat.format("yyyy-MM-dd_hh:mm:ss", now); // We give a different name from the previous one.
            File file = saveBitmap(bm, now.toString()); // and save

, .

 private Bitmap screenShot(View view) {
        Bitmap bitmap = Bitmap.createBitmap(view.getWidth(),view.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        view.draw(canvas);
        return bitmap;
    }

    private static File saveBitmap(Bitmap bm, String fileName){
        final String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Screenshots";
        File dir = new File(path);
        if(!dir.exists())
            dir.mkdirs();
        File file = new File(dir, fileName);
        try {
            FileOutputStream fOut = new FileOutputStream(file);
            bm.compress(Bitmap.CompressFormat.PNG, 100, fOut);
            fOut.flush();
            fOut.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return file; // save the screenshot into the folder that is created as "Screenshots" if it not existed.
    }
+1

, EACCES (Permission denied) , . M Docs android

,

    String[] perms = {"android.permission.RECORD_AUDIO", "android.permission.CAMERA"};
    int permsRequestCode = 200; 
    requestPermissions(perms, permsRequestCode);

@Override

public void onRequestPermissionsResult(int permsRequestCode, String[] permissions, int[] grantResults){

    switch(permsRequestCode){

        case 200:

            boolean audioAccepted = grantResults[0]==PackageManager.PERMISSION_GRANTED;

            boolean cameraAccepted = grantResults[1]==PackageManager.PERMISSION_GRANTED;

            break;

    }

}
0

try under the code:

public void screenshot(View view){
    View v = view.getRootView();
    v.setDrawingCacheEnabled(true);
    Bitmap b = v.getDrawingCache();
    File root = new File(Environment.getExternalStorageDirectory().getAbsolutePath(), "/Your Folder Name");
    root.mkdir();
    Calendar localCalendar = Calendar.getInstance();
    String fileName = new SimpleDateFormat("yyyyMMddhhmmss").format(localCalendar.getTime());
    FileOutputStream fos = null;
    try {
        fos = new FileOutputStream(new File(root.getAbsolutePath(), fileName + ".jpg"));
        b.compress(Bitmap.CompressFormat.JPEG, 100, fos);
        fos.flush();
        fos.close();
    //After run code See your folder     
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

Add to Androidmanifest.xml

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

All Articles