How to Share Files Between Android 7.0 Applications

  • 2021-12-11 09:07:57
  • OfStack

In development, it is often necessary to transfer a file to another application, such as uploading pictures to another application, copying and pasting files between different storage paths, etc., all of which need to share files. It can be understood that the application receiving files is sending requests to the application providing files.

Starting with Android 7.0, Android enforces the StrictMode policy, Prohibit exposing file outside the application: //URL, if FileProvider is not used in applications above Android 7.0, FileUriExposedException exception will be thrown. After Android 7.0, content://URL should be used to grant URL temporary access rights, that is, FileProvider should be used to grant temporary access rights. URL with temporary access rights is safe, and this temporary URL will automatically expire, in which getUriForFile () provided by FileProvider is used to generate file contents.

In all cases, the only safe way to provide a file from your application to another application is to send the contents of the file, URI, to the receiving application and grant temporary access to that URI. Content URI with temporary URI access is secure because they are only available to applications that receive URI, and they automatically expire. The Android FileProvider component provides the getUriForFile () method for generating the contents of the file URI.

Here, we will also mention an exception that often occurs in Android 7.0 and later: FileUriExposedException, which can be solved by using FileProvider. Of course, this is also the result of the continuous improvement of Android system security.

Specify FileProvider Specify the file share path

Specify FileProvider

Specify Provider in the AndroidManifest file as follows:


<manifest xmlns:android="http://schemas.android.com/apk/res/android"
 package="com.example.myapp">
 <application
  ...>
  
  <!--android:authorities="${applicationId}.yourname"-->
  <provider
   android:name="android.support.v4.content.FileProvider"
   <!--authorities Property specifies the value to be used for the FileProvider Generated content URI Adj. URI Authority ,1 Like is applicationId.yourname" Composition -->
   android:authorities="com.example.myapp.fileprovider"
   android:grantUriPermissions="true"
   android:exported="false">
   <meta-data
    android:name="android.support.FILE_PROVIDER_PATHS"
    android:resource="@xml/filepaths" />
  </provider>
  ...
 </application>
</manifest>

Specify the file share path

In the above code, the file directory to be shared is specified in meta-data directory, and the file directory is defined in filepathd. xml. The paths that can be defined in the corresponding xml are as follows, for specific reference:


<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
 <paths>

  <!-- Represents the root directory of the device (new File("/"))-->
  <root-path name="root" path="" />
  <!-- Denote context.getFileDir()-->
  <files-path name="files" path="" />
  <!-- Denote context.getCacheDir()-->
  <cache-path name="cache" path="" />
  <!-- Denote Environment.getExternalStorageDirectory()-->
  <external-path name="external" path="" />
  <!-- Denote context.getExternalFilesDirs()-->
  <external-files-path name="name" path="path" />
  <!-- Denote getExternalCacheDirs()-->
  <external-cache-path name="name" path="path" />

 </paths>
</resources>

In xml, it needs two attributes to indicate a certain path. path indicates the subdirectory of the current specified directory. If it is not specified, it indicates the root directory and subdirectory under the current specified directory. name indicates that URL added by name will be used as the access path of the file. Reference is as follows:


// Indicates that the file currently to be shared will be displayed in the  context.getFileDir()  Directory  images  Find the files to share under the subdirectory 
<paths>
 <files-path path="images/" name="myImage" />
</paths>

// Represents the file of the final generated share URL
content://com.example.myapp.fileprovider/myImage/image.jpg

Get Uri

Finally, after the configuration is complete, all the files that need to be used should be obtained as follows when obtaining Url, as follows:


public Uri getUri(File file) {
 Uri uri = null;
 if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
  uri = FileProvider.getUriForFile(mContext, mContext.getPackageName() + ".youName", file);
 } else {
  uri = Uri.fromFile(file);
 }
 return uri;
}

In this way, you can share files happily above Android 7.0, and this knowledge point is often encountered in development.

These are the details of how Android 7.0 applications share files. For more information about Android 7.0 sharing files, please pay attention to other related articles on this site!


Related articles: