1

**Note : (For Nerds) Don't mark it as a duplicate question.

  1. Yes I know what's a NullPointerException is.
  2. yes I have tried view.getDrawingCache returns null answers from Stackoverflow.

I am trying to develop a drawing application for tablets alone. I have created a custom view which is used for drawing. The custom view is included inside an ScrollView to make the user scroll image if it's too large.

activity_drawing.xml

 <RelativeLayout
            android:id="@+id/container"
            android:layout_marginLeft="@dimen/activity_correction_answer_horizontal_margin"
            android:layout_marginRight="@dimen/activity_correction_answer_horizontal_margin"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <in.com.hourglass.views.ImageScroll   
                android:layout_centerInParent="true"
                android:id="@+id/image_scroll"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <FrameLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content">

                    <ImageView
                        android:id="@+id/image"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content" />

                    <in.com.hourglass.views.DrawView
                        android:id="@+id/draw_view"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content" />
                </FrameLayout>
            </in.com.hourglass.views.ImageScroll>
  </RelativeLayout> 

so when the drawing over the view was done, the user can save the image and thinks drawn on Canvas.

I am resizing the image and attach it to custom view as Bitmap and save them as a single image. This whole process works fine Samsung Tablet 10.1.

But when I tried testing the same in lower screen device samsung Note 8.0 . view.getDrawingCache() is giving me a null Bitmap.

I have debugged and checked the width and height of the customview, scrollview they have specified width and height, and there is a bitmap present in customview as well.

I reuse Bitmap whenever possible to reduce app heap memory usage.

I know, i have to do something with this warning :

W/View: View too large to fit into drawing cache, needs 4380332 bytes, only 4096000 available

Error Log:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: in.com.hourglassPID: 10456
java.lang.NullPointerException: Attempt to invoke virtual method 'int android.graphics.Bitmap.getWidth()' on a null object reference
at android.graphics.Bitmap.createBitmap(Bitmap.java:614)
at in.com.hourglass.views.DrawView.getCorrectedBitmapWithoutResizing(DrawView.java:449)
at in.com.hourglass.activities.DrawingActivity.saveAnswerSheet(DrawingActivity.java:842)
at in.com.hourglass.activities.DrawingActivity.navigateNext(DrawingActivity.java:426)
at in.com.hourglass.activities.DrawingActivity.access$500(DrawingActivity.java:89)
at in.com.hourglass.activities.DrawingActivity$ToolIconClick.onClick(DrawingActivity.java:362)
at android.view.View.performClick(View.java:4789)
at android.view.View$PerformClick.run(View.java:19881)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5294)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)

Code Used to get Resized Bitmap

       public class DrawView extends View {

        public void setBitmap(Bitmap bitmap) {
            this.bitmap = bitmap;
            invalidate();
        }
  @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int width = MeasureSpec.getSize(widthMeasureSpec);
        int height = MeasureSpec.getSize(heightMeasureSpec);
        setMeasuredDimension(width, height);
    }




          public Bitmap getResizedBitmap(int originial_width, int original_height) {
                this.setDrawingCacheEnabled(true);
                Bitmap bitmap = Bitmap.createBitmap(this.getDrawingCache());
                 return  Bitmap.createScaledBitmap(bitmap,originial_width_answersheet,original_height_answersheet,false);
            }
        }

Links I have referred and unsuccessful,

Android View.getDrawingCache returns null, only null

Call to getDrawingCache returns null when scroll is enabled

etc..

6
  • I have a drawing view like you and i am using this.setDrawingCacheEnabled(true); in my Constructor : public DrawingView(Context c,AttributeSet attrs) did you tried like that. Jul 18, 2016 at 13:01
  • tried , that doesn't help either @YasinKaçmaz
    – HourGlass
    Jul 18, 2016 at 13:09
  • where are you attaching your Bitmap to Canvas , in onSizeChanged ? Jul 18, 2016 at 13:12
  • nope in public method liket this public void setBitmap(Bitmap bitmap) { this.bitmap = bitmap; invalidate();} @YasinKaçmaz
    – HourGlass
    Jul 18, 2016 at 13:19
  • can you add more code and i will compare yours to mine . Especially in my onSizeChanged method i am crating a new scaled bitmap and attaching it to my canvas like : mCanvas = new Canvas(mBitmap); Jul 18, 2016 at 13:23

1 Answer 1

8

As per your explanation, it seems like large images are not draw by the code, so for that please use below code it it is work,

I have not tested yet but I think this will help you

   public static Bitmap loadLargeBitmapFromView(View v) 
   {
      Bitmap b = Bitmap.createBitmap(v.getWidth(), v.getHeight(), Bitmap.Config.ARGB_8888);

      Canvas c = new Canvas(b);
      v.layout(0, 0, v.getLayoutParams().width, v.getLayoutParams().height);
      v.draw(c);
      return b;
   } 

Edit your method as below

     public Bitmap getResizedBitmap(int originial_width, int original_height) {
                this.setDrawingCacheEnabled(true);
     Bitmap bitmap = null;
    if(this.getDrawingCache()==null)
        {
      bitmap = loadLargeBitmapFromView(this);
     }
    else
    {
     bitmap = Bitmap.createBitmap(this.getDrawingCache());
       }

   return  Bitmap.createScaledBitmap(bitmap,originial_width_answersheet,original_height_answersheet,false);
            }
3
  • that worked, thanks. any idea why I am getting low memory in samsung Tab 8.0, but it works fine in other tablets.
    – HourGlass
    Jul 19, 2016 at 5:42
  • @HourGlass nice , im late . Also you can take a look at FingerPaint class from Api Demos : FingerPaint.java Jul 19, 2016 at 9:45
  • haha okay man. Will definitely have a look at fingerPaint . thanks u .@YasinKaçmaz
    – HourGlass
    Jul 19, 2016 at 11:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.