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?