Android应用程序创建不完整的图像

时间:2013-07-27 02:49:35

标签: android file bitmap fileoutputstream

我正在开发一个结合了两个位图的应用程序,其中一个位图来自drawable,另一个位图来自相机快照。然而,图片总是不完整。一半的画面很好,但另一半是灰色的。有没有办法确保在应用程序继续使用代码之前完成文件?下面是用于编写和保存文件的代码。感谢

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

1 个答案:

答案 0 :(得分: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);

即可