jeudi 13 août 2015

Out of memory after some image creation

in my application I create some foto on button click where I take the screen view and thene I merge this image with 2 logo, the app with 5,10 image don't have problem but with more the 10 image I get an out of memory, this is the code:

    @Override
    public void onScreenshotImage(ImageStruct image) {
        //do whatever you want with the image parameter

        super.onScreenshotImage(image);

        Bitmap a = image.getBitmap();

        ResizeImage resize = new ResizeImage(a);
        resize.execute();

        Log.d("onScreenshot","get image");
    }

private class ResizeImage extends AsyncTask<String, Void, String> {
    Bitmap bottomImage;

    public ResizeImage (Bitmap image) {
        bottomImage = image;
    }

    @Override
    protected String doInBackground(String... params) {

        Bitmap output = Bitmap.createBitmap(bottomImage.getWidth(), bottomImage.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(output);
        Paint paint = new Paint();
        paint.setAntiAlias(true);

        canvas.drawBitmap(bottomImage, 0, 0, paint);

        Bitmap a = BitmapFactory.decodeResource(getResources(), R.drawable.logo);
        canvas.drawBitmap(a, 0, 0, paint);
        Bitmap b = BitmapFactory.decodeResource(getResources(), R.drawable.logo_1);
        canvas.drawBitmap(b, bottomImage.getWidth()-(b.getWidth()+20), bottomImage.getHeight()-(b.getHeight()+30), paint);

        String outputString = Environment.getExternalStorageDirectory().getPath() + "/images/";

        File folder_thumb= new File(outputString);
        if (!folder_thumb.exists()) {
            folder_thumb.mkdirs();
        }

        String tmpImg = String.valueOf(System.currentTimeMillis()) + ".png";
        OutputStream os = null;
        try {
            os = new FileOutputStream(outputString + tmpImg);
            output.compress(Bitmap.CompressFormat.PNG, 100, os);
            os.close();
        }
        catch(IOException e) {
            Log.e("combineImages", "problem saving images", e);
        }

        a.recycle();
        b.recycle();
        output.recycle();
        bottomImage.recycle();

        return "Executed";
    }

    @Override
    protected void onPostExecute(String result)  {
        System.gc();
        Runtime.getRuntime().gc();
    }
}

ps. the first funciton is the function for get the image in metaio. where is the mistake?

edit: I saw that waiting for the end of the task the memory does not exceed tot mb , while removing the block ( a simple boolean ) memory also goes to 100mb.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire