Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7509761
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T23:00:59+00:00 2026-05-29T23:00:59+00:00

I’m trying to figure out how I can use Canvas to draw a small

  • 0

I’m trying to figure out how I can use Canvas to draw a small graphic (doesn’t really matter what it is) onto a large white surface. The issue is that if I start with a large empty Bitmap, when I make a mutable copy of it using ARGB_8888 Android immediately runs out of memory. I’m curious if I’m missing something, or if it’s actually not possible to composite a small graphic onto a large white surface and save it out as a PNG or JPG due to memory constraints in Android.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T23:01:00+00:00Added an answer on May 29, 2026 at 11:01 pm

    Naturally, you are limited by memory when you want to create huge bitmaps, but you have enough memory to create quite big bitmaps. For example, a 1024*1024 ARGB_8888 bitmap will need roughly 4 MB of memory, which is not a problem if your app is frugal with memory in general. The normal heap size for an Android app is usually between 16-32 MB depending on Android version, just to give you a feeling for what you have to play with.

    You say you make a copy of large bitmap, and that might be your main problem. There’s no need to make a copy of a large bitmap, you need only one. Here’s a sample project that creates a large (1024*1024) white bitmap and draws a View in your app in the middle of it, and then writes the result to a PNG:

    package com.example.android;
    
    import android.app.Activity;
    import android.graphics.Bitmap;
    import android.graphics.Canvas;
    import android.os.Bundle;
    import android.os.Environment;
    import android.util.Log;
    import android.view.View;
    
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    
    public class WhitePngActivity extends Activity {
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            findViewById(R.id.draw_to_bitmap).setOnClickListener(new View.OnClickListener() {
                public void onClick(View view) {
                    Bitmap largeWhiteBitmap = Bitmap.createBitmap(1024, 1024, Bitmap.Config.ARGB_8888);
    
                    // Make a canvas with which we can draw to the bitmap
                    Canvas canvas = new Canvas(largeWhiteBitmap);
    
                    // Fill with white
                    canvas.drawColor(0xffffffff);
    
                    // Draw the view to the middle of the big white bitmap. In this
                    // case, it will be the button, but you can draw any View in
                    // your view hierarchy to the bitmap like this. And of course
                    // you can position the View anywhere you want
                    canvas.save();
                    canvas.translate(
                            largeWhiteBitmap.getWidth() / 2 - view.getWidth() / 2,
                            largeWhiteBitmap.getHeight() / 2 - view.getHeight() / 2);
                    view.draw(canvas);
                    canvas.restore();
    
                    // Write the file (don't forget android.permission.WRITE_EXTERNAL_STORAGE)
                    File pictureDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
                    File pngFile = new File(pictureDir, "big-white-image-with-view.png");
                    try {
                        largeWhiteBitmap.compress(Bitmap.CompressFormat.PNG, 0, new FileOutputStream(pngFile));
                    } catch (FileNotFoundException e) {
                        Log.e("WhitePngActivity", "Could not write " + pngFile, e);
                    }
    
                    // Immediately release the bitmap memory to avoid OutOfMemory exception
                    largeWhiteBitmap.recycle();
                }
            });
        }
    }
    

    Together with this main layout:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
    
        <Button
            android:id="@+id/draw_to_bitmap"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Click to draw to bitmap" />
    
    </LinearLayout>
    

    You’ll get a bitmap somewhere like /mnt/sdcard/Pictures/big-white-image-with-view.png that looks something like this:

    enter image description here

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I am trying to understand how to use SyndicationItem to display feed which is
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I used javascript for loading a picture on my website depending on which small
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
I am confused How to use looping for Json response Array in another Array.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.