Sharing image via android studio apps and that image stored in firebaseStorage

34 views Asked by At

The image stored in FirbaseStorage, and its link stored in FirebaseDatarealtime which look like this:

(https://i.stack.imgur.com/AsCrC.png)

then showed the image to user using Glide and when the user click on the image a new intent created for sharing the image and the code gose like this:

imageUrl = the image url stored in Firebase Datarealtime

StorageReference storageRef = FirebaseStorage.getInstance().getReferenceFromUrl(imageUrl);
storageRef.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
    @Override
    public void onSuccess(Uri uri) {
        String downloadUrl = uri.toString();
        shareImage(downloadUrl);
    }
})
.addOnFailureListener(new OnFailureListener() {
    @Override
    public void onFailure(@NonNull Exception exception) {
        // Handle any errors
    }
});
private void shareImage(String downloadUrl) {
    Intent shareIntent = new Intent(Intent.ACTION_SEND);
    shareIntent.setType("image/png");
    shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(downloadUrl));
    shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    startActivity(Intent.createChooser(shareIntent, "Share Image"));
}

The resule : " The File formate is not supported "

I want to be able to share these images which stored in FirebaseStorage by grabing its links from Firebase Datarealtime, and share them in social apps like whatsapp, facebook app, etc.

1

There are 1 answers

1
SHAHEEN BSV On

It looks like you're almost there! The error "The File format is not supported" typically occurs when the URI you're trying to share is not in the correct format for the sharing intent. Instead of using Uri.parse(downloadUrl), you should create a FileProvider URI for the file and use that in the sharing intent. Here's how you can modify your code to achieve this:

First, make sure you have a FileProvider configured in your AndroidManifest.xml:

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

Then, create a file_paths.xml file in your res/xml directory with the following content:

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

Next, modify your code to use FileProvider.getUriForFile():

private void shareImage(String downloadUrl) {
    FirebaseStorage storage = FirebaseStorage.getInstance();
    StorageReference storageRef = storage.getReferenceFromUrl(downloadUrl);

    try {
        final File localFile = File.createTempFile("images", "jpg");
        storageRef.getFile(localFile).addOnSuccessListener(taskSnapshot -> {
            Uri fileUri = FileProvider.getUriForFile(MainActivity.this,
                    BuildConfig.APPLICATION_ID + ".fileprovider", localFile);

            Intent shareIntent = new Intent(Intent.ACTION_SEND);
            shareIntent.setType("image/*");
            shareIntent.putExtra(Intent.EXTRA_STREAM, fileUri);
            shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            startActivity(Intent.createChooser(shareIntent, "Share Image"));
        }).addOnFailureListener(exception -> {
            // Handle any errors
            Log.e("TAG", "Failed to download file: " + exception.getMessage());
        });
    } catch (IOException e) {
        e.printStackTrace();
    }
}

This code will download the image file to a temporary location and create a FileProvider URI for sharing. Note that this approach will download the image every time you share it. If you want to avoid unnecessary downloads, you can implement caching logic to store the images locally after the first download.