view.getDrawingCache() returns null only in Samsung Galaxy note 8.0?
**Note : (For Nerds) Don't mark it as a duplicate question.
NullPointerException
is. 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..
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);
}
链接地址: http://www.djcxy.com/p/67632.html
上一篇: 将膨胀的布局转换为位图android
下一篇: view.getDrawingCache()仅在Samsung Galaxy note 8.0中返回null?