Android -- Displaying background WebView as a Bitmap - android

I would like to load a webview in the background and only display a bitmap of the page it loads. I have a webview in my view hierarchy that has visibility set to "invisible" and an imageview that I wish to display a bitmap of the webview.
imageView = (ImageView)findViewById(R.id.imageview);
webView = (WebView)findViewById(R.id.webview);
webView.setBackgroundColor(Color.parseColor("#006330"));
webView.setDrawingCacheEnabled(true);
webView.loadDataWithBaseURL(SiteAPI.URL, html, "text/html", "iso-8859-1", null);
webView.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
webView.layout(0, 0, webView.getMeasuredWidth(), webView.getMeasuredHeight());
Bitmap b = webView.getDrawingCache();
webView.setDrawingCacheEnabled(false);
if(b != null) {
Toast.makeText(getBaseContext(), "Not null", Toast.LENGTH_LONG).show();
imageView.setImageBitmap(Bitmap.createBitmap(b));
}
else {
Toast.makeText(getBaseContext(), "Null.", Toast.LENGTH_LONG).show();
}
The call to getDrawingCache() returns null everytime. I've tried setContentView(webView) and the page displays fine. I've made sure that isDrawingCacheEnabled() returns true, yet the bitmap is still null. Where am I going wrong?

You have two options:
Use webView.capturePicture() method, but note that this will return a picture of the whole webpage, so you have to crop it to display just the part that you want.
Manually draw your webView in the Canvas object you want, using webView.draw(Canvas).

Related

How to make docx preview in imageview android

