2014-01-20 148 views
1

在這些日子裏,圖片讓我感到緊張。現在,保存圖像變成黑色。我用豐富多彩的照片保存了一張照片,但是當保存時,它只能獲得全黑色。我不知道我的問題是什麼。給我一些幫助。保存時出現黑色圖像?

這是我的代碼。

BitmapFactory.Options options = new BitmapFactory.Options(); 
    options.inPreferredConfig = Bitmap.Config.ARGB_8888; 
    Bitmap myBitmap = BitmapFactory.decodeFile(picturePath,options); 
    ByteArrayOutputStream bao= new ByteArrayOutputStream(); 
    myBitmap.compress(Bitmap.CompressFormat.PNG,100, bao); 
    byte [] ba = bao.toByteArray(); 
    imageSave = Base64.encodeToString(ba, Base64.DEFAULT); 

看起來就是這樣。

回答

0

我明白了。這是我的答案。

public byte[] getBytesFromBitmap(Bitmap bitmap) { 
     ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
     bitmap.compress(CompressFormat.PNG, 70, stream); 
     return stream.toByteArray(); 
    } 

    BitmapFactory.Options options = new BitmapFactory.Options(); 
    options.inPreferredConfig = Bitmap.Config.ARGB_8888; 
    Bitmap myBitmap = BitmapFactory.decodeFile(picturePath,options); 
    imageSave = Base64.encodeToString(getBytesFromBitmap(myBitmap), Base64.NO_WRAP); 
0
ByteArrayOutputStream bytes = new ByteArrayOutputStream(); 

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

    File file = new File(Environment.getExternalStorageDirectory() 
      + File.separator + "myImage.jpg"); 
    try { 
     file.createNewFile(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

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