PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, July 18, 2022

[FIXED] How to display gif on Android device

 July 18, 2022     android, animated-gif, gif     No comments   

Issue

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.


Solution

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()
       }


Answered By - Varun Agarwal
Answer Checked By - David Goodson (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing