Pecurka Pecurka - 1 month ago 11
Android Question

How to find files in a folder that name starts with a specific String in Android

Can someone help me...

I am making an Android app and i need now to search for files in Download folder of my device and if i find files that name starts with

data
then call a function.

Thanks.

Answer

Something like this should work:

File dir = Environment
    .getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
File[] files = dir.listFiles();

for (File file : files) {
    if (file.getName().startsWith("data")) {
        // call your function
    }
}

Don't forget to add the READ_EXTERNAL_STORAGE permission to your manifest:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

Since the protectionLevel of this permission is dangerous, on API level 23+ you need to request this permission at runtime. Check out the corresponding developer's guide article.