Tuesday, January 31, 2012

How to open the flash file present on sdcard in Android Webview?

Following is the example.............Create the flash folder on sdcard and store the flash file in it and write down the following file name in String "a" variable.


public class WebViewActivity extends Activity {
Button btnprev, btnnext;
static int count;
WebView mWebView;

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

String htmlPre = "<!DOCTYPE html><html lang=\"en\"><head><meta charset=\"utf-8\"></head><body style='margin:0; pading:0; background-color: black;'>";
String htmlCode =
" <embed style='width:100%; height:100%' src='http://www.platipus.nl/flvplayer/download/1.0/FLVPlayer.swf?fullscreen=true&video=@VIDEO@' " +
"  autoplay='true' " +
"  quality='high' bgcolor='#000000' " +
"  name='VideoPlayer' align='middle'" + // width='640' height='480'
"  allowScriptAccess='*' allowFullScreen='true'" +
"  type='application/x-shockwave-flash' " +
"  pluginspage='http://www.macromedia.com/go/getflashplayer' />" +
"";
String htmlPost = "</body></html>";

setContentView(R.layout.main);
mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setPluginsEnabled(true);
mWebView.getSettings().setAllowFileAccess(true);

String a="Cervifert_Intro_Final.swf";
String b="file://" + Environment.getExternalStorageDirectory() + "/flash/"+a+"";


String assethtml="<object width=\"1024\" height=\"710\"> <param name=\"movie\" value=\""+b+"\"> <embed src=\""+b+"\" width=\"1024\" height=\"710\">";

String mimeType = "text/html";
String encoding = "utf-8";
if(isFlashInstalled())
mWebView.loadDataWithBaseURL("null", assethtml, mimeType, encoding, "");
else
Toast.makeText(this, "Not Present", Toast.LENGTH_LONG).show();


}
public boolean isFlashInstalled(){
 boolean flashInstalled = false;
 try {
   PackageManager pm = getPackageManager();
   ApplicationInfo ai = pm.getApplicationInfo("com.adobe.flashplayer", 0);
   if (ai != null)
     flashInstalled = true;
 } catch (NameNotFoundException e) {
   flashInstalled = false;
 }
 return flashInstalled;
 }



 @Override
 protected void onPause(){
     super.onPause();
   
     callHiddenWebViewMethod("onPause");

     mWebView.pauseTimers();
     if(isFinishing()){
     mWebView.loadUrl("about:blank");
         setContentView(new FrameLayout(this));
     }
 }

 @Override
 protected void onResume(){
     super.onResume();
     callHiddenWebViewMethod("onResume");
     mWebView.resumeTimers();
 }

 private void callHiddenWebViewMethod(String name){
 
     if( mWebView != null ){
         try {
             Method method = WebView.class.getMethod(name);
             method.invoke(mWebView);
         } catch (NoSuchMethodException e) {
             Log.e("No such method: ",  name + e);
         } catch (IllegalAccessException e) {
             Log.e("Illegal Access: ",  name + e);
         } catch (InvocationTargetException e) {
             Log.e("Invocation Target Exception: ",  name + e);
         }
     }
 }
}

No comments:

Post a Comment