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
Related
This question already has answers here:
Convert view to bitmap on Android
(9 answers)
Closed 6 years ago.
Ok, so I'll try my best to explain my problem.
I have used this code to get a "screenshot" of my FrameLayout
/**
* Function that takes a screenshot of the view passed and returns a bitmap for it.
*
* #param view {#link View}
* #return screenshot of the view passed
*/
public Bitmap screenShot(View view) {
Bitmap bitmap = Bitmap.createBitmap(view.getWidth(),
view.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
view.draw(canvas);
return bitmap;
}
Here is my FrameLayout
<FrameLayout
android:id="#+id/frame_layout_picture"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/photoImageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitCenter" />
<com.my.android.util.DrawCustomView
android:id="#+id/draw_custom_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
DrawCustomView is used to paint on photoImageView.
Now I also have different activity to crop image. So, I saved this bitmap (using screenshot(view)) in a file and send the file path to this crop activity. In my crop activity I am using https://github.com/ArthurHub/Android-Image-Cropper library's CropImageView to crop the image.
When I try to crop the painted photo sent to crop activity I get a weird crop, something like this :
The image doubles on cropping too much :
https://drive.google.com/open?id=0B4jK5QX65b0ZSHdMTWIzZXluZXc
Before zooming in :
https://drive.google.com/open?id=0B4jK5QX65b0ZeUk3NUlGUHRSQ0E
This is how a normal picture appears in crop activity :
https://drive.google.com/open?id=0B4jK5QX65b0ZNk9oSG1ZaGxYOVk
I also notice that the bounds for cropping are restricted to image size in the normal case but when edited using screenshot(), the bounds take the size of the screen.
I have tested my crop activity beforehand and it works fine for images sent from camera.
I also tried to send a picture that is not obtained using screenshot function, and it worked fine in crop activity.
I'll be happy to provide more info, any help is much appreciated... I am relatively new to android development :)
EDIT
This video might give more perspective to the problem
https://drive.google.com/open?id=0B4jK5QX65b0ZSUVFZDlQeTc4QnM
public static Bitmap shot(View view) {
view.setDrawingCacheEnabled(true);
Bitmap bitmap = view.getDrawingCache();
view.setDrawingCacheEnabled(false);
return bitmap;
}
You can try this method to capture a screenshot of your view.
/**
* this method capture the image from provided view and save it to application internal directory.
*
* #param view to capture image.
* #return path of image saved in the phone.
*/
public static String captureScreenShotAndSave(Context mContext, View view) {
view.setBackgroundColor(Color.WHITE);
String mPath = mContext.getApplicationContext().getExternalCacheDir() + "/" + "IMG_" + DateTimeUtils.getCurrentTimeStamp() + "_" + SHARE_IMAGE_NAME;
Bitmap bitmap = Bitmap.createBitmap(view.getMeasuredWidth(), view.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(bitmap);
view.draw(c);
c.drawBitmap(bitmap, 0, 0, null);
OutputStream fout = null;
File imageFile = new File(mPath);
try {
fout = new FileOutputStream(imageFile);
bitmap.compress(Bitmap.CompressFormat.JPEG, 80, fout);//set image quality and formate as you required.
fout.flush();
fout.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return imageFile.getAbsolutePath();
}
You can call this method like YourUtilsClass.captureScreenShotAndSave(mContext, mViewToCapture);
Hello I am trying to create PDF from webview in Crosswalk but I didn't find any supporting methods how to do it. Currently I am using this option to create bitmap
How can I capture the whole view into a Bitmap when using crosswalk to display webpage?
and then I try to convert the bitmap using itext lib
How I can convert a bitmap into PDF format in android as output I get blank pdf (black page).
Here is the code for bitmap:
private void createBitmap (XWalkView view) {
String pathToFile = Environment.getExternalStorageDirectory() +"/bitmap.jpg";
view.setDrawingCacheEnabled(true);
view.buildDrawingCache(true);
Bitmap btmp = view.getDrawingCache();
Canvas canvas = new Canvas(btmp);
Paint paint = new Paint ();
int height = btmp.getHeight();
canvas.drawBitmap(btmp, 0, height, paint);
view.draw(canvas);
try {
OutputStream fOut = null;
File file = new File(pathToFile);
fOut = new FileOutputStream(file);
btmp.compress(Bitmap.CompressFormat.JPEG, 50, fOut);
fOut.flush();
fOut.close();
btmp.recycle();
view.setDrawingCacheEnabled(false);
view.destroyDrawingCache();
} catch (Exception e) {
e.printStackTrace();
}
}
I need just hint :). Thanks a lot. This is my first question I apologize for typos
XWalkView use a standalone SurfaceView(or TextureView) as its output target.The draw or onDraw of XWalkView draws nothing. So you can not rely on it to draw something into a pdf file.
As a workaround, there is another api XWalkView.captureBitmapAsync which can be used to capture the visible webpage into a bitmap. Here is a simple guide about how to use it:
1), Implement XWalkGetBitmapCallback
import org.xwalk.core.XWalkGetBitmapCallback;
class XWalkGetBitmapCallbackImpl extends XWalkGetBitmapCallback {
public XWalkGetBitmapCallbackImpl() {
super();
}
//Note: onFinishGetBitmap happens at the same thread as captureBitmapAsync, usually the UI thread.
#Override
public void onFinishGetBitmap(Bitmap bitmap, int response) {
//if response == 0, save this bitmap into a jpg file //otherwise errors.
}
}
2), Start the capture in UI thread:
private void captureContent() {
if ( xWalkView == null) return;
mXWalkGetBitmapCallback = new XWalkGetBitmapCallbackImpl();
xWalkView.captureBitmapAsync(mXWalkGetBitmapCallback);
}
More details is here: https://crosswalk-project.org/jira/browse/XWALK-5110
Everything I tried with setDrawingCacheEnabled and getDrawingCache was not working. The system was making an image but it just looked black.
Other people on SO seemed to be having a similar problem but the answers seemed either too complicated or irrelevant to my situation. Here are some of the ones I looked at:
Save view like bitmap, I only get black screen
Screenshot shows black
getDrawingCache always returns the same Bitmap
Convert view to bitmap on Android
bitmap is not saving properly only black image
Custom view converting to bitmap returning black image
And here is my code:
view.setDrawingCacheEnabled(true);
Bitmap bitmap = view.getDrawingCache();
try {
FileOutputStream stream = new FileOutputStream(getApplicationContext().getCacheDir() + "/image.jpg");
bitmap.compress(CompressFormat.JPEG, 80, stream);
stream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
view.setDrawingCacheEnabled(false);
I'm sharing my answer below in case anyone else makes the same mistake I did.
My problem was that my view was a TextView. The text on the TextView was black (naturally) and in the app the background looked white. However, I later recalled reading that a view's background is by default transparent so that whatever color is below shows through.
So I added android:background="#color/white" to the layout xml for the view and it worked. When I had been viewing the image before I had been looking at black text over a black background.
See the answer by #BraisGabin for an alternate way that does not require overdrawing the UI.
I just found a good option:
final boolean cachePreviousState = view.isDrawingCacheEnabled();
final int backgroundPreviousColor = view.getDrawingCacheBackgroundColor();
view.setDrawingCacheEnabled(true);
view.setDrawingCacheBackgroundColor(0xfffafafa);
final Bitmap bitmap = view.getDrawingCache();
view.setDrawingCacheBackgroundColor(backgroundPreviousColor);
bitmap.compress(Bitmap.CompressFormat.JPEG, 80, stream);
view.setDrawingCacheEnabled(cachePreviousState);
Where 0xfffafafa is the desired background color.
Used below code to get bitmap image for view it work fine.
public Bitmap loadBitmapFromView(View v) {
DisplayMetrics dm = getResources().getDisplayMetrics();
v.measure(View.MeasureSpec.makeMeasureSpec(dm.widthPixels,
View.MeasureSpec.EXACTLY),
View.MeasureSpec.makeMeasureSpec(dm.heightPixels,
View.MeasureSpec.EXACTLY));
v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
Bitmap returnedBitmap =
Bitmap.createBitmap(v.getMeasuredWidth(),
v.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(returnedBitmap);
v.draw(c);
return returnedBitmap;
}
I am using the following code to get a Bitmap image of the html webpage that is loaded in WebView, however getting crashes due to running out of memory. how do i avoid memory problems if it is directly converting the picturedrawable into a Bitmap.
Is there a way to load file directly into a compressed format lke jpg? or is there some other way to handle this problem?
using Bitmapfactory to downsize the image does not look like it is possible, because the Bitmap is already created so it would be too late to do anything about it.
wv = (WebView)findViewById(R.id.webView1);
wv.loadUrl("http://www.google.com");
WebSettings webSettings = wv.getSettings();
webSettings.setJavaScriptEnabled(true);
Picture picture = wv.capturePicture();
HTMLBitmap = pictureDrawable2Bitmap(picture);
Toast.makeText(HTMLActivity.this, "bitmap " + HTMLBitmap.toString(), Toast.LENGTH_LONG).show();
Bitmap bmp = Bitmap.createScaledBitmap(HTMLBitmap, 500, 300, false);
// the imageView that is being set to the bitmap
imageOne.setImageBitmap(bmp);
private static Bitmap pictureDrawable2Bitmap(Picture picture){
PictureDrawable pictureDrawable = new PictureDrawable(picture);
Bitmap bitmap = Bitmap.createBitmap(pictureDrawable.getIntrinsicWidth(),pictureDrawable.getIntrinsicHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
canvas.drawPicture(pictureDrawable.getPicture());
return bitmap;
}
EDIT:
two possible solutions
1
one idea I just thought of is somehow creating the Image on external memory like the SD card and using BitmapFactory to drop the size if it is large, before putting on the internal memory, how to actually do that is another question. the bitmap would have to be originally created on the external memory.
2
or is the memory problem only caused by loading it into an imageView. In that case I can drop the image with BitmapFactory Options before loading it in the imageView.
Bitmap bmp = Bitmap.createScaledBitmap(HTMLBitmap, 500, 300, false);
I'm trying to implement the css3 page flip effect on a android/phonegap app. To do this, I need to dynamically save the current webview to png or jpeg so that it can be loaded to a div in the page flip html. I noticed the Picture class in android's docs but I'm not sure if that can be converted and saved. Could this be done through JS? Any ideas?
thanks
Try following code for capturing webview and saved jpg to sdcard.
webview.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
Picture picture = view.capturePicture();
Bitmap b = Bitmap.createBitmap(
picture.getWidth(), picture.getHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
picture.draw(c);
FileOutputStream fos = null;
try {
fos = new FileOutputStream( "/sdcard/" + "page.jpg" );
if ( fos != null ) {
b.compress(Bitmap.CompressFormat.JPEG, 90, fos );
fos.close();
}
}
catch( Exception e ) {
System.out.println("-----error--"+e);
}
}
});
webview.loadUrl("http://stackoverflow.com/questions/15351298/capturing-android-webview-image-and-saving-to-png-jpeg");
The easiest way (to my knowledge) to capture a View as an image is to create a new Bitmap and a new Canvas. Then, simply ask your WebView to draw itself on your own Canvas instead of the Activity's default one.
Pseudocode:
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
myWebView.draw(canvas);
//save your bitmap, do whatever you need