Android 当我去参加另一项活动时滑翔崩溃
我使用Glide in Recyclerview adapter从服务器加载映像当我关闭活动或转到另一个活动时,问题就出现了Android 当我去参加另一项活动时滑翔崩溃,android,okhttp,android-glide,Android,Okhttp,Android Glide,我使用Glide in Recyclerview adapter从服务器加载映像当我关闭活动或转到另一个活动时,问题就出现了 @Override public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { final MyAdapterHolader postholader = (MyAdapterHolader) holder; f
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
final MyAdapterHolader postholader = (MyAdapterHolader) holder;
final ListMeesg move = (ListMeesg) moveList.get(position);
glide.asBitmap().load(move.imagp).skipMemoryCache(true).override(move.width,move.hegth).format(DecodeFormat.PREFER_RGB_565).into(new CustomTarget<Bitmap>(move.width,move.hegth) {
@Override
public void onResourceReady(@NonNull final Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
postholader.imgtitle.setImageBitmap(resource);
new Thread(new Runnable() {
public void run() {
File file1 = new File(Environment.getExternalStoragePublicDirectory("/Android/data/mjplus.messages/"), move.imagn);
try {
if (!file1.exists()) {
FileOutputStream fileOutputStream;
fileOutputStream = new FileOutputStream(file1);
resource.compress(CompressFormat.JPEG, 80, fileOutputStream);
fileOutputStream.close();
}
} catch (IOException e2) {
e2.printStackTrace();
}
}
}).start();
}
@Override
public void onLoadCleared(@Nullable Drawable placeholder) {
postholader.imgtitle.setImageDrawable(placeholder);
}
@Override
public void onDestroy() {
}
@Override
public void onStop() {
}
@Override
public void onLoadFailed(@Nullable Drawable errorDrawable) {
]
}
@Override
public void onLoadStarted(@Nullable Drawable placeholder) {
]
}
});
}
问题是当滑翔被打断时,你的关闭没有被调用 已在附加的堆栈跟踪中获取资源,但从未释放。 有关避免资源泄漏的信息,请参阅java.io.Closeable。 java.lang.Throwable:未在调用显式终止方法“close” 打开(CloseGuard.java:184) 您可以使用try with资源
if (!file1.exists()) {
try (
//put the resources that you want to close in the round brackets
FileOutputStream fileOutputStream = new FileOutputStream(file1);
){
resource.compress(CompressFormat.JPEG, 80, fileOutputStream);
} catch (IOException e2) { ... your exception stuff...}
}
请注意,不再需要调用close
if (!file1.exists()) {
try (
//put the resources that you want to close in the round brackets
FileOutputStream fileOutputStream = new FileOutputStream(file1);
){
resource.compress(CompressFormat.JPEG, 80, fileOutputStream);
} catch (IOException e2) { ... your exception stuff...}
}