我正在处理以下功能,但我无法返回值" 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;
}
感谢您的帮助。
答案 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对象声明为outFile 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)
这是因为您的fi
在if 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;
}