1

i enable strict mode in my app and it crashed with an error

 boolean DEVELOPER_MODE=true;
        if (DEVELOPER_MODE) {
            StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
                    .detectDiskReads()
                    .detectDiskWrites()
                    .detectAll()   // or .detectAll() for all detectable problems //.detectNetwork()
                    .penaltyLog()
                    .build());
            StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
                    .detectLeakedSqlLiteObjects()
                    .detectLeakedClosableObjects()
                    .penaltyLog()
                    .penaltyDeath()
                    .build());
        }

E/StrictMode: A resource was acquired at attached stack trace but never released. See java.io.Closeable for information on avoiding resource leaks.java.lang.Throwable: Explicit termination method 'close' not called

at line

 private  void getRecentImages (){

            String[] colums={MediaStore.Images.Media.DATA,MediaStore.Images.Media._ID};

            Cursor imageCursor = getActivity().getContentResolver().query(
                    MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                    colums,
                    null,
                    null,
                    MediaStore.Images.Media._ID);
            int image_Column_Index=imageCursor.getColumnIndex(MediaStore.Images.Media._ID);
            count=imageCursor.getCount();
            thumbnails=new Bitmap[count];
            arrayPath=new String[count];
            for (int i = 0; i <count; i++) {
                imageCursor.moveToPosition(i);
                int id = imageCursor.getInt(image_Column_Index);
                int dataColumnIndex = imageCursor.getColumnIndex(MediaStore.Images.Media.DATA);
                arrayPath[i] = imageCursor.getString(dataColumnIndex);
                // APP CRASHED WITH THIS LINE
                thumbnails[i] = MediaStore.Images.Thumbnails.getThumbnail(getActivity().getContentResolver(),id,MediaStore.Images.Thumbnails.MICRO_KIND,null);  
            }
            imageCursor.close();

        }

i am closing cursor after loop ending and i am not sure why i am getting this error even i closed cursor after i no longer needed.please guide me to get this fix. I don't have option to disable strict mode in any case so i need to fix this without disabling strict mode.

Full stacks trace of error.

