1. Download our Official Android App: Forums for Android!

Can't get bitmap from cache

Discussion in 'Android Development' started by Onur Ozbek, Apr 15, 2019.

  1. Onur Ozbek

    Onur Ozbek Lurker
    Thread Starter
    Rank:
    None
    Points:
    15
    Posts:
    2
    Joined:
    Jul 4, 2017

    Jul 4, 2017
    2
    0
    15
    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 (Text):
    1. public void shareMeme(Bitmap bitmap) {
    2.     String path = Objects.requireNonNull(getContext()).getCacheDir().getAbsolutePath();
    3.     File file = new File(path + "/Memes/" + timeStamp + counter + ".jpg");
    4.     Uri uri = FileProvider.getUriForFile(getContext(), "com.example.omar.memegenerator.fileprovider", file);
    5.     try {
    6.         OutputStream stream = new FileOutputStream(file);
    7.         bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
    8.         stream.flush();
    9.         stream.close();
    10.     } catch (FileNotFoundException e) {
    11.         e.printStackTrace();
    12.     } catch (IOException e) {
    13.         e.printStackTrace();
    14.     }
    15.  
    16.     Intent share = new Intent(Intent.ACTION_SEND);
    17.     share.setType("image/*");
    18.     share.putExtra(Intent.EXTRA_STREAM, uri);
    19.     share.putExtra(Intent.EXTRA_TEXT, "This is my Meme");
    20.     share.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    21.     getContext().startActivity(Intent.createChooser(share, "Share Your Meme!"));
    22.  
    23.     Toast.makeText(getContext(), "The Cache drive is: " + path, Toast.LENGTH_LONG).show();
    24. }
    This is my res/xml/filepaths.xml file:

    Code (Text):
    1. <?xml version="1.0" encoding="utf-8"?>
    2. <paths xmlns:android="http://schemas.android.com/apk/res/android">
    3.     <cache-path name="shared_images" path="Memes/"/>
    4. </paths>
    And this is the manifest:

    Code (Text):
    1. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    2.     xmlns:tools="http://schemas.android.com/tools"
    3.     package="com.example.omar.memegenerator">
    4.     ...
    5.  
    6.     <application
    7.         ...
    8.         <provider
    9.             android:name="android.support.v4.content.FileProvider"
    10.             android:authorities="com.example.omar.memegenerator.fileprovider"
    11.             android:grantUriPermissions="true"
    12.             android:exported="false">
    13.             <meta-data
    14.                 android:name="android.support.FILE_PROVIDER_PATHS"
    15.                 android:resource="@xml/filepaths" />
    16.         </provider>
    17.         ...
    18.     </application>
    19.  
    20. </manifest>
     

    Advertisement

Share This Page

Loading...