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

标签 android

我希望与使用 android:singleUser 属性运行的服务共享文件。该文件保存到 Android 用户的本地数据目录中。所以这可能类似于以下之一:

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

我启动服务来共享此文件,例如:

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);

但是当我收到从我的 FileSharingService 中的 UserHandle.USER_OWNER 以外的任何用户发送的 Intent 时,ContentResolver 找不到该文件,因为该文件不存在于运行该服务的同一用户数据目录中,因为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);
    }
}

这是 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>

最佳答案

我最终使用了带有 android:singleUser="true"android:exported="true" 的 DocumentsProvider 而不是 FileProvider。

设置了 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();

关于android - 使用 FileProvider 在 Android 用户之间共享文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28953460/

相关文章:

java - 如何使用 java 中的 Retrofit 从 Google 附近的搜索 API 检索图像 url

android - 如何从内部存储中的特定文件夹获取文件列表?

android - 如何将 listview 项目链接到 android 中的不同 Activity ?

android - 如何定期在后台跟踪(如果可能)Android 设备(最新版本)?

android - 如何以编程方式删除android手机上联系人列表中的所有联系人

android - Activity 和 Service 之间的通信

android - RxJava 的模拟单元测试

android - 在 Android 中解码 JSON 字符串

android - Facebook ,安卓 java.lang.IllegalStateException : Cannot execute task: the task is already running

android - 从android中的文本文件中读取