I have been trying to show some HTML content from SD card in the browser Field and every time
instead of rendering the HTML data,browser Field renders the Base URL.I tried to find the reason for the same and couldn't get any solution.I didn't find any documentation that clearly explains when that base URL is called.Can any one please explain why and when is that Base URL called? Any Help is highly appreciated...
Thanks in advance.
May be it will help you. This answer explain how to display html content from sdcard in two ways
1)Read data as text file and add it to String and dislay that content like following way
StringBuffer sb1;
FileConnection fconn = (FileConnection)Connector.open("filepath="file:///SDCard/BlackBerry/documents/HtmlFileName.txt", Connector.READ_WRITE);
if (fconn.exists())
{
InputStream input=fconn.openDataInputStream();
sb1=new StringBuffer();
int chars,i=0;
while((chars=input.read())!=-1)
{
sb1.append((char)chars);
}
}
String str=sb1.toString();
VerticalFieldManager vfm = new VerticalFieldManager(VERTICAL_SCROLL|VERTICAL_SCROLL_MASK);
BrowserFieldConfig browserFieldConfig = new BrowserFieldConfig();
browserFieldConfig.setProperty(BrowserFieldConfig.NAVIGATION_MODE, BrowserFieldConfig.NAVIGATION_MODE_POINTER);
browserFieldConfig.setProperty(BrowserFieldConfig.NAVIGATION_MODE, BrowserFieldConfig.NAVIGATION_MODE_CARET);
browserFieldConfig.setProperty(BrowserFieldConfig.JAVASCRIPT_ENABLED,Boolean.TRUE);
browserFieldConfig.setProperty(BrowserFieldConfig.NAVIGATION_MODE_POINTER.toString(),Boolean.TRUE);
browserFieldConfig.setProperty(BrowserFieldConfig.ALLOW_CS_XHR,Boolean.TRUE);
BrowserField browser_field=new BrowserField(browserFieldConfig);
browser_field.displayContent(str,"");
vfm.add(browser_field);
add(vfm);
2)You can directly call that page from browserfield like following
String url="file:///sdcard/index.html";//specify exact path if resource then local:///.if sd card file:///
VerticalFieldManager manager=new VerticalFieldManager(VERTICAL_SCROLL|VERTICAL_SCROLLBAR|HORIZONTAL_SCROLL|HORIZONTAL_SCROLLBAR){
protected void sublayout(int maxWidth, int maxHeight) {
super.sublayout(640,Display.getHeight());
setExtent(640,Display.getHeight());
}
};
BrowserFieldConfig myBrowserFieldConfig = new BrowserFieldConfig();
myBrowserFieldConfig.setProperty(BrowserFieldConfig.NAVIGATION_MODE,BrowserFieldConfig.NAVIGATION_MODE_POINTER);
BrowserField browserField = new BrowserField(myBrowserFieldConfig);
history=browserField.getHistory();
browserField.requestContent(url);
manager.add(browserField);
browserField.requestContent(url);
add(manager);
I realize this is a little old, but I had the same problem and wanted to share how I solved my case.
It turns out that if you try to navigate to a new page in the BrowserField and the new page can't be loaded or an error occurs, the BrowserField will load the Base URL.
This issue occurred twice to me. In one instance, my program threw a runtime exception which was handled silently (by accident). This caused the navigation to fail and the Base URL to load. In the second instance, the page I was attempting to navigate to did not exist (either by misspelling or misuse of file names or locations).
Just to be clear, it was the Base URL of the original page which was loaded, not the page that failed to load.
I hope this helps!
Related
on Vaadin 7 I have the working code :
private void gridAttachmentsClickItemEventAction(ItemClickEvent event) {
// blablabla some code to get the data from the repository
byte[] data = bibocoAttachmentResponseEntity.getBody().getContent();
StreamResource.StreamSource source = convertByteArrayToStreamResource(data);
String filename = "c:\\droppdf\\"
+"temp"+bibocoAttachmentResponseEntity.getBody().getFileName()+LocalDate.now().toString();
StreamResource resource = new StreamResource(source, filename);
resource.setMIMEType("application/pdf");
resource.getStream().setParameter("Content-Disposition", "attachment; filename=" + filename);
BrowserWindowOpener opener = new BrowserWindowOpener(resource);
opener.extend(btnAttachmentPreview);
}
When I click on a grid row, the data is collected from that grid
and code following on it gets the data byte[] from a repository by calling a service.
Afterwards, when the user clicks on btnAttachmentPreview a new browser tab opens
and shows the pdf (that's what's in the data byte[])
This works fine the first time, but when I select a new row in the grid,
the problem is that the second call does not set the listener to the button right.
It show the first data byte[] again in a new tab, not the current data ...
The method is accessed, the correct data[] has been loaded in the array the second time, I checked.
I believe the listener on the btnAttachmentPreview attached due the code
opener.extend(btnAttachmentPreview);
should be binned (empty'ed or nulled) first. But I have no reference to it as for as I can tell.
Problem is that I don't want to destroy the btnAttachmentPreview object.
(The btnAttachmentPreview is a global variable and is set to a layout that I may not change. I know, not nice, but it's a ancient product)
When I close the browser and restart and clicking another row, the right data byte[] is showed.
Anyone a clue ?
Any help appreciated
You can remove an extension using its remove() method, i.e. opener.remove();.
If you cannot easily structure your code to store a reference to the old opener so that you have it available when you want to add a new one, then you can use btnAttachmentPreview.getExtensions() to get a collection of all current extensions and then from that you can find the appropriate extension (if any) and call remove() on it.
I have a table with addresses. I have a button you can click which I want to open a google search for the address in a separate window. I have tried this code with BrowserWindowOpener.
getUI().getPage().open(url, "_blank")
and
BrowserWindowOpener opener = new BrowserWindowOpener(url);
opener.extend(googleBtn)
but both are appending my url to the current path. I want to simply run a search in google in a separate window. I'm sure this is much simpler than I'm making it. It sure should be, at least. Thanks.
Brimby, you were right with your second try. The BrowserWindowOpener extension is the way to go. You should use an ExternalResource instance with an absolute URL like this:
public class OpenGoogleUI extends UI {
#Override
protected void init(VaadinRequest request) {
BrowserWindowOpener extension = new BrowserWindowOpener(new ExternalResource("https://www.google.by/#q=vaadin"));
Button button = new Button("Open Google");
extension.extend(button);
setContent(button);
}
}
Try this:
// Hyperlink to a given URL
Link link = new Link("Google It",
new ExternalResource("https://www.google.by/#q=search+query"));
// Open the URL in a new window/tab
link.setTargetName("_blank");
I am struggling to find a way to take a screenshot of a website in MVC4. I have seen two potential solutions, which neither work well for MVC.
The first is using the WebBrowser, tutorial found here, but this gives me a ActiveX control '8856f961-340a-11d0-a96b-00c04fd705a2' cannot be instantiated because the current thread is not in a single-threaded apartment error.
The other is using a 3rd party called Grabz.It, but I haven't found a way to integrate it into MVC.
Any other ideas/solutions?
Thanks.
Given your additional details, you should be able to do this with any number of tools. CodeCaster's idea is fine, and PhantomJS also offers similar webkit-based image generation of an arbitrary url (https://github.com/ariya/phantomjs/wiki/Screen-Capture). It offers several output format options, such as PNG, JPG, GIF, and PDF.
Since PhantomJS is using WebKit, a real layout and rendering engine, it can capture a web page as a screenshot. Because PhantomJS can render anything on the web page, it can be used to convert contents not only in HTML and CSS, but also SVG and Canvas.
You would need to execute the phantomjs.exe app from your MVC app, or probably even better by some service that is running behind the scenes to process a queue of submitted urls.
Why do you want to integrate this in MVC? Is it your website's responsibility to take screenshots of other websites? I would opt to create the screenshot-taking-logic in a separate library, hosted as a Windows Service for example.
The WebBrowser control needs to run on an UI thread, which a service (like IIS) doesn't have. You can try other libraries though.
You could for example write some code around wkhtmltopdf, which renders (as the name might suggest) HTML to PDF using the WebKit engine.
You need to specify that the thread is in STA (single threaded apartment mode in order to instantiate the web browser).
public ActionResult Save()
{
var url = "http://www.google.co.uk";
FileContentResult result = null;
Bitmap bitmap = null;
var thread = new Thread(
() =>
{
bitmap = ExportUrlToImage(url, 1280, 1024);
});
thread.SetApartmentState(ApartmentState.STA); //Set the thread to STA
thread.Start();
thread.Join();
if (bitmap != null)
{
using (var memstream = new MemoryStream())
{
bitmap.Save(memstream, ImageFormat.Jpeg);
result = this.File(memstream.GetBuffer(), "image/jpeg");
}
}
return result;
}
private Bitmap ExportUrlToImage(string url, int width, int height)
{
// Load the webpage into a WebBrowser control
WebBrowser wb = new WebBrowser();
wb.ScrollBarsEnabled = false;
wb.ScriptErrorsSuppressed = true;
wb.Navigate(url);
while (wb.ReadyState != WebBrowserReadyState.Complete)
{
Application.DoEvents();
}
// Set the size of the WebBrowser control
wb.Width = width;
wb.Height = height;
Bitmap bitmap = new Bitmap(wb.Width, wb.Height);
wb.DrawToBitmap(bitmap, new System.Drawing.Rectangle(0, 0, wb.Width, wb.Height));
wb.Dispose();
return bitmap;
}
Hi i want to show html content in BrowserField. I used the code blove to do this but i only see white empty page.
BrowserField demo = new BrowserField();
String res="<html><body><p>demo</p></body></html>";
demo.displayContent(res, "http://localhost");
Sometimes it shows my page correctly with css fonts but sometimes it does not show anything.
What is the problem in my code?
If you look at this BlackBerry example,
they make sure to add() the BrowserField to its parent Manager before calling displayContent().
I don't know if you just omitted that line of code to shorten your question, if you're missing the call to add() entirely, or if you put it after the call to displayContent(). But, try doing it in the order listed in the BlackBerry example, and let me know if that works.
BrowserField demo = new BrowserField();
add(demo);
String res="<html><body><p>demo</p></body></html>";
demo.displayContent(res, "http://localhost");
How do I display loading image before download a picture from web. After coming the web response(image), old image is replaced with the newly download picture.
I'm not sure I understand what you are asking for, but I'll try an answer for what I think your question is:
So, I'm assuming you already have an image on the screen(the old image). That will probably be an BitmapField. Let's say it's called img(BitmapField img;). This image will be on a manager on the screen.
You should do something like this:
PopupScreen ps = new PopupScreen(new HorizontalFieldManager());
ps.add(new RichTextField("Loading...", Field.FIELD_VCENTER | Field.NON_FOCUSABLE | Field.FIELD_HCENTER););
pushScreen(ps);
Thread t = new Thread(new Runnable() {
public void run() {
BitmapField newImage;
//logic to get new image is here
manager.replace(img, newImage);
Ui.getUiEngine().popScreen(ps);//dismiss the loading screen
}
});
t.start();