Android 关闭输入流以停止资源泄漏

Android 关闭输入流以停止资源泄漏,android,inputstream,Android,Inputstream,我最近遇到了“在附加的堆栈跟踪中获取了资源,但从未释放”的问题 我已经读到你需要关闭连接后,它已被使用。我在这里找不到任何对我有帮助的问题,所以我正在写我自己的问题 在使用输入流来防止这种情况发生后,我将如何关闭它 类别: public class SetWallpaperAsync extends AsyncTask<String, String, String> { private Context context; private ProgressDialog pDialog;

我最近遇到了“在附加的堆栈跟踪中获取了资源,但从未释放”的问题

我已经读到你需要关闭连接后,它已被使用。我在这里找不到任何对我有帮助的问题,所以我正在写我自己的问题

在使用输入流来防止这种情况发生后,我将如何关闭它

类别:

public class SetWallpaperAsync extends AsyncTask<String, String, String> {
private Context context;
private ProgressDialog pDialog;
String image_url;
URL mImageUrl;
String myFileUrl;
Bitmap bmImg = null;

public SetWallpaperAsync(Context context) {
this.context = context;
} 

@Override
protected void onPreExecute() {
// TODO Auto-generated method stub

super.onPreExecute();

pDialog = new ProgressDialog(context);
pDialog.setMessage("Setting Wallpaper...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();

}

@Override
protected String doInBackground(String... args) {
// TODO Auto-generated method stub

try {

    mImageUrl = new URL(args[0]);


    HttpURLConnection conn = (HttpURLConnection) mImageUrl
            .openConnection();
    conn.setDoInput(true);
    conn.connect();
    InputStream is = conn.getInputStream();



    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inPreferredConfig = Config.RGB_565;
    Bitmap bmImag = BitmapFactory.decodeStream(is, null, options);





} catch (IOException e) {
    e.printStackTrace();
}

return null;
}

@Override
protected void onPostExecute(String args) {
// TODO Auto-generated method stub
WallpaperManager wpm = WallpaperManager.getInstance(context);
try {
    wpm.setBitmap(bmImg);
} catch (IOException e) {

    // TODO Auto-generated catch block
    e.printStackTrace();
}
pDialog.dismiss();

}

}
公共类setwallparasync扩展异步任务{
私人语境;
私人对话;
字符串图像链接;
URL-mImageUrl;
字符串myFileUrl;
位图bmImg=null;
公共setwallparasync(上下文){
this.context=上下文;
} 
@凌驾
受保护的void onPreExecute(){
//TODO自动生成的方法存根
super.onPreExecute();
pDialog=新建进度对话框(上下文);
设置消息(“设置壁纸…”);
pDialog.setUndeterminate(假);
pDialog.setCancelable(假);
pDialog.show();
}
@凌驾
受保护的字符串doInBackground(字符串…args){
//TODO自动生成的方法存根
试一试{
mImageUrl=新URL(参数[0]);
HttpURLConnection conn=(HttpURLConnection)mImageUrl
.openConnection();
conn.setDoInput(真);
连接();
InputStream is=conn.getInputStream();
BitmapFactory.Options=new-BitmapFactory.Options();
options.inPreferredConfig=Config.RGB_565;
位图bmImag=BitmapFactory.decodeStream(is,null,选项);
}捕获(IOE异常){
e、 printStackTrace();
}
返回null;
}
@凌驾
受保护的void onPostExecute(字符串参数){
//TODO自动生成的方法存根
WallpareManager wpm=wallpareManager.getInstance(上下文);
试一试{
wpm.setBitmap(bmImg);
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
pDialog.disclose();
}
}

您需要在输入流上调用close方法。理想情况下,它应该在finally块中,这样即使出现异常也可以调用它。要实现这一点,请将InputStream声明移到try之外:

InputStream is = null;
然后可以在try块中执行此操作: is=conn.getInputStream()

然后,在您的捕获块之后,包括一个finally:

 finally{
      if(is != null){
          try{
            is.close();
          }catch(Exception e){}
      }
    }

阅读
InputStream
的文档怎么样?也许它包含了一个线索?