0

这是我拍摄屏幕截图并将其保存在画廊中的代码。通过调试在这里尝试捕获屏幕但不保存到画廊。

@Override
            public void onClick(View v) {
                Bitmap bitmap = takeScreenshot();
                saveBitmap(bitmap);
            }

制作截图

protected Bitmap takeScreenshot() {
    // TODO Auto-generated method stub
    View rootView = findViewById(android.R.id.content).getRootView();
    rootView.setDrawingCacheEnabled(true);
    Toast.makeText(getApplicationContext(), "Screen captured", Toast.LENGTH_LONG).show();
    return rootView.getDrawingCache();

}

在此处保存屏幕

 protected void saveBitmap(Bitmap bitmap) {
            // TODO Auto-generated method stub
            File imagePath = new File(Environment.getExternalStorageDirectory()
                    + "/screenshot.png");

            FileOutputStream fos;
            try {
                fos = new FileOutputStream(imagePath);
                bitmap.compress(CompressFormat.JPEG, 100, fos);
                fos.flush();
                fos.close();
                Toast.makeText(getApplicationContext(), "Screen saved", Toast.LENGTH_LONG).show();
            } catch (FileNotFoundException e) {
                Log.e("GREC", e.getMessage(), e);
            } catch (IOException e) {
                Log.e("GREC", e.getMessage(), e);
            }
        }
4

1 回答 1

0

请在设备中运行您的代码,这可能是模拟器的问题,因为模拟器没有物理 SD 卡。

于 2013-09-16T10:31:16.170 回答