问题
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, replacingpath
with just the subdirectory of interest (audio/records
), eliminating the/data/user/0/com.xl.cl.debug/cache
bitYou are calling
grantUriPermission()
, where the first parameter is not a packageYou are calling
grantUriPermission()
, where the first parameter is not a package identifying the app to which you are trying to grant permissionYou are not adding
FLAG_GRANT_READ_URI_PERMISSION
to theIntent
, which is the typical way of saying "theUri
in thisIntent
should be readable by the recipient of thisIntent
" (though it is possible thatACTION_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 thatCalling
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