简体   繁体   English

Android-无法识别函数的返回名称

[英]Android - function not recognized return name

I am working on the following function, but I'm having trouble returning the value "fi", giving the following error: 我正在使用以下功能,但是在返回值“ fi”时遇到麻烦,出现以下错误:

fi can not be resolved to a variable.

Here is my function: 这是我的功能:

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

Thanks for your help. 谢谢你的帮助。

the variable fi is out of scope for your return statement, you need to define it outside of your initial if statement. 如果变量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;
}

Declare File object out side if case : 如果出现以下情况,请声明File对象:

File fi;

Example : 范例:

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

That is because your fi is declared in an if clause and out of the scope. 那是因为您的fi是在if clause声明的,并且不在范围内。 Do something like this: 做这样的事情:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM