截图未在Android Bitmap中更新

时间:2016-06-24 09:15:55

标签: android screenshot

我正在处理Android代码,我点击了按钮的截图。我能够第一次获得当前活动的屏幕截图,但是当我再次获取屏幕截图时,它不会更新。我发现它再次获取屏幕截图,但它保存了旧图像。这是我的代码:

    View v1 = getWindow().getDecorView().getRootView();
    v1.setDrawingCacheEnabled(true);
    v1.refreshDrawableState();
    v1.buildDrawingCache(true);
    Bitmap  myBitmap = Bitmap.createBitmap(v1.getDrawingCache());
    v1.destroyDrawingCache();
    v1.setDrawingCacheEnabled(false);
    String filePath = Environment.getExternalStorageDirectory() + File.separator + "Pictures/myapp.png";
    File imagePath = new File(filePath);
    FileOutputStream fos;
    try {
        fos = new FileOutputStream(imagePath);
        myBitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
        fos.flush();
        fos.close();
        MediaScannerConnection.scanFile(MainActivity.this,
                new String[]{imagePath.toString()}, null,
                new MediaScannerConnection.OnScanCompletedListener()
                {
                    public void onScanCompleted(String path, Uri uri)
                    {
                        Log.w("ExternalStorage", "Scanned " + path + ":");
                        Log.w("ExternalStorage", "-> uri=" + uri);
                    }
                });
        send(imagePath);
    }
    catch (FileNotFoundException e)
    {
        Log.e("MSG", e.getMessage(), e);
    }
    catch (IOException e)
    {
        Log.e("MSG", e.getMessage(), e);
    }

虽然这个问题以不同的方式提出并且有答案,但我仍然无法解决这个问题。

2 个答案:

答案 0 :(得分:0)

也许是文件存取问题?您可以尝试检查您的文件是否已经存在(即使您之前刚刚创建了500毫秒),有时Android在重写文件和打开它们时会有非常奇怪的行为。

这个或您显示屏幕截图的视图(如果我正确地解决了您的问题)没有“刷新”,您可以尝试在那里查看:How can you tell when a layout has been drawn?

答案 1 :(得分:0)

有时会发生..我的一个项目遇到了同样的问题。我通过更改文件名解决了这个问题。如果您更改文件名和访问权限,则可以解决。