0

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

Toper
  • 263
  • 5
  • 17

1 Answers1

1

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

Salman Khakwani
  • 6,684
  • 7
  • 33
  • 58