gpt4 book ai didi

java - 位图空指针异常 bitmap = content.getDrawingCache();

转载 作者:行者123 更新时间:2023-11-29 22:17:32 24 4
gpt4 key购买 nike

我正在尝试创建一个允许用户截取当前 View 的应用程序。我正在使用以下代码来执行此操作。

View content = ((Activity)ctx).findViewById(R.id.rootlayout);
Bitmap bitmap = content.getDrawingCache();
File file = new File("/sdcard/test.png");

try
{
file.createNewFile();
FileOutputStream ostream = new FileOutputStream(file);
bitmap.compress(CompressFormat.PNG, 100, ostream);
ostream.close();
}
catch (Exception e)
{
e.printStackTrace();
}

但是,我在以下行中收到空指针异常:

Bitmap bitmap = content.getDrawingCache();

这意味着什么, View 是空的?

任何帮助都会很棒!

最佳答案

您需要调用 content.setDrawingCacheEnabled(true); 才能使用它。完成后调用 content.setDrawingCacheEnabled(false);

关于java - 位图空指针异常 bitmap = content.getDrawingCache();,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8137209/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com