如何在我的Android应用程序中截取屏幕截图

时间:2016-04-07 05:00:56

标签: java android

我正在尝试从我的Android应用程序中截取屏幕截图。我正在使用我在网上找到的一些代码,因为我是java的新手。这是我到目前为止所拥有的。

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();
    }
}

当我按下调用我的方法的按钮时,这就是日志所说的内容:

W / System.err:java.io.FileNotFoundException:/storage/emulated/0/newimg.jpg:open failed:EACCES(Permission denied)

这些是我在清单中的权限:

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

如果有人想进一步帮助我,我也很想知道如何截取屏幕的某个部分,例如我的VideoView 小部件而不是整个屏幕(或者可能是在将屏幕截图存储到图库/ sd之前裁剪屏幕截图的方法)。

任何提示都非常感激。

3 个答案:

答案 0 :(得分:1)

如上所述设置权限后,

这些行应该在onCreate中。

 Bitmap bm = screenShot(getWindow().getDecorView().findViewById(R.id.webView)); // you must change the view's 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's not existed.
    }

答案 1 :(得分:0)

您必须在运行时请求许可,否则会发生EACCES(权限被拒绝),请参阅Android网站上的M个文档Requesting Permissions

添加要在列表中使用的权限

    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;

    }

}

答案 2 :(得分:0)

尝试以下代码:

gyp WARN download NVM_NODEJS_ORG_MIRROR is deprecated and will be removed in node-gyp v4, please use NODEJS_ORG_MIRROR

添加到Androidmanifest.xml文件

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();
    }
}