我想将图像导出到csv,但只要尝试执行此操作,我的应用程序就会崩溃。在我的应用程序中,我有一个问题“您认识哪些品牌?”,并希望让用户通过选中一个复选框和相应的ImageView来回答。目标是通过单击按钮将问题(字符串)和答案(图像)保存到csv或doc,但应用程序崩溃,并且只在生成的csv中打印字符串。
public class Page3 extends Activity implements OnClickListener {
Intent myIntent;
TextView question4;
CheckBox one, two, three;
ImageView one1, two2, three3;
Bitmap bmp1, bmp2, bmp3;
String ques4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.page3);
initialize();
}
private void initialize() {
Button prev = (Button) findViewById(R.id.page3previous);
Button next = (Button) findViewById(R.id.page3next);
next.setOnClickListener(this);
prev.setOnClickListener(this);
question4 = (TextView) findViewById(R.id.question4tv);
one1 = (ImageView) findViewById(R.id.imageView1);
two2 = (ImageView) findViewById(R.id.imageView2);
three3 = (ImageView) findViewById(R.id.imageView3);
one = (CheckBox) findViewById(R.id.checkBox1);
two = (CheckBox) findViewById(R.id.checkBox2);
three = (CheckBox) findViewById(R.id.checkBox3);
}
@Override
public void onClick(View arg0) {
switch (arg0.getId()) {
case R.id.page3previous:
myIntent = new Intent(Page3.this, Page2.class);
startActivity(myIntent);
break;
case R.id.page3next:
ques4 = question4.getText().toString();
if (one.isChecked()) {
one1.buildDrawingCache();
bmp1 = one1.getDrawingCache();
setBitMapOneTwoThree(bmp1, bmp3, bmp2, ques4);
} else if (two.isChecked()) {
two2.buildDrawingCache();
bmp2 = two.getDrawingCache();
setBitMapOneTwoThree(bmp1, bmp3, bmp2, ques4);
} else if (three.isChecked()) {
three3.buildDrawingCache();
bmp3 = three3.getDrawingCache();
setBitMapOneTwoThree(bmp1, bmp3, bmp2, ques4);
} else if (two.isChecked() && one.isChecked()) {
one1.buildDrawingCache();
bmp1 = one1.getDrawingCache();
two2.buildDrawingCache();
bmp2 = two.getDrawingCache();
setBitMapOneTwoThree(bmp1, bmp3, bmp2, ques4);
} else if (two.isChecked() && three.isChecked()) {
two2.buildDrawingCache();
bmp2 = two.getDrawingCache();
three3.buildDrawingCache();
bmp3 = three3.getDrawingCache();
setBitMapOneTwoThree(bmp1, bmp3, bmp2, ques4);
} else if (one.isChecked() && three.isChecked()) {
three3.buildDrawingCache();
bmp3 = three3.getDrawingCache();
one1.buildDrawingCache();
bmp1 = one1.getDrawingCache();
setBitMapOneTwoThree(bmp1, bmp3, bmp2, ques4);
} else if (one.isChecked() && three.isChecked() && two.isChecked()) {
three3.buildDrawingCache();
bmp3 = three3.getDrawingCache();
two2.buildDrawingCache();
bmp2 = two.getDrawingCache();
one1.buildDrawingCache();
bmp1 = one1.getDrawingCache();
setBitMapOneTwoThree(bmp1, bmp3, bmp2, ques4);
}
myIntent = new Intent(Page3.this, Page4.class);
startActivity(myIntent);
break;
}
}
private void setBitMapOneTwoThree(Bitmap bmp12, Bitmap bmp32, Bitmap bmp22,
String ques42) {
bmp12 = this.bmp1;
bmp32 = this.bmp3;
bmp22 = this.bmp2;
ques42 = this.ques4;
generateCsvFile("Image.csv", bmp12, bmp32, bmp22, ques42);
}
private void generateCsvFile(String string, Bitmap bmp12, Bitmap bmp32,
Bitmap bmp22, String ques42) {
try {
File root = Environment.getExternalStorageDirectory();
File gpxfile = new File(root, string);
FileWriter writer = new FileWriter(gpxfile);
FileOutputStream fOut = new FileOutputStream(gpxfile);
writer.append(ques42);
writer.flush();
writer.close();
bmp12.compress(Bitmap.CompressFormat.PNG, 85, fOut);
bmp32.compress(Bitmap.CompressFormat.PNG, 85, fOut);
bmp22.compress(Bitmap.CompressFormat.PNG, 85, fOut);
fOut.flush();
fOut.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
答案 0 :(得分:1)
由于csv是一种文本格式,因此必须先将每个位图编码为自己的base64字符串,然后才能将它们插入到csv文件中。
这是一个online converter,可以快速举例说明我所谈论的内容。
以下是将encoding图像转换为base64字符串的代码示例。一旦你需要从csv文件反序列化图像,这里的decoding代码为base64字符串,回到原始的二进制格式。