Download svg file from storage.googleapis with webview - android - java

I would like to show some svg logos in my android mobile app. Some weeks ago google released AI www.autodraw.com (Web based tool for drawing)
I loaded the url in a webview, and everything works fine.
I would like to download the svg files (small pictures on top, the results of your drawings) directly to my app from the webview, when the user clicks one the choosed one. (after it, I would like to convert it to VectorDrawable and set it as a background of ImageViews to use it flexible with all pixel densities).
Google allows you to download your drawings, but it returns a png file and not always centered (users will not draw correctly always). That's why I would like to download the svg directly.
As an example of a result svg file from storage.googleapis:
https://storage.googleapis.com/artlab-public.appspot.com/stencils/selman/house-01.svg
This is what I have now:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_auto_draw);
myWebView = (WebView) findViewById(R.id.autodraw_webview);
imgView = (ImageView) findViewById(R.id.imageView);
myWebView.loadUrl("http://www.autodraw.com");
myWebView.setWebViewClient(new WebViewClient() {
#Override
public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
return super.shouldInterceptRequest(view, request);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
Question:
Is it possible to get the svg file from the WebView to the app by user clicking one of the result images of his/her drawing ?

Related

Get random Youtube video in webview by clicking button in Android studio

I want to make an app where the user can get random youtube videos in a webview by clicking a button.
I already can show a video in my webview with the url but I want to get any random video from youtube by clicking my button.
In my xml file I have a webview and a button
´´´
MainActivity.java
´´´
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String path = "https://www.youtube.com/watch?v=1uwvxTT5V5M";
String dataUrl =
"<html><body>Video From YouTube<br><iframe width=\"420\" height=\"315\" src=\"https://www.youtube.com/embed/47yJ2XCRLZs\" frameborder=\"0\" allowfullscreen></iframe></body></html>";
WebView showYoutubeVideo = (WebView) findViewById(R.id.zufallvideo);
showYoutubeVideo.setWebViewClient(new WebViewClient(){
public boolean shouldoverloadUrlLoading(WebView view, String url) {
return false;
}
});
showYoutubeVideo.getSettings().setJavaScriptEnabled(true);
showYoutubeVideo.loadData(dataUrl,"text/html", "utf-8");
}
}
TL;DR: it's complicated.
Until Youtube exposes an endpoint in their API to do this, you're gonna need to find a workaround.
Some would suggest to query a search with "IMG" followed by a random integer (ex. "IMG 22930"). The end result is quite surprising, since tons of videos are uploaded without renaming.
Here's an example of this workaround implementation in python: https://github.com/0x01h/random-youtube-video-generator

Send to Messenger Button not showing up in Android Webview

Context: I'm trying to link an authenticated user in my android app to a Facebook fanpage PSID using the "Send to Messenger" Plugin to be able to send messages to that specific user on certain events that responds to the user actions in other systems.
I figured the best way to achieve this (since said plugin only works with the Facebook Javascript SDK) was to open a WebView Activity in my app, loading a website with the plugin button.
The website includes the following to load the SDK and the button:
<script>
window.fbAsyncInit = function() {
FB.init({
appId : '<<Here I put my FB App ID>>',
autoLogAppEvents : true,
xfbml : true,
version : 'v2.12'
});
};
(function(d, s, id){
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) {return;}
js = d.createElement(s); js.id = id;
js.src = "https://connect.facebook.net/en_US/sdk.js";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
</script>
<div class="fb-send-to-messenger"
messenger_app_id="<<Same FB App ID>>"
page_id="<<FB fanpage ID>>"
data-ref="<<user identification>>"
color="blue"
size="standard">
</div>
Here's the onCreate method of the Webview Activity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
WebView webview = new WebView(this);
getWindow().requestFeature(Window.FEATURE_PROGRESS);
setContentView(webview);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
WebView.setWebContentsDebuggingEnabled(true);
}
webview.getSettings().setJavaScriptEnabled(true);
final Activity activity = this;
webview.setWebChromeClient(new WebChromeClient() {
#Override
public void onProgressChanged(WebView view, int progress) {
// Activities and WebViews measure progress with different scales.
// The progress meter will automatically disappear when we reach 100%
activity.setProgress(progress * 1000);
}
#Override
public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
Log.d("WEBVIEW_CONSOLE", consoleMessage.message() + " -- From line "
+ consoleMessage.lineNumber() + " of "
+ consoleMessage.sourceId());
return super.onConsoleMessage(consoleMessage);
}
});
webview.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(activity, "Oh no! " + description, Toast.LENGTH_SHORT).show();
}
});
webview.loadUrl(getString(R.string.authenticated_webview_url_example), headers);
Expectation: The facebook Send to Messenger Button should load in the WebView, allowing the user to register a PSID on my Facebook fanpage/App.
Problem: When I load the website on the Android WebView, the button is not shown.
Details: Using Chrome Remote Debugging, I was able to determine the Facebook SDK was working and almost all the elements of the buttons were loaded, except for the content of the iframe generated.
Tested the Facebook jssdk by using a Like button, which worked and was shown.
No errors in website console or logcat.
Why is WebView not showing that particular button?
Testing website: https://www.vsdesigns.cl/debug/login.php
Edit:
Here is an image of the testing website from a desktop browser. It shows the button.
Here is another image with the testing website being loaded by the WebView. It's actually a representation of the chrome remote debugger, but it's the same as what my smartphone screen shows. It doesn't show the button.
Thanks in advance.
I figured out the Facebook Cookies weren't loading at all, and Facebook doesn't render the plugin if the App is in development and the user is not previously logged in, so I implemented the following code to accept Third party cookies. (Only works on SDK version 21 Lollipop and up)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
cookieManager.setAcceptThirdPartyCookies(mWebview,true);
}
mWebview would be the variable storing the actual WebView.

