Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to convert image from asset folder into drawable in android?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 500
    Comment on it

    If we want to load image from asset folder and we need to convert that image into drawable, than we just need to call this method loadImageFromAsset() and pass the name of the image as a parameter at your activity class, it will convert it into drawable image and return it to you.

    String imageName="splash";
    Drawable drawable = loadImageFromAsset(imageName);
    
    if (drawable != null) {
         // set it wherever you want. 
            Log.d("TheActivity", "Setting the background");
        }
    
    public Drawable loadImageFromAsset(String imageName) {
    
        Drawable drawable;
    
        // load image
        try {
    
            // get input stream
            InputStream ims = getAssets().open(imageName+".png");
            //Note: Images can be in hierarical
    
            // load image as Drawable
            drawable = Drawable.createFromStream(ims, null);
    
        }
        catch(IOException ex) {
            Log.d("LoadingImage", "Error reading the image");
            return null;
        }
    
        return drawable;
    }
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: