2

I am trying to save a graphView as an image. There are two things that are happening which are not expected. First thing is that the file is not getting created in sd card, Instead it is getting created in internal storage of the phone. Second is that the file created occupies 0B of memory. That is, it is an empty file. On opening it says "cannot generate thumbnail"

I am giving the code below. It was taken from an example from this website itself.

public class MainActivity extends AppCompatActivity {
LinearLayout ll;
Bitmap bitmap;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_mainn);
    final GraphView graphView = (GraphView) findViewById(R.id.graph);
    LineGraphSeries<DataPoint> series = new LineGraphSeries<DataPoint>(new DataPoint[]{
            new DataPoint(0, 1),
            new DataPoint(1, 5),
            new DataPoint(2, 3),
            new DataPoint(3, 2),
            new DataPoint(4, 6)
    });
    graphView.addSeries(series);


    RelativeLayout rl = (RelativeLayout) findViewById(R.id.myLayout);
    rl.setDrawingCacheEnabled(true);
    bitmap = rl.getDrawingCache();
    rl.setDrawingCacheEnabled(false);

    File file,f;
    if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))
    {
        file =new File(android.os.Environment.getExternalStorageDirectory(),"GRAPHS_PLOTTED");
        if(!file.exists())
        {
            file.mkdirs();

        }
        try{
            f = new File(file.getAbsolutePath()+file.separator+ "name"+".jpg");
            FileOutputStream ostream = new FileOutputStream(f);
           bitmap.compress(Bitmap.CompressFormat.PNG, 10, ostream);
            ostream.close();

        }
        catch (Exception e){
            e.printStackTrace();
        }
    }

}
}

Can anybody suggest some solutions??

sp9
  • 21
  • 5

1 Answers1

0

Try to put this Line at the end of the code. rl.setDrawingCacheEnabled(false);

when you used Compress type PNG. the Quality i.e 10 has been ignored.

Nitin Jain
  • 1,314
  • 11
  • 15
  • I believe that the problem is- image is formed even before graph is plotted. @ Nitin Jain ...I tried using JPEG instead of PNG....I tried putting "rl.setDrawingCacheEnabled(false);" at the end as you told..still no luck. – sp9 Dec 12 '16 at 06:03
  • ok if that was the case Try new Handler().postDelayed(new Runnable(){ callMethodToSaveImage(); }, 5000); – Nitin Jain Dec 12 '16 at 07:21
  • @ Nitin Jain ...Still No luck – sp9 Dec 13 '16 at 09:08