2013-07-27 59 views
1

我正在開發一個應用程序,它結合了兩個位圖,其中一個位圖來自drawable,另一個來自相機快照。但照片總是不完整。一半的圖片是好的,但另一半是灰色的。有沒有辦法確保該文件在應用程序繼續使用代碼之前完成?以下是編寫和保存文件的代碼。由於Android應用程序創建不完整的圖像

Combine.java

protected void createPostcard(byte[] data, File pictureFile, CameraActivity app, Button shareButton, 
           Button newButton) { 
    try { 
     Bitmap photo = BitmapFactory.decodeByteArray(data, 0, data.length); 
     Bitmap splash = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(app.getResources(), 
                R.drawable.wishsplash), photo.getWidth(), photo.getHeight(), false); 
     Bitmap postcard = Bitmap.createBitmap(photo.getWidth(), photo.getHeight(), photo.getConfig()); 
     Canvas canvas = new Canvas(postcard); 

     canvas.drawBitmap(photo, new Matrix(), null); 
     canvas.drawBitmap(splash, 0, 0, null); 
     savePostcard(postcard, pictureFile, app, shareButton, newButton); 
    } catch (Exception e) { 
    }//end catch 
}//end createPostcard 

/** 
* Saves the postcard 
*/ 
private void savePostcard(Bitmap postcard, File pictureFile, CameraActivity app, Button shareButton, 
          Button newButton) { 

     BitmapDrawable mBitmapDrawable = new BitmapDrawable(postcard); 
     Bitmap mNewSaving    = mBitmapDrawable.getBitmap(); 
     ByteArrayOutputStream stream = new ByteArrayOutputStream(); 

     mNewSaving.compress(CompressFormat.JPEG, 100, stream); 
     byte[] byteArray = stream.toByteArray(); 
     save(byteArray, pictureFile, app); 
     shareButton.setBackgroundResource(R.drawable.sharebutton); 
     newButton.setBackgroundResource(R.drawable.newbutton); 
     shareButton.setEnabled(true); 
     newButton.setEnabled(true); 
}//end savePostcard 


/** 
* Check if external is available. If not, postcard will be saved in internal. 
* @retun 
*/ 
private void save(byte[] data, File pictureFile, CameraActivity app) { 
    try { 
     if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { 
      FileOutputStream fos   = new FileOutputStream(pictureFile); 
      imageUri      = Uri.fromFile(pictureFile); 
      fos.write(data); 
      imageFile = pictureFile; 
      fos.close(); 
      app.sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, 
             Uri.parse("file://"+ Environment.getExternalStorageDirectory()))); 
     } else { 
      File cache   = app.getCacheDir(); 
      File internalPic  = new File(cache, pictureFile.getName()); 
      FileOutputStream fos = new FileOutputStream(internalPic); 
      imageUri    = Uri.fromFile(internalPic); 
      imageFile   = internalPic; 
      fos.write(data); 
      fos.close(); 
     }//end else 
    } catch (FileNotFoundException e) { 
     System.out.println("FILENOTFOUND"); 
    } catch (IOException e) { 
     System.out.println("IOEXCEPTION"); 
    }//end catch 
}//end getStorage 

回答

0

試試這個代碼

public Bitmap PutoverBmp(Bitmap all, Bitmap scaledBorder) { 
    Paint paint = new Paint(); 
    final int width = bmp.getWidth(); // bmp is your main Bitmap 
    final int height = bmp.getHeight(); 
    patt = Bitmap.createScaledBitmap(bmp, width, height, true); 
    Bitmap mutableBitmap = patt.copy(Bitmap.Config.ARGB_8888, true); 
    Canvas canvas = new Canvas(mutableBitmap); 
    scaledBorder = Bitmap.createScaledBitmap(border, width, height, true); 
    paint.setAlpha(100); 
    canvas.drawBitmap(scaledBorder, 0, 0, paint); 
    return mutableBitmap; 

} 

只需撥打這個Bitmap combine = (bmp , yourOtherBitmap);

+0

謝謝你的建議。但是現在我得到一個OutOfMemoryError,堆大小= 49187KB,分配= 42138KB,限制= 49152KB。這似乎發生在以'patt' – user1657178

+0

開頭的行上,您必須首先在SampleSample中創建位圖,否則會導致內存不足。 – k0sh

相關問題