截屏然后尝试将其保存在模拟器库中,但它没有保存

时间:2013-09-16 10:26:45

标签: android

这是我的代码,用于拍摄屏幕截图并将其保存在gallery.tried通过调试此处屏幕被捕获但未保存到图库。

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

1 个答案:

答案 0 :(得分:0)

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