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 7172003
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T15:31:34+00:00 2026-05-28T15:31:34+00:00

I created a custom View by extending from View. In onDraw() I managed to

  • 0

I created a custom View by extending from View. In onDraw() I managed to draw some circles and other stuff. But now I want to add a background from a resource (sd card or a stream) which is actually a map I download from our server and than draw on it. It’s for Android 8+

@Override
protected void onDraw(Canvas canvas) {
    Canvas g = canvas;
    String file = "/mnt/sdcard/download/tux.png";
    Bitmap bg = null;
    try {
        bg = BitmapFactory.decodeFile(file);
        g.setBitmap(bg);
    } catch (Exception e) {
        Log.d("MyGraphics", "setBitmap() failed according to debug");
    }
}

Somehow g.setBitmap(bg) keeps failing, I haven’t looked at the image specs, but actually it’s just a tux image (no 24 bits colors) of PNG format.
Can someone give me some tips how to add a background image so I can draw on it?
Thank you.

  • 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-28T15:31:35+00:00Added an answer on May 28, 2026 at 3:31 pm

    You actually don’t want to draw on to the bitmap you load, you just want to draw it on the Canvas, so you should use Canvas.drawBitmap(). You also really should not load a Bitmap in each onDraw(), do it in the constructor instead. Try this class:

    package com.example.android;
    
    import android.content.Context;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.graphics.Canvas;
    import android.util.AttributeSet;
    import android.view.View;
    
    public class CustomView extends View {
        private final Bitmap mBitmapFromSdcard;
    
        public CustomView(Context context) {
            this(context, null);
        }
    
        public CustomView(Context context, AttributeSet attrs) {
            super(context, attrs);
            mBitmapFromSdcard = BitmapFactory.decodeFile("/mnt/sdcard/download/tux.png");
        }
    
        @Override
        protected void onDraw(Canvas canvas) {
            Canvas g = canvas;
            if (mBitmapFromSdcard != null) {
                g.drawBitmap(mBitmapFromSdcard, 0, 0, null);
            }
        }
    }
    

    You can also let Android draw the bitmap in the background:

    package com.example.android;
    
    import android.content.Context;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.graphics.drawable.BitmapDrawable;
    import android.util.AttributeSet;
    import android.view.View;
    
    public class CustomView extends View {
        public CustomView(Context context) {
            this(context, null);
        }
    
        public CustomView(Context context, AttributeSet attrs) {
            super(context, attrs);
            Bitmap bm = BitmapFactory.decodeFile("/mnt/sdcard/download/tux.png");
            if (bm != null) {
                setBackgroundDrawable(new BitmapDrawable(bm));
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've created a custom view class and right now just want to draw an
I've created a custom view by extending View, it draws fine but When i
I've created a custom view that loads its content from a nib, like this:
I have created a custom view by extending Relative Layout and it looks like
I create a custom image view by extending ImageView that just draws some text
I have created one custom view that contains the horizontalscrollview. Now when i changes
I have created one custom view that contains the horizontalscrollview. Now when i changes
I have created a custom view by extending AutoCompleteTextView (with a specialized function called
I created a custom View Round button which consists of an image and some
I've created a custom view for a page called views-view--projects-landing-page.tpl.php and I want to

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.