Android 与其他应用共享多张图片

Android Share Multiple Images with Other Apps

我正在尝试使用本机 Android 共享功能来共享我使用 Glide 检索到的一组图像。因此,任何与图像兼容的应用程序都会显示在 Android 的共享菜单中。

我创建了一个在单击共享时调用的方法。

private void onShareClicked() { 
    GlideUrl glideUrl = new GlideUrl(url1, new LazyHeaders.Builder().addHeader("x-auth-token", mToken).build());
    FutureTarget<File> image1 = mGlide.load(glideUrl).downloadOnly(SIZE_ORIGINAL, SIZE_ORIGINAL);

    glideUrl = new GlideUrl(url2, new LazyHeaders.Builder().addHeader("x-auth-token", token).build());
    FutureTarget<File> image2 = mGlide.load(glideUrl).downloadOnly(SIZE_ORIGINAL, SIZE_ORIGINAL);

    new ShareImagesTask(this, "image/png").execute(image1, image2);
}

然后创建了 ShareImagesTask,它允许您通过 Glide 完成获取图像文件的请求来共享多个图像:

public class ShareImagesTask extends AsyncTask<FutureTarget<File>, Void, ArrayList<Uri>> {

    private final Context mContext;
    private final String mMimeType;

    public ShareImagesTask(Context context, String mimeType) {
        this.mContext = context;
        this.mMimeType = mimeType;
    }

    @Override
    protected ArrayList<Uri> doInBackground(FutureTarget<File>... targets) {
        final ArrayList<Uri> imageUris = new ArrayList<>(targets.length);

        for(FutureTarget<File> target : targets) {
            try {
                File file = target.get();
                Uri uri = FileProvider.getUriForFile(mContext, "com.myapp.fileprovider", file);
                imageUris.add(uri);
            } catch (Exception ex) {
                Log.w("SHARE", "Sharing failed for one or more image files.", ex);
            }
        }

        return imageUris;
    }

    @Override
    protected void onPostExecute(ArrayList<Uri> result) {
        if(!result.isEmpty()) {
            Intent intent = new Intent();
            intent.setAction(Intent.ACTION_SEND_MULTIPLE);
            intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, result);
            intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            intent.setType(mMimeType);
            mContext.startActivity(Intent.createChooser(intent, "Share Your Photo"));
        }
    }
}

这是我的 AndroidManifest.xml FileProvider:

<provider
    android:name=".provider.MyAppFileProvider"
    android:authorities="com.myapp.fileprovider"
    android:grantUriPermissions="true"
    android:exported="false">
    <meta-data
        android:name="android.support.FILE_PROVIDER_PATHS"
        android:resource="@xml/filepaths" />
</provider>

最后,filepaths.xml

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <cache-path name="shared_images" path="/data/data/com.myapp/cache/image_manager_disk_cache/"/>
</paths>

我可以调用 FileProvider.getUriForProvider(),它给出了以下异常。我也尝试将 filepaths.xml 中的路径更改为异常中提到的路径的变体,但我得到了同样的错误。有什么想法吗?

java.lang.IllegalArgumentException: Failed to find configured root that contains /data/data/com.myapp/cache/image_manager_disk_cache/11fa9845150748cc77d49a75954d92f246cc7095e72e36b85d6a07dbdeb9cf46.0

替换:

<cache-path name="shared_images" path="/data/data/com.myapp/cache/image_manager_disk_cache/"/>

与:

<cache-path name="shared_images" />

或可能与:

<cache-path name="shared_images" path="image_manager_disk_cache/"/>

如果您试图将 FileProvider 限制为 getCacheDir() 的子集。