提交 512fcec1 编写于 作者: S Sam Judd

Fix NPE in ImageVideoBitmapDecoder.

上级 ac17cee9
......@@ -36,7 +36,10 @@ public class ImageVideoBitmapDecoder implements ResourceDecoder<ImageVideoWrappe
}
if (result == null) {
result = fileDescriptorDecoder.decode(source.getFileDescriptor(), width, height);
ParcelFileDescriptor fileDescriptor = source.getFileDescriptor();
if (fileDescriptor != null) {
result = fileDescriptorDecoder.decode(source.getFileDescriptor(), width, height);
}
}
return result;
}
......
......@@ -9,11 +9,27 @@ import com.bumptech.glide.load.DecodeFormat;
import java.io.IOException;
public class VideoBitmapDecoder implements BitmapDecoder<ParcelFileDescriptor> {
private static final DefaultFactory DEFAULT_FACTORY = new DefaultFactory();
private MediaMetadataRetrieverFactory factory;
interface MediaMetadataRetrieverFactory {
public MediaMetadataRetriever build();
}
public VideoBitmapDecoder() {
this(DEFAULT_FACTORY);
}
VideoBitmapDecoder(MediaMetadataRetrieverFactory factory) {
this.factory = factory;
}
@Override
public Bitmap decode(ParcelFileDescriptor resource, BitmapPool bitmapPool, int outWidth, int outHeight,
DecodeFormat decodeFormat)
throws IOException {
MediaMetadataRetriever mediaMetadataRetriever = new MediaMetadataRetriever();
MediaMetadataRetriever mediaMetadataRetriever = factory.build();
mediaMetadataRetriever.setDataSource(resource.getFileDescriptor());
Bitmap result = mediaMetadataRetriever.getFrameAtTime();
mediaMetadataRetriever.release();
......@@ -23,6 +39,13 @@ public class VideoBitmapDecoder implements BitmapDecoder<ParcelFileDescriptor> {
@Override
public String getId() {
return "VideoBitmapDecoder.com.bumptech.glide.load.data.bitmap";
return "VideoBitmapDecoder.com.bumptech.glide.load.resource.bitmap";
}
private static class DefaultFactory implements MediaMetadataRetrieverFactory {
@Override
public MediaMetadataRetriever build() {
return new MediaMetadataRetriever();
}
}
}
......@@ -82,6 +82,16 @@ public class ImageVideoBitmapDecoderTest {
verify(harness.streamDecoder, never()).decode(any(InputStream.class), anyInt(), anyInt());
}
@Test
public void testDoesNotTryToDecodeNullFileDescriptor() throws IOException {
when(harness.wrapper.getStream()).thenReturn(null);
when(harness.wrapper.getFileDescriptor()).thenReturn(null);
harness.decoder.decode(harness.wrapper, 100, 102);
verify(harness.fileDescriptorDecoder, never()).decode(any(ParcelFileDescriptor.class), anyInt(), anyInt());
}
@Test
public void testHasValidId() {
Util.assertClassHasValidId(ImageVideoBitmapDecoder.class, harness.decoder.getId());
......@@ -94,6 +104,5 @@ public class ImageVideoBitmapDecoderTest {
ResourceDecoder<ParcelFileDescriptor, Bitmap> fileDescriptorDecoder = mock(ResourceDecoder.class);
ImageVideoBitmapDecoder decoder = new ImageVideoBitmapDecoder(streamDecoder, fileDescriptorDecoder);
ImageVideoWrapper wrapper = mock(ImageVideoWrapper.class);
}
}
package com.bumptech.glide.load.resource.bitmap;
import android.graphics.Bitmap;
import android.media.MediaMetadataRetriever;
import android.os.ParcelFileDescriptor;
import com.bumptech.glide.load.DecodeFormat;
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import com.bumptech.glide.tests.Util;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import java.io.FileDescriptor;
import java.io.IOException;
import static junit.framework.Assert.assertEquals;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@RunWith(RobolectricTestRunner.class)
public class VideoBitmapDecoderTest {
private BitmapPool bitmapPool;
private DecodeFormat decodeFormat;
private ParcelFileDescriptor resource;
private VideoBitmapDecoder decoder;
private VideoBitmapDecoder.MediaMetadataRetrieverFactory factory;
private MediaMetadataRetriever retriever;
@Before
public void setup() {
bitmapPool = mock(BitmapPool.class);
decodeFormat = DecodeFormat.ALWAYS_ARGB_8888;
resource = mock(ParcelFileDescriptor.class);
factory = mock(VideoBitmapDecoder.MediaMetadataRetrieverFactory.class);
retriever = mock(MediaMetadataRetriever.class);
when(factory.build()).thenReturn(retriever);
decoder = new VideoBitmapDecoder(new VideoBitmapDecoder.MediaMetadataRetrieverFactory() {
@Override
public MediaMetadataRetriever build() {
return factory.build();
}
});
}
@Test
public void testReturnsRetrievedFrameForResource() throws IOException {
Bitmap expected = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);
when(retriever.getFrameAtTime()).thenReturn(expected);
FileDescriptor toSet = FileDescriptor.in;
when(resource.getFileDescriptor()).thenReturn(toSet);
Bitmap result = decoder.decode(resource, bitmapPool, 100, 100, decodeFormat);
verify(retriever).setDataSource(eq(toSet));
assertEquals(expected, result);
}
@Test
public void testReleasesMediaMetadataRetriever() throws IOException {
decoder.decode(resource, bitmapPool, 1, 2, decodeFormat);
verify(retriever).release();
}
@Test
public void testClosesResource() throws IOException {
decoder.decode(resource, bitmapPool, 1, 2, decodeFormat);
verify(resource).close();
}
@Test
public void testHasValidId() {
Util.assertClassHasValidId(VideoBitmapDecoder.class, decoder.getId());
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册