在附加的堆栈跟踪中获取了资源,但从未释放过-错误 [英] A resource was acquired at attached stack trace but never released - Error
问题描述
我不确定为什么会出现此错误,在5次使用模拟器设置墙纸的过程中,有2次出现错误-
资源是在附加堆栈跟踪中获取的,但从未释放。有关避免资源泄漏的信息,请参见java.io.Closeable。
I'm not sure why I am getting this error, 2 out of 5 times I set the wallpaper using the emulator, I get the error - "A resource was acquired at attached stack trace but never released. See java.io.Closeable for information on avoiding resource leaks."
当我使用手机设置墙纸时,它可以完美工作而不会出现故障。为什么在使用模拟器时会崩溃?
When I set the wallpaper using my phone, it works perfectly without fault. Why does it crash when I use I use the emulator?
这是我的代码:
public class SetWallpaperAsync extends AsyncTask<String, String, String> {
private Context context;
private ProgressDialog pDialog;
String image_url;
URL mImageUrl;
String myFileUrl1;
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]);
// myFileUrl1 = 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();
}
}
LOG Cat:
11-09 15:09:11.726: E/StrictMode(1632): A resource was acquired at attached stack trace but never released. See java.io.Closeable for information on avoiding resource leaks.
11-09 15:09:11.726: E/StrictMode(1632): java.lang.Throwable: Explicit termination method 'end' not called
11-09 15:09:11.726: E/StrictMode(1632): at dalvik.system.CloseGuard.open(CloseGuard.java:184)
11-09 15:09:11.726: E/StrictMode(1632): at java.util.zip.Inflater.<init>(Inflater.java:82)
11-09 15:09:11.726: E/StrictMode(1632): at java.util.zip.GZIPInputStream.<init>(GZIPInputStream.java:96)
11-09 15:09:11.726: E/StrictMode(1632): at java.util.zip.GZIPInputStream.<init>(GZIPInputStream.java:81)
11-09 15:09:11.726: E/StrictMode(1632): at libcore.net.http.HttpEngine.initContentStream(HttpEngine.java:528)
11-09 15:09:11.726: E/StrictMode(1632): at libcore.net.http.HttpEngine.readResponse(HttpEngine.java:836)
11-09 15:09:11.726: E/StrictMode(1632): at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:274)
11-09 15:09:11.726: E/StrictMode(1632): at libcore.net.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:486)
11-09 15:09:11.726: E/StrictMode(1632): at com.google.ads.internal.f.b(SourceFile:490)
11-09 15:09:11.726: E/StrictMode(1632): at com.google.ads.internal.f.run(SourceFile:460)
11-09 15:09:11.726: E/StrictMode(1632): at java.lang.Thread.run(Thread.java:856)
推荐答案
乳化器默认情况下启用StrictMode,在实际设备上,可以通过以下方式在代码中将其打开:
Emulators have StrictMode on by default, on real devices it can be turned on in code via:
StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll().penaltyLog()
.penaltyDeath().build());
您在此处遇到的崩溃表明您需要手动关闭打开的网络资源,以便他们可以释放内存,请参见 Android文档:
The crash you're getting here says you need to manually close opened web resources so they could free the memory, see this example from Android docs:
URL url = new URL("http://www.android.com/");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
try {
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
readStream(in);
finally {
urlConnection.disconnect();
}
这篇关于在附加的堆栈跟踪中获取了资源,但从未释放过-错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!