代码省略了“try-catch”

时间:2011-10-19 20:01:43

标签: android eclipse try-catch screenshot

我有这个代码,用于截取屏幕截图。我把它放在我的一个应用程序中并且运行得非常好,我用它作为“草稿”。

但是,现在我将代码复制到原始项目中,似乎代码从未真正进入“try”部分。这有点令人困惑,因为它在其他示例应用程序上仍然正常运行但不在此处。仅为了您的信息,也没有发布任何错误。这里发生了什么?

public void getScreen()
{
    View table = findViewById(R.id.TransactionLog);
    table.setDrawingCacheEnabled(true);

    table.buildDrawingCache(true);

    Bitmap bitmap = Bitmap.createBitmap(table.getDrawingCache());
    table.setDrawingCacheEnabled(false); // Clear drawing cache

    File doc = new File( Environment.getExternalStorageDirectory() + "TransactionHistory.png");

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

1 个答案:

答案 0 :(得分:1)

除了提出的所有其他建议之外,还可以在线上抛出异常:

File doc = new File.....

在这种情况下,它永远不会输入你的try / catch。在方法的开头和调用方法的任何地方放置一些日志语句或调试断点。