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
1 Answers
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

- 6,684
- 7
- 33
- 58
-
Do i need to import this library? Because here's a lot of Box library files – Toper Aug 07 '18 at 09:25
-
where do i have to set path to my file? in `loadUrl("path")` ? – Toper Aug 07 '18 at 12:07
-
You can do that in your `onCreate(...)` method of your desired Activity or on `onCreateView(...)` method of your desired Fragment. – Salman Khakwani Aug 08 '18 at 06:31
-
okey, i understand, but do i need to set path in `loadUrl()` method? – Toper Aug 08 '18 at 08:10
-
Well, webView can not open doxc files – Toper Aug 08 '18 at 12:57