12

Am trying to play local .swf files (kept in asset or sdcard) inside webview. But am not getting any luck...Can anyone guide me the proper way??? I am able to play swf files via url....but getting difficulty in playing local file inside webview

Ashish Anand
  • 3,531
  • 6
  • 34
  • 45
Vivek Tamrakar
  • 271
  • 3
  • 4
  • 15

3 Answers3

7

swf2.html:

<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html" charset="UTF-8" />
  </head>
  <body>
    <object width="215" height="140">
      <param name="movie" value="choudanse7us.swf">
        <embed src="file:///mnt/sdcard/choudanse7us.swf"
               width="215" height="140">
        </embed>
    </object>
  </body>
</html>

below is the android code

package webView.video;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.webkit.WebView;


public class WebViewActivity extends Activity {
private WebView mWebView;

/** Called when the activity is first created. */
     @Override
     public void onCreate (Bundle savedInstanceState) {
         super. onCreate (savedInstanceState);
         setContentView(R.layout.main);



         // html file with sample swf video in sdcard

         //swf2.html points to swf in sdcard

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


         if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
             System.exit(4);
         } else {
             mWebView.loadUrl("file://" + Environment.getExternalStorageDirectory() + "/swf2.html");
         }

     }
}
Ashish Anand
  • 3,531
  • 6
  • 34
  • 45
  • 1
    i have tried the same code but got this error http://stackoverflow.com/questions/9308712/how-to-run-the-swf-file-in-android-emulator how to solve this? – VENKI Feb 16 '12 at 10:52
  • @VENKI did you get answer to your question? I faced the same problem, and in my application also it was showing that blue colored cube with question mark symbol. I managed to solve it. I had flash installed in my device and still I was getting that cube. Let me know if you need the answer. – vaibhav Mar 02 '12 at 13:05
  • in ur manifest file. add following `android:hardwareAccelerated="true">` inside `` e.g.: ` android:icon="@drawable/ic_launcher" android:label="@string/app_name" > ` – vaibhav Mar 05 '12 at 05:55
6

For assets:

webView.loadUrl("file:///android_asset/YourFile.swf");

will play the file auto-scaled to the WebView size.


For the SD card, I expect something like this would work:

if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
    Log.d(TAG, "No SDCard");
} else {
    webView.loadUrl("file://" + Environment.getExternalStorageDirectory() + "/YourPath/YourFile.swf");
}

(Using the READ_EXTERNAL_STORAGE permission, of course).

Edit: You may also need to set:

webView.getSettings().setAllowFileAccess(true);
Sven Viking
  • 2,660
  • 21
  • 34
  • Thanks but my doubt is while running swf file in browser (from server) we use these lines: " " + " " + ""+ "" ........But for local file how to give path under tag – Vivek Tamrakar May 24 '11 at 09:09
  • Well, the first question is whether you need it to be embedded in a web page at a specific pixel size... if not, the above solution may be more useful. If you do need it embedded, does `...` work for assets? If it does, you could probably edit the HTML string programmatically to include the results of `"file://" + Environment.getExternalStorageDirectory() + "/YourPath/YourFile.swf"` as the embedded file, for files on the SD card. – Sven Viking May 24 '11 at 09:26
  • 1
    You'd still need to set `setAllowFileAccess(true)` and add the Permission as mentioned above, of course, for reading from the SD card. – Sven Viking May 24 '11 at 09:33
  • I have tried the code as : _webview.loadUrl("file:///android_asset/sample.3gp"); I kept sample.3gp inside asset folder but no output in screen ..Please help me out – Vivek Tamrakar May 24 '11 at 10:38
  • I have tried the code as : _webview.loadUrl("file:///android_asset/sample.3gp"); I kept sample.3gp inside asset folder but no output in screen ..Please help me out – – @Sven Viking – Vivek Tamrakar May 24 '11 at 11:38
  • 2
    Afraid I haven't worked with .3gp files before -- I didn't know the Android WebView even supported them. **EDIT**: I just did some quick Googling, and from what I can see I'm pretty sure the WebView cannot load .3gp files. – Sven Viking May 24 '11 at 13:34
  • Looks as if a VideoView would be needed for 3gp video. Related question here: http://stackoverflow.com/questions/4818047/how-to-play-3gp-video-file-in-android – Sven Viking May 25 '11 at 01:03
  • Ok...so Iam trying with swf alternative for 3gp .. Here is my code..... _linearLayout = new LinearLayout(this); _webview = new WebView(this); _linearLayout.addView(_webview); setContentView(_linearLayout); _webview.getSettings().setJavaScriptEnabled(true); _webview.getSettings().setPluginsEnabled(true); _webview.getSettings().setAllowFileAccess(true); String url = "file:///android_asset/sample.swf"; _webview.loadUrl(url); Iam gettin a blank screen ..Please help me....@Sven Viking – Vivek Tamrakar May 25 '11 at 05:21
  • 1
    The problem is just that the WebView has no height or width. Change the addView line to: `linearLayout.addView(webview, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);` . I just tried this myself, and it worked fine. – Sven Viking May 25 '11 at 07:45
  • I'm actually pretty new to Android development myself, but if you do need to contact me, you can find details on the website linked from my user page. – Sven Viking May 26 '11 at 05:39
0
package webView.video;


import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.webkit.WebView;


public class WebViewActivity extends Activity {
private WebView mWebView;

/** Called when the activity is first created. */
     @Override
     public void onCreate (Bundle savedInstanceState) {
         super. onCreate (savedInstanceState);
         setContentView(R.layout.main);



         // html file with sample swf video in sdcard

         //swf2.html points to swf in sdcard

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


         if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
             System.exit(4);
         } else {
             mWebView.loadUrl("file://" + Environment.getExternalStorageDirectory() + "/swf2.html");
         }

     }
}
Paŭlo Ebermann
  • 73,284
  • 20
  • 146
  • 210
Ashish Anand
  • 3,531
  • 6
  • 34
  • 45