Repository: opencv
Issue: imread fail in android's JNI sometimes.
Labels: platform: android,category: imgcodecs,incomplete,needs investigation

##### System information (version)
- OpenCV => :4.4.0, 4.2.0, 4.1.0, 4.0.1
- Operating System / Platform => :android api 29, ndk 21
##### Detailed description

I'm trying to read images by opencv on android's JNI. Here is my code.
```
#include <android/asset_manager_jni.h>
#include <android/bitmap.h>
#include <android/log.h>
#include <opencv2/opencv.hpp>

#include <jni.h>

#include <string>
#include <vector>
#include <dirent.h>
#include <unistd.h>

extern "C" {

JNIEXPORT jboolean JNICALL Java_com_example_lightnet_LightNet_Init(JNIEnv *env, jobject thiz)
{
    __android_log_print(ANDROID_LOG_DEBUG, "lightnet",  "----------------------------------lightnet info----------------------------------");
    std::string dir_path = "/storage/emulated/0/Download/";
    DIR* dir;
    dir = opendir("/storage/emulated/0/Download/");
    struct dirent* ptr;
    cv::Mat img;
    while((ptr = readdir(dir)) != NULL){
        if(ptr->d_name[0] == '.') continue;
        img = cv::imread(dir_path+ptr->d_name);
        if(img.empty()){
            __android_log_print(ANDROID_LOG_DEBUG, "lightnet",  "fail to load %s%s", dir_path.c_str(),ptr->d_name);
        } else{
            __android_log_print(ANDROID_LOG_DEBUG, "lightnet",  "success to load %s%s with opencv",dir_path.c_str(),ptr->d_name);
            usleep(30*1000);
            //do something
        }
    }
    closedir(dir);
    __android_log_print(ANDROID_LOG_DEBUG, "lightnet",  "----------------------------------lightnet info----------------------------------");
    return 1;
}
}
```

_imread_ is used in a loop, and the code will sleep 30 ms if _imread_ get data. Result is here.

_D/lightnet: ----------------------------------lightnet info----------------------------------
D/lightnet: success to load /storage/emulated/0/Download/t.jpg with opencv
D/lightnet: success to load /storage/emulated/0/Download/sun_avolqigacohsryip.jpg with opencv
D/lightnet: success to load /storage/emulated/0/Download/Maine_Coon_247.jpg with opencv
D/lightnet: success to load /storage/emulated/0/Download/n02085620_3093.jpg with opencv
D/lightnet: success to load /storage/emulated/0/Download/76e8820d12ef75fcc8bb341f4184e04b.jpg with opencv
D/lightnet: success to load /storage/emulated/0/Download/0083aaf965ba309a150ea1fe0534ac68.jpg with opencv
D/lightnet: success to load /storage/emulated/0/Download/71490.jpg with opencv
D/lightnet: success to load /storage/emulated/0/Download/2503513.jpg with opencv
D/lightnet: success to load /storage/emulated/0/Download/american_bulldog_182.jpg with opencv
D/lightnet: success to load /storage/emulated/0/Download/beagle_103.jpg with opencv
D/lightnet: success to load /storage/emulated/0/Download/d09f6c8abd1d352db52d2dd498ffd4ff.jpg with opencv
D/lightnet: success to load /storage/emulated/0/Download/drone_0847_image-14.jpeg with opencv
D/lightnet: success to load /storage/emulated/0/Download/drone_0914_image-42.jpeg with opencv
D/lightnet: success to load /storage/emulated/0/Download/John_Ashcroft_0053.jpg with opencv
D/lightnet: fail to load /storage/emulated/0/Download/query_drone_0025_image-16.jpeg
    fail to load /storage/emulated/0/Download/n02101006_1814.jpg
    fail to load /storage/emulated/0/Download/sun_asjcjpiziksykten.jpg
    fail to load /storage/emulated/0/Download/1eb4d9dc8b31095400fc792f95689231.jpg
    fail to load /storage/emulated/0/Download/00000785.jpg
    fail to load /storage/emulated/0/Download/888c745680bd6dfb1573f6c4279e33e1.jpg
    fail to load /storage/emulated/0/Download/1631901.jpg
    fail to load /storage/emulated/0/Download/2443052.jpg
    fail to load /storage/emulated/0/Download/3327579.jpg
    fail to load /storage/emulated/0/Download/gallery_drone_0089_image-20.jpeg
    fail to load /storage/emulated/0/Download/google_1575_15.5580166456_724e7136e2_b.jpg
    ----------------------------------lightnet info----------------------------------_

As you see, some images **can't** be loaded. But if delete "usleep(30\*1000)", all images **can** be loaded rightly.  30 ms ~= time for doing something, so i use "usleep(30\*1000)" here to replace it. And in my environment, "usleep(800\*1000)"  and "usleep(10\*1000)" is ok to read all images, but 30ms or 50ms fails. 
without "usleep()", all images can be read. so images and my environment should be ok. I've tried OpenCV 4.0.1, 4.1.0, 4.2.0, 4.4.0. Can anyone explain this result for me? 

##### Steps to reproduce
MainAcitvity.java is here.
```
package com.example.lightnet;


import android.Manifest;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.widget.TextView;
import android.widget.Button;

import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends Activity {
    private static String[] permissions = {
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE
    };
    private LightNet lightnet = new LightNet();
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        verifyStoragePermissions(this);

        lightnet.Init();


    }

    public static void  verifyStoragePermissions(Activity activity){
        try{
            ActivityCompat.requestPermissions(activity, permissions, 1);
        } catch (Exception e){
            e.printStackTrace();
        }
    }
}
```
LightNet.java is here.
```
package com.example.lightnet;

public class LightNet {
    public native boolean Init();

    static {
        System.loadLibrary("lightnet");
    }
}
```