Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 当我去参加另一项活动时滑翔崩溃_Android_Okhttp_Android Glide - Fatal编程技术网

Android 当我去参加另一项活动时滑翔崩溃

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

我使用Glide in Recyclerview adapter从服务器加载映像当我关闭活动或转到另一个活动时,问题就出现了

   @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...}
 }