2015-01-19 2 views
0

텍스트가있는 비트 맵을 만들었습니다. 이미지 뷰에서 볼 수 있지만 비트 맵을 저장할 때 검정색 이미지 만 나타납니다. 나는 비슷한 질문을 보면서 3 시간을 보냈지 만 나를 위해 일한 것은 하나도 없다. 여기에 코드가 있습니다. 도움 주셔서 감사합니다.저장된 비트 맵이 검정색입니다

public void createBitmap(){ 
    Bitmap LabelBitmap; 
    FileOutputStream fos = null; 
//create Text Bitmap 
    LabelBitmap = textAsBitmap(this,"BRO D 0813","fonts/arialbd.ttf", 4, Color.BLACK); 
//load bitmap in to Imageview 
    ImageView myImageView = (ImageView) findViewById(R.id.imageView); 
    myImageView.setImageBitmap(LabelBitmap); 
// save bitmap 
    String root = Environment.getExternalStorageDirectory().toString(); 
    File myDir = new File(root + "/myfolder"); 
    myDir.mkdirs(); 

    ByteArrayOutputStream bytes = new ByteArrayOutputStream(); 

    LabelBitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes); 
    if (!myDir.exists()) { 
     myDir.mkdir(); 
    } 

    File myDirFile = new File(root +"/myfolder/mybitmap.jpg"); 

    try { 
     if(myDirFile.exists()){ 
      myDirFile.delete(); 
     } 
     myDirFile.createNewFile(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

    try { 
     fos = new FileOutputStream(myDirFile); 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } 
    try { 
     fos.write(bytes.toByteArray()); 
     fos.flush(); 
     fos.close(); 
     Toast.makeText(this, "Image saved", Toast.LENGTH_SHORT).show(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

} 
+0

이 봐,이 말 http://stackoverflow.com/questions/8799290에 캔버스와 같은 결과를 사용하고/convert-string-text-to-bitmap – Yurets

답변

4

JPEG 이미지는 기본적으로 검은 색을 가지고, 그래서 당신의 텍스트 색상도 검은 색이면 당신은 검은 이미지를 얻을 것이다. 이미지에 배경색이없는 경우 PNG으로 저장해야합니다.

LabelBitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes); 

에 : : 다음과 시도가로 변경

LabelBitmap.compress(Bitmap.CompressFormat.PNG, 100, bytes); 
+0

고마워요. 또한 JPG로 저장하면 비트 맵 myCanvas.drawColor (Color.WHITE)를 만들 때이 줄을 추가해야합니다. –

관련 문제