0

I tried various methods after googling for hours. Some of the methods seem to help some people, but not for me. What am I missing here? Maybe a permission? I know this is a common bug or something, but there must be a solution.

All the methods are similar.

First:

GraphicalView v = ChartFactory.getBarChartView(ChartsDuration.this, buildBarDataset(titles, values), renderer, Type.DEFAULT); 
                    v.setDrawingCacheEnabled(true);
                    v.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
                    v.buildDrawingCache(true);
                    Bitmap bitmap = Bitmap.createBitmap(v.toBitmap()); //bitmap is null
                    v.setDrawingCacheEnabled(false);

or this:

                    Bitmap bitmap = Bitmap.createBitmap(v.getDrawingCache()); //bitmap is null

I also tried the solution here which is:

                    v.setDrawingCacheEnabled(true);

                 // this is the important code :)  
                 // Without it the view will have a dimension of 0,0 and the bitmap will be null          
                 v.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), 
                             MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
                 v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight()); 

                 v.buildDrawingCache(true);
                 Bitmap b = Bitmap.createBitmap(v.getDrawingCache());
                 v.setDrawingCacheEnabled(false); // clear drawing cache

Then the method on the bottom of this page:

v.setDrawingCacheEnabled(false);
                    if (!v.isDrawingCacheEnabled()) {
                      v.setDrawingCacheEnabled(true);
                    }
                    if (renderer.isApplyBackgroundColor()) {
                      v.setDrawingCacheBackgroundColor(renderer.getBackgroundColor());
                    }
                    v.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
                    v.getDrawingCache(true);
                    Bitmap b = Bitmap.createBitmap(v.getDrawingCache());

I also did not get any luck with this:

   v.setDrawingCacheEnabled(true);
                    v.requestFocus();
                    v.getRootView();


                    v.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
                    MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
                    v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());

                    v.buildDrawingCache(true);
                    Bitmap mBitmap = v.getDrawingCache();

The bitmaps are always null.

Community
  • 1
  • 1
erdomester
  • 11,789
  • 32
  • 132
  • 234
  • Please, did you solve it? If yes, what have to do? I have the same problem when I call ImageView.setImageBitmap(GraphicalView.toBitmap()). The imageview is invisible... However if i get an intent and launch the activity, chart is visible – BamsBamx May 15 '13 at 13:53
  • Was there a solution to this problem? – JanBo Aug 07 '13 at 13:37

1 Answers1

0

just an idea , not sure if it would work:

put the Achartengine view in another view (a frameLayout , for example) , and then capture it instead of the Achartengine view.

also , sure your code runs only after some time , that all of the views have drawn themselves? in order to check it , try getting the width or height of the view.

you also don't have any exceptions or special logs?

android developer
  • 114,585
  • 152
  • 739
  • 1,270