Dear Android developer

(This post was originally published in Spanish, on the 31st of January, 2025.)

A few days ago, I received the -nth communication from Google, in this case about an Android application. It could be summarized as follows, since all Google emails are more or less the same.

Dear Android developer:

Leave everything, we have preference… We are Google! In our infinite wisdom, we have decided that we should not let your scabby app open a file open dialog. Especially if you’re going to just select a media file. This is a vulgarity, even a barbarity, and thus we have decided to declare it obsolete.
You know, that READ_MEDIA permission? The one allowing you to explore the multimedia contents… tsk, tsk… Too dangerous. Obsolete too.
It’s important for you to take into account that we have kindly prepared a term for you to upgrade your “apps”, using the new API. Unfortunately, the deadline was yesterday, although you can apply for a term extension.
One of our teams had prepared a new compatibility API designed to ease the transition. Once the new API was ended, we realized it was already obsolete.

F**k you,
Google.

I think you get the idea.

Until know, the way to prepare a callback for a media file open dialog was as follows:

final ActivityResultLauncher<String> LAUNCH_PICKER =
    this.registerForActivityResult(
        new ActivityResultContracts.GetContent(), 
            uri -> {
                if ( uri != null ) {
                    // Do something with the URI
                }
            });

Enter fullscreen mode Exit fullscreen mode

And it was open like this:

String intentTypeStr = "image/*";
this.LAUNCH_PICKER.launch( intentTypeStr );

Enter fullscreen mode Exit fullscreen mode

So, you choose the adequate MIME filter for images (you would use another one for videos), create the selector and launch it. The thing is not more complex like that.

Currently, you have to use this other (specific) API:

ActivityResultLauncher<PickVisualMediaRequest> pickMedia =
        this.registerForActivityResult(new PickVisualMedia(), uri -> {
    if ( uri != null ) {
        // Do something with the URI
    }
});

Enter fullscreen mode Exit fullscreen mode

And it will be launched like so:

pickMedia.launch(new PickVisualMediaRequest.Builder()
        .setMediaType(PickVisualMedia.ImageOnly.INSTANCE)
        .build());

Enter fullscreen mode Exit fullscreen mode

I can understand that leaving file filtering in the hands of the programmer could suppose a security problem, but the new layers of security added in the last years should also be considered. At the beginning, one could open almost any file using the Java File class directly, just directing it to the app’s storage. This even allowed directory navigation. Since Android 7, though, you can only just open URI’s from the app’s storage. These URI’s must be sanctioned by a ContentProvider scaffolding. Last but not least, remember that you are offering the user to do something intentional: selecting a file!

The thing is even better. If we take a look to the blog post where they talk about the availability for media files in the cloud for the new media picker, we discover that this support must be explicit. So, if a company like Dropbox or Microsoft Cloud or… want to offer their users the possibility of selecting a media file from their services… they have to add that support explicitly! Again! Wouldn’t it be more rational to directly port the support from the old API? At least the most popular ones?

That is where the biggest “but” sits. Actually, Google is just offering a new, more specific API, connecting it to existing functionality. Wouldn’t it be easier to launch the new API from the old one (the next code is only a draft):

    if ( filter == MIME_IMAGES
      || filter == MIME_VIDEOS )
    {
        pickMedia.launch(
            new PickVisualMediaRequest.Builder()
                .setMediaType( PickVisualMedia.
                                   ImageAndVideo.
                                   INSTANCE )
                .build());
    }

    // More things

Enter fullscreen mode Exit fullscreen mode

…the transition would be much easier for everyone. The new documentation should just mention the new API, sure.

But then, it wouldn’t be Google. The company has shown much contempt for developers. Remember AsyncTask? It was originally recommended as the preferred method for concurrency. Sure, it’s obsolete. For years, now. It took everyone by surprise. What was the big sin that would come from adapting it to use Runnables and so on?

Life must go on despite Google, though.

原文链接:Dear Android developer

© 版权声明
THE END
喜欢就支持一下吧
点赞11 分享
The world is like a mirror: Frown at itand it frowns at you; smile, and it smiles too.
世界犹如一面镜子:朝它皱眉它就朝你皱眉,朝它微笑它也吵你微笑
评论 抢沙发

请登录后发表评论

    暂无评论内容