12-26 10:21:58.358 12925-12925/com.example.testD/Bottom Sheet: Parent = null Context com.elaxer.Navigation_Drawer@cbf3705
12-26 10:21:59.976 12925-12925/com.example.testW/ExifInterface: Skip the tag entry since tag number is not defined: 769
12-26 10:21:59.976 12925-12925/com.example.testW/ExifInterface: Skip the tag entry since tag number is not defined: 771
12-26 10:21:59.976 12925-12925/com.example.testW/ExifInterface: Skip the tag entry since tag number is not defined: 20752
12-26 10:21:59.976 12925-12925/com.example.testW/ExifInterface: Skip the tag entry since tag number is not defined: 20753
12-26 10:21:59.976 12925-12925/com.example.testW/ExifInterface: Skip the tag entry since tag number is not defined: 20754
12-26 10:22:01.059 12925-12925/com.example.testW/ExifInterface: Skip the tag entry since tag number is not defined: 2
12-26 10:22:01.149 12925-12925/com.example.testW/ExifInterface: Skip the tag entry since tag number is not defined: 2
12-26 10:22:01.255 12925-12925/com.example.testW/ExifInterface: Skip the tag entry since tag number is not defined: 2
12-26 10:22:01.334 12925-12925/com.example.testW/ExifInterface: Skip the tag entry since tag number is not defined: 2
12-26 10:22:01.788 12925-12925/com.example.testW/ExifInterface: Skip the tag entry since tag number is not defined: 41985
12-26 10:22:01.788 12925-12925/com.example.testW/ExifInterface: Skip the tag entry since tag number is not defined: 41986
12-26 10:22:01.788 12925-12925/com.example.testW/ExifInterface: Skip the tag entry since tag number is not defined: 41987
12-26 10:22:01.788 12925-12925/com.example.testW/ExifInterface: Skip the tag entry since tag number is not defined: 41988
12-26 10:22:01.788 12925-12925/com.example.testW/ExifInterface: Skip the tag entry since tag number is not defined: 41990
12-26 10:22:01.789 12925-12925/com.example.testW/ExifInterface: Skip the tag entry since tag number is not defined: 41996
12-26 10:22:01.867 12925-12925/com.example.testW/ExifInterface: Skip the tag entry since tag number is not defined: 41985
12-26 10:22:01.867 12925-12925/com.example.testW/ExifInterface: Skip the tag entry since tag number is not defined: 41986
12-26 10:22:01.867 12925-12925/com.example.testW/ExifInterface: Skip the tag entry since tag number is not defined: 41987
12-26 10:22:01.867 12925-12925/com.example.testW/ExifInterface: Skip the tag entry since tag number is not defined: 41988
12-26 10:22:01.867 12925-12925/com.example.testW/ExifInterface: Skip the tag entry since tag number is not defined: 41990
12-26 10:22:01.867 12925-12925/com.example.testW/ExifInterface: Skip the tag entry since tag number is not defined: 41996
12-26 10:22:01.904 12925-12933/com.example.testE/StrictMode: A resource was acquired at attached stack trace but never released. See java.io.Closeable for information on avoiding resource leaks.
    java.lang.Throwable: Explicit termination method 'close' not called
        at dalvik.system.CloseGuard.open(CloseGuard.java:180)
        at java.io.FileInputStream.<init>(FileInputStream.java:78)
        at java.io.FileInputStream.<init>(FileInputStream.java:103)
        at android.media.ExifInterface.loadAttributes(ExifInterface.java:1338)
        at android.media.ExifInterface.<init>(ExifInterface.java:1057)
        at android.media.ThumbnailUtils.createThumbnailFromEXIF(ThumbnailUtils.java:479)
        at android.media.ThumbnailUtils.createImageThumbnail(ThumbnailUtils.java:96)
        at android.provider.MediaStore$InternalThumbnails.getThumbnail(MediaStore.java:753)
        at android.provider.MediaStore$Images$Thumbnails.getThumbnail(MediaStore.java:1085)
        at com.elaxer.mainScreenFragments.BottomSheet.getRecentImages(BottomSheet.java:207)
        at com.elaxer.mainScreenFragments.BottomSheet.onCreateView(BottomSheet.java:69)
        at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2600)
        at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:881)
        at androidx.fragment.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManagerImpl.java:1238)
        at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:1303)
        at androidx.fragment.app.BackStackRecord.executeOps(BackStackRecord.java:439)
        at androidx.fragment.app.FragmentManagerImpl.executeOps(FragmentManagerImpl.java:2079)
        at androidx.fragment.app.FragmentManagerImpl.executeOpsTogether(FragmentManagerImpl.java:1869)
        at androidx.fragment.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManagerImpl.java:1824)
        at androidx.fragment.app.FragmentManagerImpl.execPendingActions(FragmentManagerImpl.java:1727)
        at androidx.fragment.app.FragmentManagerImpl$2.run(FragmentManagerImpl.java:150)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:148)
        at android.app.ActivityThread.main(ActivityThread.java:5417)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
12-26 10:22:01.906 12925-12933/com.example.testW/System.err: StrictMode VmPolicy violation with POLICY_DEATH; shutting down.
12-26 10:22:01.906 12925-12933/com.example.testI/Process: Sending signal. PID: 12925 SIG: 9
Ritu
  • 518
  • 2
  • 12
  • 35

1 Answers1

0

Without removing your StrictMode you can achieve with the help of Glide library (Image Handling Library). It will not give you any error. I remove bit of your code to make it work without errors.

 private  void getRecentImages (){            
        String[] colums={MediaStore.Images.Media.DATA,MediaStore.Images.Media._ID};
        final String orderBy = MediaStore.Images.Media.DATE_ADDED + " DESC";
        Cursor imageCursor = getActivity().getContentResolver().query(
                MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                colums,
                null,
                null,
                orderBy);
        count=imageCursor.getCount();
        arrayPath=new String[count];
        for (int i = 0; i <count; i++) {
            imageCursor.moveToPosition(i);
            int dataColumnIndex = imageCursor.getColumnIndex(MediaStore.Images.Media.DATA);
            arrayPath[i] = imageCursor.getString(dataColumnIndex);
            //thumbnails[i] = MediaStore.Images.Thumbnails.getThumbnail(getActivity().getContentResolver(),id,MediaStore.Images.Thumbnails.MICRO_KIND,null);
        }
        imageCursor.close();

    }

You are getting path of your images in arrayPath so no need to use thumbnail method. so where ever you need to load image just use Glide like this.

        `Glide.with(getActivity()).load(arrayPath[position]).into(youImageView);`
androidXP
  • 1,692
  • 3
  • 27
  • 58