Does IS_PENDING default to 1?

3 weeks ago 27
ARTICLE AD BOX

I'm experiencing some strange behaviour with IS_PENDING and MediaStore files. The documentation is very clear:

If your app performs potentially time-consuming operations, such as writing to media files, it's useful to have exclusive access to the file as it's being processed. On devices that run Android 10 or higher, your app can get this exclusive access by setting the value of the IS_PENDING flag to 1. Only your app can view the file until your app changes the value of IS_PENDING back to 0.

This sounds like IS_PENDING is something that is entirely managed by my app. Nevertheless, I'm experiencing a situation where it looks like IS_PENDING is set automatically by MediaStore because I'm forced to set it to 0 to make things work.

The situation is this: I'm creating an image in downloads using something like this:

values.put(MediaStore.Downloads.DISPLAY_NAME, "foo.png"); values.put(MediaStore.Downloads.MIME_TYPE, "image/png"); values.put(MediaStore.Downloads.RELATIVE_PATH, Environment.DIRECTORY_DOWNLOADS); target = MediaStore.Downloads.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY); uri = resolver.insert(target, values); pfd = resolver.openFileDescriptor(uri, "w"); ... pfd.close();

Now I try to query the file size of this newly created file immediately after closing it using something like this:

cursor = resolver.query(uri, new String[]{OpenableColumns.DISPLAY_NAME, OpenableColumns.SIZE}, null, null, null); cursor.moveToFirst(); size = cursor.getLong(1)

Surprisingly, SIZE is 0 even though that's clearly not the file size because I've written several bytes to it.

To my great confusion I can easily fix the issue by simply setting IS_PENDING to 0 on the Uri, like so:

ContentValues values = new ContentValues(); values.put(MediaStore.MediaColumns.IS_PENDING, 0); resolver.update(uri, values, null, null);

If I do this after pfd.close() then I can query SIZE just fine and it returns the correct file size instead of 0. But this behaviour doesn't make sense to me because I 've never set IS_PENDING to 1 so why should I suddenly be forced to set it to 0 again?

That's why I'd like to ask for some explanation of this behaviour. Could it be that IS_PENDING gets automatically set to 1 by MediaStore or why am I seeing this strange behaviour?

My test system is Android 16, target SDK is 35.

Read Entire Article