mediastore – How to access files of specific folder in android that contains .nomedia file-ThrowExceptions

Exception or error:

I am using below code to try to get images data from WhatsApp/Media/.Statuses, as .Statuses folder contains .nomedia file so cursor is not returning any data. How can i ignore .nomedia file in order to get images data for .Statuses folder.

public class MainActivity extends AppCompatActivity {

private final String TAG = "statusTest";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
        Log.i(TAG, "permission available");


        Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
        String[] projection = {MediaStore.Images.Media.DISPLAY_NAME};
        String selection = MediaStore.Images.Media.DATA + " LIKE ?";
        String[] selectionArgs = {"%WhatsApp/Media/.Statuses%"};
        Cursor cursor = getContentResolver().query(uri, projection, selection, selectionArgs, null);
        if (cursor != null && cursor.getCount() > 0) {
            while (cursor.moveToNext()) {
                Log.i(TAG, cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DISPLAY_NAME)));
            }
        } else {
            Log.i(TAG, "cursor null");
        }
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if (requestCode == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
        Log.i(TAG, "permission granted");
    } else {
        Log.i(TAG, "permission refused");
    }
}}
How to solve:

Leave a Reply

Your email address will not be published. Required fields are marked *