Method of adapting Android 7.0 to obtain URI of file

Foreword#

Android 7.0 has been released for a long time. Although the market share is not very high, it will be popular sooner or later, so it is urgent to adapt to Android 7.0.

Android 7.0 optimizes the system a lot: for example, file access rights, power saving, network, background, etc., the most prominent of which is URI access outside the application.

When will the out of application access of URI be used? For a simple example, download the APK update. At this time, the system function will be called to install the APK. This is the URI of the file to access the file outside the application.

However, this may seem unsafe. In case of any very important file, it will be bad. Therefore, Android 7.0 needs to process and encrypt the URI accessed outside the application.

Text#

How should we deal with this problem? First, we need to register the ContentProvider, which is very familiar. It can share the contents of the database with other applications. Now it is to share files:

There are three ways to set sharing paths:

After the provider is registered, we need to modify our java code slightly:

In the Java code, we first judge the system version of the current mobile phone. If it is below 7.0, use the previous URI. If it is above 7.0, start to obtain the encrypted URI through the provider.

Format of the resulting URI:

It seems that the shared path is encrypted, and the complete path of the file cannot be seen directly.

Summary#

OK, it's so simple to solve this problem. If you access files in the application, it won't be affected. For example, our operations such as reading pictures are relatively simple due to tight time. Then write something a little higher.

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>