嗨,我有一个循环,它迭代以保存具有更改值的布局,对于每次迭代,xml 中的值都是不同的,并且应该在用户单击保存按钮时保存。
String [] sizes = {"A","B","C","D"};
for (int i = 0; i <= sizes.length-1 ; i++){
try {
String x = sizes[i];
Intent intents = new Intent();
intents.setClass(Sizes.this, Sizes.class);
Bundle bundles = new Bundle();
bundles.putCharSequence("sizes", x);
intents.putExtras(bundles);
startActivity(intents);
doSave();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
每种尺寸都会加载不同的值,我需要将循环中所有值的布局保存在 sdcard 上。我的doSave():
public void doSave() throws IOException, InterruptedException {
RelativeLayout imagevw =(RelativeLayout)findViewById(R.id.sizelayout);
Bitmap b = Bitmap.createBitmap(imagevw.getWidth(), imagevw.getHeight(), Bitmap.Config.ARGB_8888);
int weight,height;
weight = imagevw.getWidth();
height = imagevw.getHeight();
Bitmap cs = Bitmap.createBitmap(weight, height, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(cs);
c.drawBitmap(b,0,0,null);
imagevw.draw(c);
String getDirPath = Environment.getExternalStorageDirectory().getAbsolutePath();
File myDirPath = new File(getDirPath +"/Size");
if(!myDirPath.exists()){
myDirPath.mkdir();
}
File file1 = new File("/sdcard/size/"+size+"saved image.png");
FileOutputStream fos = new FileOutputStream(file1);
if (fos != null) {
cs.compress(Bitmap.CompressFormat.PNG, 90, fos);
fos.close();
}
}
当您保存单个布局时,它保存得很好。问题是当循环完成时,它只会在您单击保存按钮之前使用当前值保存当前布局。这意味着它只保存当前的布局。如何保存每个循环的布局,因为它们具有不同的值。请注意,在该实例中,“ size +”saved image.png”大小是一个捆绑包。