【问题标题】:Trouble in saving GraphView as image将 GraphView 保存为图像时出现问题
【发布时间】:2016-12-08 05:34:43
【问题描述】:

我正在尝试将 graphView 保存为图像。 有两件事情正在发生,这是意料之外的。 首先是文件不是在 sd 卡中创建的,而是在手机的内部存储中创建的。 二是创建的文件占用0B内存。也就是说,它是一个空文件。打开时显示“无法生成缩略图”

我在下面给出代码。它取自本网站本身的示例。

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();
        }
    }

}
}

有人可以提出一些解决方案吗?

【问题讨论】:

    标签: android android-file android-graphview


    【解决方案1】:

    尝试将此行放在代码的末尾。 rl.setDrawingCacheEnabled(false);

    当您使用压缩类型 PNG 时。质量,即 10 已被忽略。

    【讨论】:

    • 我认为问题在于 - 图像甚至在绘制图表之前就已形成。 @Nitin Jain ...我尝试使用JPEG而不是PNG ....我尝试输入“rl.setDrawingCacheEnabled(false);”最后如你所说..仍然没有运气。
    • 好吧,如果是这种情况试试 new Handler().postDelayed(new Runnable(){ callMethodToSaveImage(); }, 5000);
    • @Nitin Jain ...仍然没有运气
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-24
    • 1970-01-01
    • 2020-03-22
    • 2019-05-26
    相关资源
    最近更新 更多