简体   繁体   中英

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 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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