I've now tried since a long time to make a cross app sharing intent from assets folder for different types of files, but there is always a problem working. I went on a lot of different types of solutions such as:
- Try to use a custom content provider (here, or here, ..)
- Try to save the file on local storage (example here)
- and others but as it didn't worked I don't have links anymore..
Right now, here is my working solution for sharing an audio file to whatsapp and messenger only, all other app are failing).
My function to create a new file on app storage:
public String getNewPathFromSbElem(SbElem sbElem, String fileName) {
AssetManager assetManager = getAssets();
String path = sbElem.soundPath;
String newName = fileName;
String newPath = getExternalFilesDir(null).getAbsolutePath() + File.separator + newName;
InputStream in = null;
OutputStream out = null;
File outFile;
File deleteFile = new File(newPath);
if(deleteFile.exists()) {
deleteFile.delete();
}
try {
in = assetManager.open(path);
outFile = new File(getExternalFilesDir(null), newName);
out = new FileOutputStream(outFile);
copyFile(in, out);
} catch (IOException ex) {
ex.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
if (out != null) {
try {
out.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
return newPath;
}
My 2 functions to share on whatsapp or messenger:
public void shareOnWhatsApp(SbElem sbElem) {
final String newPath = getNewPathFromSbElem(sbElem, "_share_temp_file.mp3");
Intent whatsappIntent = new Intent(Intent.ACTION_SEND);
whatsappIntent.setPackage("com.whatsapp");
whatsappIntent.setType("audio/mp3");
whatsappIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(newPath));
whatsappIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
try {
startActivity(whatsappIntent);
} catch (android.content.ActivityNotFoundException ex) {
Log.d("error", "WhatsApp not installed");
}
}
public void shareOnMessenger (SbElem sbElem) {
final String newPath = getNewPathFromSbElem(sbElem, "_share_temp_file.mp3");
final File newFile = new File(newPath);
final Uri newUri = FileProvider.getUriForFile(this, getString(R.string.file_provider_authority), newFile);
final Integer SHARE_TO_MESSENGER_REQUEST_CODE = 1;
String mimeType = "audio/*";
ShareToMessengerParams shareToMessengerParams = ShareToMessengerParams.newBuilder(newUri, mimeType).build();
MessengerUtils.shareToMessenger(this, SHARE_TO_MESSENGER_REQUEST_CODE, shareToMessengerParams);
}
The thing is, I would like to be able to share from my asset folder a .mp3, a .jpg, a .png, to gmail, to whatsapp, to slack, or any type of app that supports this extension...
So as almost all the 1000 questions / articles online regarding sharing assets are answered by using a custom content provider, I tried the following sharing function:
public void shareBasic () {
// I added the test.jpg in the root of my asset folder
// Tried with content / file / 2 or 3 '/', with package name and with assets / ...
Uri theUri = Uri.parse("content:///com.MY_PACKAGE_NAME/test.jpg");
//Uri theUri = Uri.parse("content:///assets/test.jpg");
//Uri theUri = Uri.parse("file:///com.MY_PACKAGE_NAME/test.jpg");
//Uri theUri = Uri.parse("file:///asset.jpg");
Intent theIntent = new Intent(Intent.ACTION_SEND);
// Tried with jpeg / png / jpg ...
theIntent.setType("image/*");
theIntent.putExtra(Intent.EXTRA_STREAM, theUri);
theIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(theIntent);
}
And here is my Android manifest:
<provider
android:name="MY_PACKAGE_NAME.MyContentProvider"
android:authorities="MY_PACKAGE_NAME"
android:grantUriPermissions="true"
android:exported="true" />
And the file provider (almost the same on every tutorial)
public class MyContentProvider extends ContentProvider {
@Override
public boolean onCreate() {
return true;
}
@Override
public Cursor query(@NonNull Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
return null;
}
@Override
public Cursor query( Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder, CancellationSignal cancellationSignal )
{
// TODO: Implement this method
return super.query( uri, projection, selection, selectionArgs, sortOrder, cancellationSignal );
}
@Nullable
@Override
public String getType(@NonNull Uri uri) {
return null;
}
@Nullable
@Override
public Uri insert(@NonNull Uri uri, ContentValues values) {
return null;
}
@Override
public int delete(@NonNull Uri uri, String selection, String[] selectionArgs) {
return 0;
}
@Override
public int update(@NonNull Uri uri, ContentValues values, String selection, String[] selectionArgs) {
return 0;
}
@Override
public AssetFileDescriptor openAssetFile(Uri uri, String mode) throws FileNotFoundException {
AssetManager am = getContext().getAssets();
String fileName = uri.getLastPathSegment();
if(fileName == null)
throw new FileNotFoundException();
AssetFileDescriptor fileDescriptor = null;
try {
fileDescriptor = am.openFd(fileName);
} catch (IOException e) {
e.printStackTrace();
}
return fileDescriptor;
}
}
I guess I'm doing huge mistakes as sharing an file from asset folder shouldn't be such a struggle, any ideas ?