Android View.getDrawingCache returns null, only null
I was having this problem also and found this answer:
v.setDrawingCacheEnabled(true);
// this is the important code :)
// Without it the view will have a dimension of 0,0 and the bitmap will be null
v.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
v.buildDrawingCache(true);
Bitmap b = Bitmap.createBitmap(v.getDrawingCache());
v.setDrawingCacheEnabled(false); // clear drawing cache
Call to `getDrawingCache` returns null when scroll is enabled
I solved it. Created a bitmap of view size and drew the view into it.
TableLayout page = (TableLayout) findViewById(R.id.page);
Bitmap pageBmp = Bitmap.createBitmap(page.getWidth(), page.getHeight(),
Config.ARGB_8888);
Canvas canvas = new Canvas(pageBmp);
page.draw(canvas);
Used the pageBmp
bitmap..
view.getDrawingCache() returns null only in Samsung Galaxy note 8.0?
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);
}
getDrawingCache() always returns null
Similar question here more info here...
Problem appears to be that you can't access contents of a surfaceview - apparently you can capture the contents of an ImageView... I assume your using a surfaceView and thus encountering the same problem... If your willing to dig further this might help (from one of the above threads/topics)
If your not using a surfaceview then setLayerType might be your answer...
getDrawingCache() always returning null
Try calling buildDrawingCache()
before getDrawingCache()
EDIT:
Call getDrawingCache()
, after the page have loaded, instead of onCreate
view.getDrawingCache() only works once
To make it work more than once you have to use view.setDrawingCacheEnabled(true)
each time before and view.setDrawingCacheEnabled(false)
each time after calling view.getDrawingCache()
. See the example:
imageView.setDrawingCacheEnabled(true);
imageView.buildDrawingCache(true);
File imageFile = new File(Environment.getExternalStorageDirectory(),
"Pictures/image.jpg");
FileOutputStream fileOutputStream = new FileOutputStream(imageFile);
imageView.getDrawingCache(true).compress(CompressFormat.JPEG, 100,
fileOutputStream);
fileOutputStream.close();
imageView.setDrawingCacheEnabled(false);
Bitmap from TextView (getDrawingCache) always null
Do this before getting drawing cache it will solve the problem
view.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
and then getDrawingCache()
will return a bitmap and draw that bitmap on your canvas.
and if you are using bitmaps in your app prefer to clear them from memory by calling recycle() method on them so that bitmap get cleared from the memory for your safe side to avoid outOfMemoryException
Related Topics
Checkbox in Recyclerview Keeps on Checking Different Items
Android Error: Failed to Install *.Apk on Device *: Timeout
How to Open the "Front Camera" on the Android Platform
How to Customize Listview Using Baseadapter
Android - How to Investigate an Anr
Using Intents to Pass Data Between Activities
How to Remove Application from Recent Application List
Proper Implementation of Viewpager2 in Android
Android View.Getdrawingcache Returns Null, Only Null
Set Up Device For Development (? No Permissions)
How to Handle Dynamic Json in Retrofit
How to Pass a Value from One Activity to Another in Android
Nested Recycler View Height Doesn't Wrap Its Content