2014-12-05 33 views
0

我在下面的函数工作,但我无法返回值“网络连接”,给了以下错误:Android的 - 功能无法识别回报名

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

感谢您的帮助。

回答

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

作品谢谢... – FAAD 2014-12-05 07:18:20

0

申报File对象出方,如果情况:

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