Share audio file from /data/user/0/ directory using FileProvider

五迷三道 提交于 2019-12-13 03:14:36

问题


I'm using FileProvider API for sharing content actually storing in internal storage.

Following is my xml configuration that linked with Provider configured in Manifiest file.

<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <cache-path name="xs_audio" path="/audio/records"/>
</paths>

and code that I'm using to share is following:

    private final static String FILE_PROVIDER = BuildConfig.APPLICATION_ID + ".fileprovider";

    private String testPackageAppHaveAccess = "com.whats";
            public static void shareDocument(Activity activity, CallRecordData data) {
                Intent intent = new Intent();
                intent.setAction(Intent.ACTION_SEND_MULTIPLE);
                intent.putExtra(Intent.EXTRA_SUBJECT, "Record File");
                intent.setType("audio/*");

                ArrayList<Uri> files = new ArrayList<>();
                //for (AudioModelObj image : data.getDocuments()) {
                    files.add(getImageUri(activity, data.getFile()));
                //}

                activity.grantUriPermission(testPackageAppHaveAccess, files.get(0), Intent.FLAG_GRANT_READ_URI_PERMISSION);
                intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, files);
                activity.startActivity(Intent.createChooser(intent, "Share Audio File."));
            }

            private static Uri getImageUri(Activity activity, String audioURI) {
                if (Build.VERSION.SDK_INT < 24) {
                    return Uri.parse(audioURI);
                } else {
                    URI uri = URI.create(audioURI);
                    File file = new File(uri);
                    return FileProvider.getUriForFile(activity, FILE_PROVIDER, file);
                }
            }
}

but while launching with app it's not attaching anything. In case of gmail it say "can't attach empty file". File is confirmedly available as I'm displaying list of file and playing.

For reference: Uri generating from getImageUri(..) is

/data/user/0/com.xl.cl.debug/cache/audio/records/17-10-17_170728_abc_.wav

Any suggestion what I'm doing wrong ?


回答1:


  • <files-path> already points to what on some devices will be /data/user/0/com.xl.cl.debug

  • <cache-path> is what you should be using, replacing path with just the subdirectory of interest (audio/records), eliminating the /data/user/0/com.xl.cl.debug/cache bit

  • You are calling grantUriPermission(), where the first parameter is not a package

  • You are calling grantUriPermission(), where the first parameter is not a package identifying the app to which you are trying to grant permission

  • You are not adding FLAG_GRANT_READ_URI_PERMISSION to the Intent, which is the typical way of saying "the Uri in this Intent should be readable by the recipient of this Intent" (though it is possible that ACTION_SEND_MULTIPLE requires more work here, as I haven't played with that much)

  • No filesystem path in the human history has begun with content:/, so get rid of that

  • Calling new File() and supplying a value that is not a filesystem path is not going to work well

There may be more problems than those, but that should get you started



来源:https://stackoverflow.com/questions/46789824/share-audio-file-from-data-user-0-directory-using-fileprovider

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!