Home | History | Annotate | Download | only in bitmap
      1 package com.bumptech.glide.load.resource.bitmap;
      2 
      3 import android.graphics.Bitmap;
      4 import android.os.ParcelFileDescriptor;
      5 import android.util.Log;
      6 
      7 import com.bumptech.glide.load.ResourceDecoder;
      8 import com.bumptech.glide.load.engine.Resource;
      9 import com.bumptech.glide.load.model.ImageVideoWrapper;
     10 
     11 import java.io.IOException;
     12 import java.io.InputStream;
     13 
     14 /**
     15  * A {@link ResourceDecoder} that decodes {@link ImageVideoWrapper}s using
     16  * a wrapped {@link ResourceDecoder} for {@link InputStream}s
     17  * and a wrapped {@link ResourceDecoder} for {@link ParcelFileDescriptor}s.
     18  * The {@link InputStream} data in the {@link ImageVideoWrapper} is always preferred.
     19  */
     20 public class ImageVideoBitmapDecoder implements ResourceDecoder<ImageVideoWrapper, Bitmap> {
     21     private static final String TAG = "ImageVideoDecoder";
     22     private final ResourceDecoder<InputStream, Bitmap> streamDecoder;
     23     private final ResourceDecoder<ParcelFileDescriptor, Bitmap> fileDescriptorDecoder;
     24 
     25     public ImageVideoBitmapDecoder(ResourceDecoder<InputStream, Bitmap> streamDecoder,
     26             ResourceDecoder<ParcelFileDescriptor, Bitmap> fileDescriptorDecoder) {
     27         this.streamDecoder = streamDecoder;
     28         this.fileDescriptorDecoder = fileDescriptorDecoder;
     29     }
     30 
     31     @SuppressWarnings("resource")
     32     // @see ResourceDecoder.decode
     33     @Override
     34     public Resource<Bitmap> decode(ImageVideoWrapper source, int width, int height) throws IOException {
     35         Resource<Bitmap> result = null;
     36         InputStream is = source.getStream();
     37         if (is != null) {
     38             try {
     39                 result = streamDecoder.decode(is, width, height);
     40             } catch (IOException e) {
     41                 if (Log.isLoggable(TAG, Log.VERBOSE)) {
     42                     Log.v(TAG, "Failed to load image from stream, trying FileDescriptor", e);
     43                 }
     44             }
     45         }
     46 
     47         if (result == null) {
     48             ParcelFileDescriptor fileDescriptor = source.getFileDescriptor();
     49             if (fileDescriptor != null) {
     50                 result = fileDescriptorDecoder.decode(fileDescriptor, width, height);
     51             }
     52         }
     53         return result;
     54     }
     55 
     56     @Override
     57     public String getId() {
     58         return "ImageVideoBitmapDecoder.com.bumptech.glide.load.resource.bitmap";
     59     }
     60 }
     61