• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Can't get bitmap from cache

Onur Ozbek

Lurker
I'm trying to share a bitmap from my cache drive to other social media apps. My goal is to allow the user to share their memes on social media without creating a copy of it in the Gallery app, as I have already implemented a saving feature for that. But when I call the shareMeme() method, social media apps are launched without the bitmap.

This is my shareMeme() method:

Code:
public void shareMeme(Bitmap bitmap) {
    String path = Objects.requireNonNull(getContext()).getCacheDir().getAbsolutePath();
    File file = new File(path + "/Memes/" + timeStamp + counter + ".jpg");
    Uri uri = FileProvider.getUriForFile(getContext(), "com.example.omar.memegenerator.fileprovider", file);
    try {
        OutputStream stream = new FileOutputStream(file);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
        stream.flush();
        stream.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    Intent share = new Intent(Intent.ACTION_SEND);
    share.setType("image/*");
    share.putExtra(Intent.EXTRA_STREAM, uri);
    share.putExtra(Intent.EXTRA_TEXT, "This is my Meme");
    share.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    getContext().startActivity(Intent.createChooser(share, "Share Your Meme!"));

    Toast.makeText(getContext(), "The Cache drive is: " + path, Toast.LENGTH_LONG).show();
}

This is my res/xml/filepaths.xml file:

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

And this is the manifest:

Code:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.example.omar.memegenerator">
    ...

    <application
        ...
        <provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="com.example.omar.memegenerator.fileprovider"
            android:grantUriPermissions="true"
            android:exported="false">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/filepaths" />
        </provider>
        ...
    </application>

</manifest>
 
Back
Top Bottom