I need to make preview of doxc file into my image view. I found such libraries as Docx4j , but i did not found any possibility of preview creating. What is the easiest way to create preview of doxc? Besides put it in webView
You can use the following code for generating a .docx or related files thumbnail while displaying them onto the Webview.
You can show a loader inside your ImageView and load the image using Picasso, Gilde, or other libraries.
You can also generate the thumbnail in a background task (that is recommended) with WeakReference of the ImageView object and then display that thumbnail in your designated ImageView.
Approach
Just take the snapshot of the Webview after loading the .docx file and display it in your designated ImageView.
Code
//Bitmap Utility
public final class BitmapUtil{
public static Bitmap loadBitmapFromView(View v, int width, int height) {
Bitmap b = Bitmap.createBitmap(width , height, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
v.layout(0, 0, v.getLayoutParams().width, v.getLayoutParams().height);
v.draw(c);
return b;
}
}
//Then You can call this in Your Activity/Fragment's 'onCreate(...)' or 'onCreateView(...)' method:
setContentView(R.layout.YOUR_LAYOUT_FILE);
w = findViewById(R.id.YOUR_WEBVIEW_ID);
w.setWebViewClient(new WebViewClient()
{
public void onPageFinished(WebView view, String url){
try {
Bitmap bmp = BitmapUtil.loadBitmapFromView(view, 100, 100);
YOUR_IMAGE_VIEW.setImageBitmap(bmp);
} catch( Exception e ) {
Log.e("YOUR_TAG", "Error occurred while taking snapshot of webview!, Error = "+e.toString());
}
}
});
w.loadUrl("http://search.yahoo.com/search?p=android");
Edit:
You can convert your .docx file to html using this example: https://github.com/orient33/androidDocxToHtml
Here is the sample activity:
https://github.com/orient33/androidDocxToHtml/blob/aaaaa/app/src/main/java/com/singuloid/docx2html/DocxShow.java
I hope this helps

How can i take screenshot of rotated TextView with emoji?

I'm trying to make a screenshot of the rotated TextView which contain emoji icons. But on resulting bitmap i see that emoji are not rotated! Why is this happening? How can i make a screenshot with rotated emoji ?
What i expect:
And this is what i get:
I'm using this method to get screenshot of view:
layout.setDrawingCacheEnabled(true);
layout.buildDrawingCache();
Bitmap bitmap = null;
if (layout.getDrawingCache() != null)
bitmap = layout.getDrawingCache().copy(Bitmap.Config.ARGB_8888, false);
layout.setDrawingCacheEnabled(false);
layout.destroyDrawingCache();
Update:
as i figured, if I set textView.setLayerType(View.LAYER_TYPE_SOFTWARE, null); then emoji will not be rotated even in the TextView (if you rotate TextView - they will not be rotated, they will be just moving around like a carousel), but still I don't really understand why is this happening, or rotation of emoji (on first picture) is only because of hardware acceleration?
ok, i couldnt find a way to solve this really annoying issue, so i had to hack it a bit.
imageviews works perfectly with rotation.
so i basically do all the manipulations with image view - and setting it's image out of the emoji text i want using this method:
private Bitmap generateBitmapFromText(int size, String res) {
TextView tv = new TextView(getContext());
tv.setText(res);
tv.setTextSize(TypedValue.COMPLEX_UNIT_PX, 150f);
tv.setTextColor(0xffffffff);
tv.measure(size, size);
int questionWidth = tv.getMeasuredWidth();
int questionHeight = tv.getMeasuredHeight();
Bitmap bitmap = Bitmap.createBitmap(questionWidth, questionHeight, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(bitmap);
tv.layout(0, 0, questionWidth, questionHeight);
tv.draw(c);
return bitmap;
}
and then i call
Bitmap bitmap = generateBitmapFromText(stickersStartingSize, res);
imageview.setImageBitmap(bitmap);

Convert ExpandableListview to bitmap

I am unable to get bitmap of expandable listview
This is what I tried
private Bitmap convertViewToBitMap() {
View printlayout = (View) getActivity().findViewById(R.id.expandableList);
printlayout.setDrawingCacheEnabled(true);
printlayout.measure(
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
printlayout.layout(0, 0, printlayout.getMeasuredWidth(),
printlayout.getMeasuredHeight());
printlayout.buildDrawingCache(true);
Bitmap b = Bitmap.createBitmap(printlayout.getDrawingCache());
printlayout.setDrawingCacheEnabled(false); // clear drawing cache
return b;
}
Following method helped me
private Bitmap convertViewToBitMap() {
View printlayout = (View) getActivity().findViewById(R.id.expandableList);
printlayout.setDrawingCacheEnabled(true);
Bitmap b = printlayout.getDrawingCache();
return b;
}
I think all you need it to get the hold of the view and convert that view into the bitmap and then print the bitmap.
You can get the view using findViewById();
For converting view into bitmap, a quick google may help more, but i found this
And once you have bitmap, do what ever you want.
#Pietu1998 pointed correctly, also show your attempt so reduce chance for downvoting.
You can re-draw your list into a bitmap which you can then use.
Here I'm issuing a re-draw on an instance variable mListView and then using that bitmap for my ImageView. Note that I'm using a Handler to force the re-draw on the UI thread.
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Bitmap bitmap = Bitmap.createBitmap(mListView.getWidth(),
mListView.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
mListView.draw(canvas);
// Do something the
((ImageView)mContext.findViewById(R.id.img)).setImageBitmap(bitmap);
}
}, 1);
Also note that you will need to have the ImageView inside of your xml already and the ListView not taking up all the space in the layout.

ImageView - getImageBitmap()?

I have an ImageView with visibility = View.VISIBLE and predefined width and height in XML.
When I set in my code ImageBitmap like
someImageView.setImageBitmap(bitmap);
how can I later on know is there some image bitmap already defined or it's still empty (null)?
So something like someImageView.getImageBitmap() == null ...I cannot test width, height or visibility because it's already predefined and it's the same thing if there is a bitmap or not.
Your imageView doesn't contain a bitmap if:
imageView.getDrawable() == null
Try this:
try{
bitmap = ((BitmapDrawable) someImageView.getDrawable()).getBitmap();
//re-use bitmap somehow?
}
catch(NullPointerException e){
//Bitmap dont exists
}

How to capture image of any layout in background

I want to send graphs in email. So for that I have captured bitmap of that graph and save it in sdcard as image. that works successfully
private Bitmap TakeImage(View v) {
Bitmap screen = null;
try {
v.setDrawingCacheEnabled(true);
v.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
v.buildDrawingCache(true);
screen = v.getDrawingCache();
v.setDrawingCacheEnabled(false); // clear drawing cache
} catch (Exception e) {
e.printStackTrace();
}
return screen;
}
But now I want to capture that in background without showing that layout, without notify by user. My previous code does not work for that.
Inflate the view using LayoutInflater, populate the values and pass it on to the same takeImage(View v) method.
I'm not sure, and I've not tried, don't know whether this works or not. but give it a try.
Have you tried changing v.getMeasuredWidth(), v.getMeasuredHeight() to whatever values you desire?
can you use
view.getLocationOnScreen(l);

Categories

Resources