Share image files with other apps (Android programming)

We will develop a feature for sharing images to another application (such as Telegram and Facebook). The instructions will be provided in the Java programming language for Android.

Method 1: Use base64 image string:

private void shareImageBase64(Context context, String base64Image) {
    try {
        // Decode the Base64 string into a bitmap
        byte[] decodedBytes = android.util.Base64.decode(base64Image, android.util.Base64.DEFAULT);
        Bitmap decodedBitmap = BitmapFactory.decodeByteArray(decodedBytes, 0, decodedBytes.length);

        // Save the bitmap to a temporary file
        File cachePath = new File(context.getCacheDir(), "images");
        cachePath.mkdirs();
        File imageFile = new File(cachePath, "shared_image.png");
        FileOutputStream fos = new FileOutputStream(imageFile);
        decodedBitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
        fos.flush();
        fos.close();

        // Create an Intent to share the image
        Intent shareIntent = new Intent(Intent.ACTION_SEND);
        shareIntent.setType("image/*");
        Uri uriShare = FileProvider.getUriForFile(context, context.getPackageName() + ".provider", imageFile);
        shareIntent.putExtra(Intent.EXTRA_STREAM, uriShare);

        // Start an activity to allow the user to choose the app to share with
        Intent chooser = Intent.createChooser(shareIntent, "Share Image");
        List<ResolveInfo> resInfoList = context.getPackageManager().queryIntentActivities(chooser, PackageManager.MATCH_DEFAULT_ONLY);

        for (ResolveInfo resolveInfo : resInfoList) {
            String packageName = resolveInfo.activityInfo.packageName;
            context.grantUriPermission(packageName, uriShare, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
        }
        context.startActivity(chooser);
    } catch (IOException e) {
        e.printStackTrace();
        Toast.makeText(context, "Error sharing image", Toast.LENGTH_SHORT).show();
    }
}

Method 2: Use path file:

private void shareImageWithPath(Context context, String pathName) {
    // Add to AndroidManifest.xml file
    // For example, if you're sharing a file from external storage, you would add the following permission:
    // <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    File fileShare = new File(pathName);

    // Create an Intent to share the file
    Intent shareIntent = new Intent();
    shareIntent.setAction(Intent.ACTION_SEND);
    shareIntent.setType("image/*");
    Uri uriShare = Uri.fromFile(fileShare);
    shareIntent.putExtra(Intent.EXTRA_STREAM, uriShare);

    Intent chooser = Intent.createChooser(shareIntent, "Share Image");
    List<ResolveInfo> resInfoList = context.getPackageManager().queryIntentActivities(chooser, PackageManager.MATCH_DEFAULT_ONLY);

    for (ResolveInfo resolveInfo : resInfoList) {
        String packageName = resolveInfo.activityInfo.packageName;
        context.grantUriPermission(packageName, uriShare, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
    }
    // Start the share activity
    context.startActivity(chooser);
}

Next step:
Add to AndroidManifest.xml file, inside tag application

<provider
        android:name="androidx.core.content.FileProvider"
        android:authorities="${applicationId}.provider"
        android:exported="false"
        android:grantUriPermissions="true">
    <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/provider_paths" />
</provider>

Next step: Create file provider_paths.xml inside folder xml at /src/main/res/xml/provider_paths.xml

Paste to file provider_paths.xml code:

<?xml version="1.0" encoding="utf-8"?>
<paths>
    <external-files-path
        name="external_files"
        path="/" />
    <external-cache-path
        name="external_cache"
        path="/" />
    <external-media-path
        name="external_media"
        path="/" />
    <external-path
        name="external"
        path="/" />
    <cache-path
        name="cache"
        path="/" />
    <files-path
        name="files"
        path="/" />
     <external-path name="external_files" path="."/>
</paths>

Done, please build project!