简体   繁体   English

使用FileProvider在Android用户之间共享文件

[英]Share file across Android Users with FileProvider

I am looking to share a file with a service running with the android:singleUser attribute. 我希望与使用android:singleUser属性运行的服务共享文件。 The file is saved into the local data directory for the Android User. 该文件将保存到Android用户的本地数据目录中。 So this could be something like one of the follow: 因此,可能类似于以下内容之一:

  • /data/user/0/com.example.fileshare/files/myfile.txt /data/user/0/com.example.fileshare/files/myfile.txt
  • /data/user/10/com.example.fileshare/files/myfile.txt /data/user/10/com.example.fileshare/files/myfile.txt
  • /data/user/11/com.example.fileshare/files/myfile.txt /data/user/11/com.example.fileshare/files/myfile.txt

I start the service to share this file like: 我启动服务以共享此文件,例如:

Uri fileUri = FileProvider.getUriForFile(getApplicationContext(), "com.example.fileshare.fileprovider", file);

Log.d(TAG, "Share File URI: " + fileUri.toString());

Intent shareFileIntent = new Intent(getApplicationContext(), FileSharingService.class);
shareFileIntent.setAction(Intent.ACTION_SEND);
shareFileIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
String mimeType = getContentResolver().getType(fileUri);
Log.d(TAG, "Share File MIME Type: " + mimeType);
shareFileIntent.setDataAndType(
     fileUri,
     mimeType);

startService(shareFileIntent);

But when I receive the intent that was sent from any user of than the UserHandle.USER_OWNER in my FileSharingService The ContentResolver cannot find the file because the file does not exists in the same users data directory that the service is running in because of the android:singleUser attribute. 但是,当我在FileSharingService中收到从除UserHandle.USER_OWNER之外的任何用户发送的意图时,ContentResolver找不到该文件,因为该文件由于android:singleUser而在运行该服务的用户数据目录中不存在android:singleUser属性。

@Override
protected void onHandleIntent(Intent intent) {
    Log.d(TAG, "onHandleIntent - " + intent.getAction());

    if (Intent.ACTION_SEND.equals(intent.getAction())) {
        Uri fileUri = intent.getData();

        // When I try and read the file it says that the file does not exist. 
        FileInputStream in = getContentResolver().openInputStream(fileUri);
    }
}

Here are the FileProvider and Service in the AndroidManifest.xml: 这是AndroidManifest.xml中的FileProvider和Service:

<provider
    android:name="android.support.v4.content.FileProvider"
    android:authorities="com.example.fileshare.fileprovider"
    android:grantUriPermissions="true" >
    <meta-data
        android:name="android.support.FILE_PROVIDER_PATHS"
        android:resource="@xml/file_paths" />
</provider>

<service
    android:name="com.example.fileshare.FileSharingService"
    android:exported="true"
    android:singleUser="true" >
    <intent-filter>
        <action android:name="android.intent.action.SEND" />

        <category android:name="android.intent.category.DEFAULT" />

        <data android:mimeType="*/*" />
    </intent-filter>
</service>

I ended up using a DocumentsProvider with the android:singleUser="true" and android:exported="true" instead of FileProvider. 我最终使用了带有android:singleUser="true"android:exported="true"而不是FileProvider的DocumentsProvider。

With a subclass of DocumentsProvider set up I could then use this piece of code to create a new document and write the contents of the file from the one user into the resolved Uri returned. 在设置了DocumentsProvider的子类之后,我可以使用这段代码来创建一个新文档,并将一个用户的文件内容写入返回的已解析Uri中。

final ContentResolver resolver = getContentResolver();
Uri derivedUri = DocumentsContract.buildDocumentUri(
                        "com.example.documentsprovider.authority", "root");
client = acquireUnstableProviderOrThrow(
                        resolver, "com.example.documentsprovider.authority");
childUri = DocumentsContract.createDocument(
                        client, derivedUri, mimeType, displayName);


InputStream in = mContext.getContentResolver().openInputStream(myFileUri);
OutputStream os = mContext.getContentResolver().openOutputStream(childUri);

byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
    os.write(buf, 0, len);
}

in.close();
os.close();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM