【问题标题】:Android App Creates Incomplete ImagesAndroid 应用创建不完整的图像
【发布时间】:2013-07-27 02:49:34
【问题描述】:

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

结合.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

【问题讨论】:

    标签: android file bitmap fileoutputstream


    【解决方案1】:

    试试这个代码

        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);

    【讨论】:

    • 感谢您的建议。但是现在我得到了 OutOfMemoryError,堆大小=49187KB,分配=42138KB,限制=49152KB。这似乎发生在以“patt”开头的行
    • 你必须先 inSampleSize 位图,否则它会让你内存不足。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-11-03
    • 1970-01-01
    • 1970-01-01
    • 2012-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多