2012-08-13 73 views
0

當我保存和加載JPG圖片(背景爲白色)到外部存儲,它看起來像這樣:保存JPG格式提交混亂起來

enter image description here

代碼:

保存:

try { 
     outStream = new FileOutputStream(fileUri); 
     image.compress(Bitmap.CompressFormat.JPEG, 100, outStream); 
        outStream.flush(); 
      outStream.close(); 

    } catch (Exception e) { 
     Log.e("cache", e.getMessage()); 
     e.printStackTrace(); 
    } 

負載:

Bitmap bm = BitmapFactory.decodeFile(fileUri.toString()); 

我在做什麼錯?謝謝。

+0

,什麼是錯? – 2012-08-13 16:10:14

+1

原始圖像有白色背景,看起來不會混亂。 – Ixx 2012-08-13 16:11:43

+0

可能會幫助你'Canvas comboImage = new Canvas(image); \t \t comboImage.drawColor(Color.WHITE); \t \t comboImage.translate(0,0);' – 2012-08-13 16:23:32

回答

0

這是我使用的代碼(應爲你工作):

保存位圖:

public void writeBitmapToMemory(String filename, Bitmap bitmap) { 
     FileOutputStream fos; 
     // Use the compress method on the Bitmap object to write image to the OutputStream 
     try { 
      fos = game.openFileOutput(filename, Context.MODE_PRIVATE); 
      // Writing the bitmap to the output stream 
      bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos); 
      fos.close(); 

     } 
     catch (FileNotFoundException e) { 
      e.printStackTrace(); 


     } 
     catch (IOException e) { 
      e.printStackTrace(); 


     } 

    } 

,並閱讀:

public Bitmap readBitmapFromMemory(String filename) { 
     Bitmap defautBitmap = null; 
     File filePath = game.getFileStreamPath(filename); 
     FileInputStream fi; 
     try { 
      fi = new FileInputStream(filePath); 
      defautBitmap = BitmapFactory.decodeStream(fi); 

     } 
     catch (FileNotFoundException e) { 
      e.printStackTrace(); 

     } 

     return defautBitmap; 

    } 
+0

我看到我的代碼的唯一區別是,您使用'outputStream = context.openFileOutput(filename,Context.MODE_PRIVATE);'而不是'outputStream = new FileOutputStream(fileUri);',我不認爲這搞亂了位圖的像素數據。 – Ixx 2012-08-13 17:02:22

+0

它允許我讀取JPEG文件,不知道它是否會工作,如果是的話,但你應該試試看。 – 2012-08-13 17:19:22