WookieLNX
WookieLNX

Reputation: 96

Kindle Fire and Youtube Links in WebView

We have several apps in the Android Market that we also submitted to Amazon to their Appstore for Android. Things worked well until the Kindle Fire came out. In several of our apps, we launch Youtube videos from a link on a WebView. Here is the relevant code from setOnClickListener that pushes the view and handles the Youtube links:

mWebView.setWebViewClient (new WebViewClient () {
     @ Override public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if (url.startsWith("vnd.youtube")){  
            startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
            return true;
         } else{
            return false;
         }
     }
});

As I said, this works great on all Android devices that we've tested on, but we are getting notified from Amazon that they are getting crashes when clicking on the video links. I assume this is happening because the Fire does not come loaded with an YouTube app. Has anyone else experienced similar issues? What would be the prefered method for playing a youtube video from a webview on the Fire?

Here is the logcat supplied by Amazon:

01-09 12:28:32.715 W/dalvikvm(5805): threadid=1: thread exiting with uncaught exception (group=0x40015560)
01-09 12:28:32.715 D/ActivityManager(1372): Starting: Intent { act=android.intent.action.VIEW dat=vnd.youtube://XXXXXXXXXX } from pid 5805
01-09 12:28:32.793 E/AndroidRuntime(5805): FATAL EXCEPTION: main
01-09 12:28:32.793 E/AndroidRuntime(5805): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=vnd.youtube://XXXXXXXXXX }
01-09 12:28:32.793 E/AndroidRuntime(5805):  at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1409)
01-09 12:28:32.793 E/AndroidRuntime(5805):  at android.app.Instrumentation.execStartActivity(Instrumentation.java:1379)
01-09 12:28:32.793 E/AndroidRuntime(5805):  at android.app.Activity.startActivityFromChild(Activity.java:3103)
01-09 12:28:32.793 E/AndroidRuntime(5805):  at android.app.Activity.startActivityForResult(Activity.java:2883)
01-09 12:28:32.793 E/AndroidRuntime(5805):  at android.app.Activity.startActivity(Activity.java:2969)
01-09 12:28:32.793 E/AndroidRuntime(5805):  at com.XXXXXXXXXX.XXXXXXXXXX.VideoActivity$1$1.shouldOverrideUrlLoading(VideoActivity.java)
01-09 12:28:32.793 E/AndroidRuntime(5805):  at android.webkit.CallbackProxy.uiOverrideUrlLoading(CallbackProxy.java:216)
01-09 12:28:32.793 E/AndroidRuntime(5805):  at android.webkit.CallbackProxy.handleMessage(CallbackProxy.java:323)
01-09 12:28:32.793 E/AndroidRuntime(5805):  at android.os.Handler.dispatchMessage(Handler.java:99)
01-09 12:28:32.793 E/AndroidRuntime(5805):  at android.os.Looper.loop(Looper.java:130)
01-09 12:28:32.793 E/AndroidRuntime(5805):  at android.app.ActivityThread.main(ActivityThread.java:3683)
01-09 12:28:32.793 E/AndroidRuntime(5805):  at java.lang.reflect.Method.invokeNative(Native Method)
01-09 12:28:32.793 E/AndroidRuntime(5805):  at java.lang.reflect.Method.invoke(Method.java:507)
01-09 12:28:32.793 E/AndroidRuntime(5805):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:850)
01-09 12:28:32.793 E/AndroidRuntime(5805):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:608)
01-09 12:28:32.793 E/AndroidRuntime(5805):  at dalvik.system.NativeStart.main(Native Method)

Edit

I was able to get this working properly now. Basically, I copied my video folder (which contains all of the html for the video pages) in assets to a new video_fire directory. I updated the actual code to point to the video_fire directory if the android.os.Build.MODEL is "Kindle Fire" and to the normal video directory for all else. I also modified the webview allocation to use getParent() rather than VideoActivity.this. This fixed the issue I described in the comments about full screen displays. Below is the updated code that I used (when clicking on an item in a ListView it displays the appropriate html page):

public void onItemClick(AdapterView<?> a, View v, int position, long id) {
    String contentsOfHtml;
    try {
        if (android.os.Build.MODEL.equals("Kindle Fire")) {
            contentsOfHtml = readFileAsString("video_fire/"+htmlarray[position]);
        } else {
            contentsOfHtml = readFileAsString("video/"+htmlarray[position]);
        }
    } catch (IOException e) { 
        // Should never happen! 
        throw new RuntimeException(e); 
    } 
    WebView mWebView =  new WebView(getParent());
    WebSettings webSettings = mWebView.getSettings(); 
    webSettings.setPluginsEnabled(true);
    webSettings.setJavaScriptEnabled(true);
    mWebView.setWebViewClient (new WebViewClient () {
        @ Override public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if (url.startsWith("vnd.youtube")){  
            startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
            return true;
         } else{
            return false;
         }
        }
    });
mWebView.loadDataWithBaseURL("file:///android_asset/", contentsOfHtml, "text/html", "utf-8", "");
ReflectionGroup.group.replaceView(mWebView);
return;
});

Yes, I'm using ActivityGroup which is depreciated (this is old code). I'm working on updating the base code that this uses to implement Fragments...

I then have html for normal devices:

<html>
<head>
    <link href="style.css" rel="stylesheet" type="text/css">
</head>
<body>
    <p class="title">Title</p>
    <hr/>
    <a href="vnd.youtube://XXXXX"><div><center><img src="images/video1.jpg" width="220" height="150"></center></div></a>
    </center>
    <p class="para">Text</p>
    <hr/>       
</body>
</html>

And specific HTML for Kindle Fire. I suppose I should check to see if this code would work on other devices, too, but that will have to be put on my to-do list.

<html>
<head>
    <link href="style.css" rel="stylesheet" type="text/css">
</head>
<body>
    <p class="title">Title</p>
    <hr/>
<iframe class="youtube-player" type="text/html" width="600" height="361" src="http://www.youtube.com/embed/XXXXXXXX" frameborder="0">
</iframe>
    </center>
    <p class="para">Text    </p>
    <hr/>       
</body>
</html>

Upvotes: 3

Views: 1981

Answers (2)

jtt
jtt

Reputation: 13541

if there is nothing that can handle the youtube links. Then you already have a problem.

This error clearly states:

android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=vnd.youtube://XXXXXXXXXX }

Meaning nothing in the system can handle that, I dont think you did anything wrong, but then again i'm not familiar with the platform of the kindle fire. however; with some utilization of the PackageManager, you can figure it out.

Upvotes: 1

akkilis
akkilis

Reputation: 1934

Try to catch this exception with the specific exception type and then write the code to launch a browser by editting your url (start it with "http", else it wont be open in web browser).

....
catch(ActivityNotFoundException e){
url = YOUR_URL_STARTING_WITH_HTTP;
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
}



Editted Section:
now thats a different problem, generally comes if the view is called or tried to show/modify after the activity is finished. Are you doing something in onPostExecuted() related to ui in any of the async task ?? This might be happening after the activity created that task is finished.

Upvotes: 1

Related Questions