创建位图时OutOfMemory AsyncTask

时间:2013-11-01 21:11:58

标签: android pdf bitmap android-asynctask out-of-memory

我需要创建一个pdf页面上传到cloudinary。我找到了效果很好的PDFJet。 pdf包括一页或两页,每页上有一个图像。每个页面都包含一个WebView和一个在其上绘制的Canvas位图。为了将WebView变为位图,我要capturePicture()将其保存为图片,然后使用下面pictureDrawable2Bitmap的方法将其转换为位图。然后我拿画布变成位图。使用overlayBitmap方法将两者叠加到新的位图中。

然后对第二个位图重复此操作。位图被压缩并转换为字节数组,因此可以使用PDFJet的图像对象将它们添加到PDF中。我在创建Image image时获得OutOfMemory。我尝试回收每个Bitmap并在使用后删除pdf文件,但它没有帮助。此外,上传的png质量较低。

有关使用PDFJet从位图创建pdf的更好方法的任何建议吗?

private class CloudinaryTask extends AsyncTask <Bitmap, Void, Object> {

     ProgressDialog progressBar;

     @Override
    protected Object doInBackground (Bitmap...bmp) {            

    tStamp = String.valueOf(System.currentTimeMillis());

        // Scaling used to match canvas drawing to the webview
    Matrix matrix = new Matrix();
    matrix.postScale(1.5f, 1.5f);   

    // Get the contents of the webview and turn it into a bitmap
    Bitmap bmp1 = pictureDrawable2Bitmap(new PictureDrawable(picture));

    // Create a new bitmap from the canvas to match the webview contents
    Bitmap resizedBitmap = Bitmap.createBitmap(DrawingView.canvasBitmap, 0, 0, DrawingView.canvasBitmap.getWidth(), DrawingView.canvasBitmap.getHeight(), matrix, false);

    // Turn the new bitmap into an InputStream to upload to Cloudinary
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    overlayBitmap = overlayMark(bmp1, resizedBitmap);
    bmp1.recycle();
    resizedBitmap.recycle();
    overlayBitmap.compress(CompressFormat.PNG, 100, bos); 

    byte[] bitmapdata = bos.toByteArray();
    overlayBitmap.recycle();
    bs = new ByteArrayInputStream(bitmapdata);

    // If there's a 2nd page or if the user clicked 'flip form over', creates a bitmap of that image
    if (pagetwo || flipPage) {
        if (!flipPage) {
            Bitmap bmp2 = pictureDrawable2Bitmap(new PictureDrawable(picture2));
            ByteArrayOutputStream bos2 = new ByteArrayOutputStream();
            bmp2.compress(CompressFormat.PNG, 100, bos2); 
            bmp2.recycle();
            byte[] bitmapdata2 = bos2.toByteArray();
            bs2 = new ByteArrayInputStream(bitmapdata2);
        }
        else {
            Bitmap bmp2 = pictureDrawable2Bitmap(new PictureDrawable(picture2));
            System.out.println(bmp2.getWidth());
            System.out.println(bmp2.getWidth());
            Bitmap resizedBitmap2 = Bitmap.createBitmap(DrawingViewBack.canvasBitmap, 0, 0, DrawingViewBack.canvasBitmap.getWidth(), DrawingViewBack.canvasBitmap.getHeight(), matrix, false);
            overlayBitmap2 = overlayMark(bmp2, resizedBitmap2);
            bmp2.recycle();
            resizedBitmap.recycle();

            ByteArrayOutputStream bos2 = new ByteArrayOutputStream();
            overlayBitmap2.compress(CompressFormat.PNG, 100, bos2); 
            overlayBitmap2.recycle();
            byte[] bitmapdata2 = bos2.toByteArray();
            bs2 = new ByteArrayInputStream(bitmapdata2);
        }
    }

    // Creates pdf of one or both pages
    PDF pdf;
    File f = new File(Environment.getExternalStorageDirectory(), "offer.pdf");
    try {
        FileOutputStream out = new FileOutputStream(f);
        pdf = new PDF(out);
        float[] aspect = new float[2];
        // Set the height and width of each pdf page
        aspect[0] = (float) overlayBitmap.getWidth();
        aspect[1] = (float) overlayBitmap.getHeight();

        // Create the first page and set the first image
        Page page = new Page(pdf, aspect);
        Image image1 = new Image(pdf, bs, ImageType.PNG);
        image1.setPosition(0, 0);
        image1.drawOn(page);
        if (pagetwo || flipPage) {
            // Create the second page and set the 2nd image
            Page page2 = new Page(pdf, aspect);
            Image image2 = new Image(pdf, bs2, ImageType.PNG);
            image2.setPosition(0, 0);
            image2.drawOn(page2);
        }
        pdf.flush();

        // Turn the pdf file into an inputstream so cloudinary can use it
        FileInputStream fileInputStream=null;
        byte[] bFile = new byte[(int) f.length()];
        try {
            //convert file into array of bytes
            fileInputStream = new FileInputStream(f);
            fileInputStream.read(bFile);
            fileInputStream.close();
            f.delete();
        }catch(Exception e){
            e.printStackTrace();         
        }

        bs3 = new ByteArrayInputStream(bFile);

    } catch (FileNotFoundException e2) {
        // TODO Auto-generated catch block
        e2.printStackTrace();
    } catch (Exception e2) {
        // TODO Auto-generated catch block
        e2.printStackTrace();
    }

    // Create a map and add some required parameters for cloudinary
    Map<Object, Object> cloud = new HashMap<Object, Object>();
    cloud.put("cloud_name", "");
    cloud.put("api_key", "");
    cloud.put("api_secret", "");

    try {
        // Upload to cloudinary
        Cloudinary cloudinary = new Cloudinary(cloud);
        cloudinary.uploader().upload(bs3, Cloudinary.asMap("public_id", Network.imageUrl + tStamp));

0 个答案:

没有答案
相关问题