0

I'm trying to display gif images on Android. I found this code on SO, which is said a correct solution by some users :

webView wView = new webView(this);    
wView.loadUrl("file:///android_asset/piggy.gif"); 
setContentView(view);

It says my gif file should be in assets folder. What should I do if my gif file is in drawable folder? And shouldn't the last line be like this :

setContentView(wView);

Other than this, if you have an easy solution for displaying gifs on Android, I can accept. Thanks.

jason
  • 6,962
  • 36
  • 117
  • 198

1 Answers1

2

Android has some basic requirements. All .gif / videos and custom fonts or (.otf) files must be only placed in assets folder. There is no other way of doing it as far as I know. To access the asset's files you have write a line of code :

The Format to access the Resource is as follows:

    "android.resource://[package]/[res id]"

    Uri myUri =("android.resource://" + context.getPackageName() + "/"
                        + R.drwable.mygifimage);
    try{
        Url myUrl = myUri.toURL();
        webView wView = new webView(this);   
        wView.loadUrl(myUrl);
        setContentView(view);

       }catch(Exception e){
              e.printStackTrace()
       }
Varun Agarwal
  • 1,587
  • 14
  • 29
  • 1
    If have any query please give your feedback on above answer. – Rajan Bhavsar Nov 17 '15 at 09:04
  • When I use this : `wView.loadUrl("android.resource://" + getPackageName() + "/" + R.drawable.a);` I get this : **android.resource://com.example.mypc.myapp/2443453543 couldn't be loaded.** Please tell me how to fix this, thanks. – jason Nov 17 '15 at 11:38
  • Have a look at my edit. This should work. If it doesnt let me know. Once I reach home, I will have access to my computer so I can take a closer look. – Varun Agarwal Nov 17 '15 at 12:53