2014-02-14 84 views
0

我想將圖像導出到csv,但是每當嘗試這樣做時,我的應用程序都會崩潰。在我的應用程序中,我有一個問題:「您認識哪些品牌?」,並希望讓用戶通過選擇複選框以及相應的ImageView來回答問題。目標是通過單擊按鈕將問題(字符串)和答案(圖像)保存到csv或doc,但應用程序崩潰並且只有字符串打印在生成的csv中。無法將位圖導出到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

您可以從LogCat中添加崩潰詳細信息嗎? –

回答

1

由於CSV是文本格式,你必須每個位編碼成它自己的base64字符串,然後才能將它們插入到一個CSV文件。

這是一個online converter爲我正在談論的一個快速示例。

這是一個代碼示例,用於將encoding圖像轉換爲base64字符串。一旦你需要反序列化csv文件中的圖像,這裏是decoding一個base64字符串的代碼,回到它原來的二進制格式。