0

I'm trying to generate and share the image of a chart built with MPAndroidChart. I'm using a ShareIntent and this is the onCreateOptionsMenu method:

public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate menu resource file
    getMenuInflater().inflate(R.menu.share_menu, menu);

    // Locate share item with ShareActionProvider
    MenuItem shareItem = menu.findItem(R.id.item_share);

    // Fetch and store ShareActionProvider
    shareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(shareItem);

    Bitmap chartBitmap = chart.getChartBitmap();

    File imagesDirectory = new File(getFilesDir(), "images");

    if (!imagesDirectory.isDirectory() || !imagesDirectory.exists()) {
        imagesDirectory.mkdir();
    }

    File bitmapFile = new File(imagesDirectory, chartName.concat(".jpeg"));

    if (!bitmapFile.exists()) {
        try {
            bitmapFile.createNewFile();
        } catch (IOException e){
            e.printStackTrace();
        }
    }

    FileOutputStream outputStream = null;
    try {
        outputStream = new FileOutputStream(bitmapFile);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

    Uri contentUri = FileProvider.getUriForFile(getBaseContext(), "com.mycompany", bitmapFile);

    chartBitmap.compress(Bitmap.CompressFormat.JPEG, 20, outputStream);

    Intent shareImageIntent = new Intent(Intent.ACTION_SEND);
    shareImageIntent.setType("image/jpeg");
    shareImageIntent.putExtra(Intent.EXTRA_STREAM, contentUri);
    shareImageIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

    setShareIntent(shareImageIntent);

    // Return true to display menu
    return true;
}

Here I get the bitmap from the chart as recommended in the MPAndroidChart wiki

getChartBitmap(): Returns the Bitmap object that represents the chart, this Bitmap always contains the latest drawing state of the chart.

Bitmap chartBitmap = chart.getChartBitmap();

Then, as recommend in the Android documentation, I'm using a FileProvider to share the image; this is where I declare it in AndroidManifest.xml:

<provider
    android:authorities="com.mycompany"
    android:name="android.support.v4.content.FileProvider"
    android:exported="false"
    android:grantUriPermissions="true">

    <meta-data
        android:name="android.support.FILE_PROVIDER_PATHS"
        android:resource="@xml/images_path"/>
</provider>

And this is where I specify the directories containing the files I'll need to share

<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <files-path name="sharedImages" path="images/"/>
</paths>

This is the screen of the chart I'd like to share but this is the image that is actually shared.

Why is the line of the chart missing?

Michele
  • 326
  • 8
  • 18

2 Answers2

0

I don't know, but you can get bitmap from layout include chart, Ex:

view.setDrawingCacheEnabled(true)
view.buildDrawingCache()
Bitmap bm = view.getDrawingCache()
Cuong Nguyen
  • 970
  • 6
  • 17
0

I solved it! It was kind of my fault, but the library isn't perfect in this case. The problem was that I was calling getChartBitmap inside onCreateOptionsMenu, which is executed as soon as I navigate to that Activity.

MPAndroidChart lets you add an animation that slowly shows the chart; the animation was still going on while I called getChartBitmap, so I couldn't see the chart line because it hadn't been loaded yet.

I disabled the animation and now everything works fine. If someone knows a way to get an image with the chart line and keep the animation, please let me know.

Michele
  • 326
  • 8
  • 18