Android - 功能无法识别返回名称

时间:2014-12-05 07:00:55

标签: java android function

我正在处理以下功能,但我无法返回值" fi",发出以下错误:

fi can not be resolved to a variable.

这是我的功能:

public File getBitmapFromwebchartView(WebView view2) {

    if (view2 != null) {
        view2.setDrawingCacheEnabled(true);
        Bitmap b = view2.getDrawingCache();
        if (b != null) {


            try {

                File fi = new File(Environment.getExternalStorageDirectory(), "Screenshot" + ".jpg");
                //fi     = new File(Environment.getExternalStorageDirectory(),"Realitycheck" + ".jpg");

                // write the bytes in file
                FileOutputStream fo;

                fo = new FileOutputStream(fi);

                b.compress(CompressFormat.JPEG, 95, fo);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    return fi;
}

感谢您的帮助。

3 个答案:

答案 0 :(得分:1)

变量fi超出了return语句的范围,您需要在初始if语句之外定义它。

public File getBitmapFromwebchartView(WebView view2) {

File fi = null;

if (view2 != null) {
    view2.setDrawingCacheEnabled(true);
    Bitmap b = view2.getDrawingCache();
    if (b != null) {


        try {

            fi = new File(Environment.getExternalStorageDirectory(), "Screenshot" + ".jpg");
            //fi     = new File(Environment.getExternalStorageDirectory(),"Realitycheck" + ".jpg");

            // write the bytes in file
            FileOutputStream fo;

            fo = new FileOutputStream(fi);

            b.compress(CompressFormat.JPEG, 95, fo);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
return fi;
}

答案 1 :(得分:0)

如果是case:

,则将File对象声明为out
File fi;

示例:

public File getBitmapFromwebchartView(WebView view2) {
    File fi;
    if (view2 != null) {
        view2.setDrawingCacheEnabled(true);
        Bitmap b = view2.getDrawingCache();
        if (b != null) {
         try {
            fi = new File(Environment.getExternalStorageDirectory(), "Screenshot" + ".jpg");
            // write the bytes in file
            FileOutputStream fo;
            fo = new FileOutputStream(fi);
            b.compress(CompressFormat.JPEG, 95, fo);
         } catch (Exception e) {
            e.printStackTrace();
         }
        }
    }
    return fi;
}

答案 2 :(得分:0)

这是因为您的fiif clause中声明并超出了范围。 做这样的事情:

public File getBitmapFromwebchartView(WebView view2) {

    File fi;

    if (view2 != null) {
        view2.setDrawingCacheEnabled(true);
        Bitmap b = view2.getDrawingCache();
        if (b != null) {


            try {

                fi = new File(Environment.getExternalStorageDirectory(), "Screenshot" + ".jpg");
                //fi     = new File(Environment.getExternalStorageDirectory(),"Realitycheck" + ".jpg");

                // write the bytes in file
                FileOutputStream fo;

                fo = new FileOutputStream(fi);

                b.compress(CompressFormat.JPEG, 95, fo);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    return fi;
}