Home | History | Annotate | Download | only in model
      1 package com.bumptech.glide.load.model;
      2 
      3 import android.net.Uri;
      4 
      5 import com.bumptech.glide.load.data.DataFetcher;
      6 
      7 import java.io.File;
      8 
      9 /**
     10  * A model loader for handling certain string models. Handles paths, urls, and any uri string with a scheme handled by
     11  * {@link android.content.ContentResolver#openInputStream(Uri)}.
     12  *
     13  * @param <T> The type of data that will be loaded from the given {@link java.lang.String}.
     14  */
     15 public class StringLoader<T> implements ModelLoader<String, T> {
     16     private final ModelLoader<Uri, T> uriLoader;
     17 
     18     public StringLoader(ModelLoader<Uri, T> uriLoader) {
     19         this.uriLoader = uriLoader;
     20     }
     21 
     22     @Override
     23     public DataFetcher<T> getResourceFetcher(String model, int width, int height) {
     24         Uri uri;
     25         if (model.startsWith("/")) {
     26             uri = toFileUri(model);
     27         } else {
     28             uri = Uri.parse(model);
     29             final String scheme = uri.getScheme();
     30             if (scheme == null) {
     31                 uri = toFileUri(model);
     32             }
     33         }
     34 
     35         return uriLoader.getResourceFetcher(uri, width, height);
     36     }
     37 
     38     private static Uri toFileUri(String path) {
     39         return Uri.fromFile(new File(path));
     40     }
     41 }
     42