Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.koushikdutta.ion.loader;
  
  import  android.content.Context;
  import  android.content.pm.PackageManager;
  import  android.content.res.Resources;
  import  android.graphics.Bitmap;
  import  android.graphics.BitmapFactory;
  import  android.graphics.Point;
  import  android.net.Uri;
 import  android.text.TextUtils;
 
 
Created by koush on 6/20/14.
 
 public class ResourceLoader extends StreamLoader {
     private static class Resource {
         Resources res;
         int id;
     }
 
     private static Resource lookupResource(Context contextString urithrows Exception {
         Uri u = Uri.parse(uri);
         if (u.getPathSegments() == null)
             throw new IllegalArgumentException("uri is not a valid resource uri");
         String pkg = u.getAuthority();
         Context ctx = context.createPackageContext(pkg, 0);
         Resources res = ctx.getResources();
         int id;
         if (u.getPathSegments().size() == 1)
             id = Integer.valueOf(u.getPathSegments().get(0));
         else if (u.getPathSegments().size() == 2) {
             String type = u.getPathSegments().get(0);
             String name = u.getPathSegments().get(1);
             id = res.getIdentifier(nametypepkg);
             if (id == 0)
                 throw new IllegalArgumentException("resource not found in given package");
         }
         else {
             throw new IllegalArgumentException("uri is not a valid resource uri");
         }
         Resource ret = new Resource();
         ret.res = res;
         ret.id = id;
         return ret;
     }
 
     @Override
     public Future<BitmapInfoloadBitmap(final Context contextfinal Ion ionfinal String keyfinal String urifinal int resizeWidthfinal int resizeHeightfinal boolean animateGif) {
         if (uri == null || !uri.startsWith("android.resource:/"))
             return null;
 
         final SimpleFuture<BitmapInforet = new SimpleFuture<BitmapInfo>();
 
 //        Log.d("FileLoader", "Loading file bitmap " + uri + " " + resizeWidth + "," + resizeHeight);
         Ion.getBitmapLoadExecutorService().execute(new Runnable() {
             @Override
             public void run() {
                 try {
                     Resource res = lookupResource(contexturi);
                     BitmapFactory.Options options = ion.getBitmapCache().prepareBitmapOptions(res.resres.idresizeWidthresizeHeight);
                     if (options == null)
                         throw new Exception("BitmapFactory.Options failed to load");
                     Point size = new Point(options.outWidthoptions.outHeight);
                     BitmapInfo info;
                     if (animateGif && TextUtils.equals("image/gif"options.outMimeType)) {
                         InputStream in = res.res.openRawResource(res.id);
                         try {
                             info = loadGif(keysizeinoptions);
                         }
                         finally {
                             StreamUtility.closeQuietly(in);
                         }
                     }
                     else {
                         Bitmap bitmap = IonBitmapCache.loadBitmap(res.resres.idoptions);
                         if (bitmap == null)
                             throw new Exception("Bitmap failed to load");
                         info = new BitmapInfo(keyoptions.outMimeTypenew Bitmap[] { bitmap }, size);
                     }
                     info.loadedFrom =  ..;
                     ret.setComplete(info);
                 }
                catch (OutOfMemoryError e) {
                    ret.setComplete(new Exception(e), null);
                }
                catch (Exception e) {
                    ret.setComplete(e);
                }
            }
        });
        return ret;
    }
    @Override
    public Future<DataEmitterload(final Ion ionfinal AsyncHttpRequest requestfinal FutureCallback<LoaderEmittercallback) {
        if (!request.getUri().getScheme().startsWith("android.resource:/"))
            return null;
        final InputStreamDataEmitterFuture ret = new InputStreamDataEmitterFuture();
        ion.getHttpClient().getServer().post(new Runnable() {
            @Override
            public void run() {
                try {
                    Resource res = lookupResource(ion.getContext(), request.getUri().toString());
                    InputStream stream = res.res.openRawResource(res.id);
                    if (stream == null)
                        throw new Exception("Unable to load content stream");
                    int available = stream.available();
                    InputStreamDataEmitter emitter = new InputStreamDataEmitter(ion.getHttpClient().getServer(), stream);
                    ret.setComplete(emitter);
                    callback.onCompleted(nullnew LoaderEmitter(emitteravailable.nullnull));
                }
                catch (Exception e) {
                    ret.setComplete(e);
                    callback.onCompleted(enull);
                }
            }
        });
        return ret;
    }
New to GrepCode? Check out our FAQ X