How to get text of hyperlink of webview once click in android?

I am building an android application where a URL is loaded on a webView. Now in that URL, there are some Hyperlink's which is loaded when a user clicks on them. While click I get that URL string and a load that URL in webView.
Now I need when a user clicks on that Hyperlink I need to get that text of that Hyperlink. I had to google a lot yet did not get any good answers.
This function will show all resources text(hyperlink, image, css file, js file)
webView.setWebViewClient(new WebViewClient(){
#Override
public void onLoadResource(WebView view, String url) {
super.onLoadResource(view, url);
Log.d(TAG, "onLoadResource: " + url);
}
});
So you have to check before using it
if (!url.endsWith(".css")
&& !url.endsWith(".js")
&& !url.endsWith(".otherformat") ) {
// Show your url
}

How can I open multiple PDF Links in Android WebView in Android?

I'm building android app where, I placed links to the page in webview on which multiple links are there pointing to PDFs. Everyday, new links are updated. I want to make such functionality that when user clicks on the particular PDF link, it should get opened in default PDF viewer or there itself.
Right now, nothing is happening when user clicks on PDF link within webview.
WebView w=(WebView)findViewById(R.id.web1);
WebSettings webSettings=w.getSettings();
webSettings.setDomStorageEnabled(true);
webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
w.setWebViewClient(new WebViewClient());
w.loadUrl("http://collegecirculars.unipune.ac.in/sites/examdocs/_layouts/mobile/view.aspx?List=7ed6607e-6c43-401a-a922-bf8d8bf08ed8&View=dc261157-c533-4a60-977b-506fd87b2a19");
You can use Google Docs Viewer to read your pdf online:
WebView webview = (WebView) findViewById(R.id.webview);
webview.getSettings().setJavaScriptEnabled(true);
String pdf = "http://collegecirculars.unipune.ac.in/sites/examdocs/Examiantions%20Timetables/Master-In-ARTS(M.A)(Rev.2008)-(24908)_Spl4-8-16.pdf?Mobile=1&Source=%2Fsites%2Fexamdocs%2F_layouts%2Fmobile%2Fview%2Easpx%3FList%3D7ed6607e%252D6c43%252D401a%252Da922%252Dbf8d8bf08ed8%26View%3Ddc261157%252Dc533%252D4a60%252D977b%252D506fd87b2a19%26ViewMode%3DSimple%26CurrentPage%3D1";
webview.loadUrl("http://drive.google.com/viewerng/viewer?embedded=true&url=" + pdf);
open in browser
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(pdf_url));
startActivity(browserIntent);
To override current URL :
private class MyWebViewClient extends WebViewClient {
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url){
// here you can check PDF
url = url.toLowerCase();
view.loadUrl(url);
return true;
}
}
set this MyWebViewClient to your webView.
PaymentWebViewClient paymentWebViewClient = new PaymentWebViewClient();
webView.setWebViewClient(paymentWebViewClient);

Android Webview ERR_NAME_NOT_RESOLVED loading an URL

I have an App made using Android Webview, while using Wifi connection the initial page loaded displays ok, after switching to mobile data mode, the application shows the ERR_NAME_NOT_RESOLVED message at times.
Is there a workaround to hide this message until the page is correctly displayed? or to make it work when the connection is not good in the initial load?
Thanks for the insight.
You can ask the user to retry at that point of time either displaying there or by showing a popup with a message. If you get the network is strong enough load the same page again.
You can check the url that is getting loaded in your webview by doing this.
myWebView.setWebViewClient(new WebViewClient() {
#SuppressLint("NewApi")
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
});

Categories