The problem is that GraphView in recent version include additional conditions - if canvas in hardware accelerated mode or not:
GraphView.java (https://github.com/appsthatmatter/GraphView/commit/0740de3e0a93633f7f168115b96edfdce156b19e)
public class GraphView extends View {
...
protected void drawGraphElements(Canvas canvas) {
// must be in hardware accelerated mode
if (android.os.Build.VERSION.SDK_INT >= 11 && !canvas.isHardwareAccelerated()) {
throw new IllegalStateException("GraphView must be used in hardware accelerated mode." +
"You can use android:hardwareAccelerated=\"true\" on your activity. Read this for more info:" +
"https://developer.android.com/guide/topics/graphics/hardware-accel.html");
}
...
}
...
}
When you create off-screen canvas it doesn't have hardware accelerated mode. But you can just override isHardwareAccelerated for your Canvas instance. Use this workaround:
Bitmap bitmap = Bitmap.createBitmap(graphView.getWidth(), graphView.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap) {
@Override
public boolean isHardwareAccelerated() {
return true;
}
};
// not it's work
graphView.draw(canvas);
// save somewhere
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, new FileOutputStream(imagePath));