【发布时间】: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