Search in sources :

Example 6 with BinaryResource

use of com.facebook.binaryresource.BinaryResource in project fresco by facebook.

the class DiskStorageCacheTest method testVersioning.

/**
 * Test to make sure that the same item stored with two different versions of the cache will be
 * stored with two different file names.
 *
 * @throws UnsupportedEncodingException
 */
@Test
public void testVersioning() throws IOException {
    // Define data that will be written to cache
    CacheKey key = new SimpleCacheKey("version_test");
    byte[] value = new byte[32];
    value[0] = 'v';
    // Set up cache with version == 1
    DiskStorage storage1 = createDiskStorage(TESTCACHE_CURRENT_VERSION);
    DiskStorageCache cache1 = createDiskCache(storage1, false);
    // Write test data to cache 1
    cache1.insert(key, WriterCallbacks.from(value));
    // Get cached file
    BinaryResource resource1 = getResource(storage1, key);
    assertNotNull(resource1);
    // Set up cache with version == 2
    DiskStorage storageSupplier2 = createDiskStorage(TESTCACHE_NEXT_VERSION);
    DiskStorageCache cache2 = createDiskCache(storageSupplier2, false);
    // Write test data to cache 2
    cache2.insert(key, WriterCallbacks.from(value));
    // Get cached file
    BinaryResource resource2 = getResource(storageSupplier2, key);
    assertNotNull(resource2);
    // Make sure filenames of the two file are different
    assertFalse(resource2.equals(resource1));
}
Also used : BinaryResource(com.facebook.binaryresource.BinaryResource) SimpleCacheKey(com.facebook.cache.common.SimpleCacheKey) CacheKey(com.facebook.cache.common.CacheKey) MultiCacheKey(com.facebook.cache.common.MultiCacheKey) SimpleCacheKey(com.facebook.cache.common.SimpleCacheKey) PrepareOnlyThisForTest(org.powermock.core.classloader.annotations.PrepareOnlyThisForTest) Test(org.junit.Test)

Example 7 with BinaryResource

use of com.facebook.binaryresource.BinaryResource in project lzc_app_lib by httplzc.

the class BigImgViewRealImgHelper method initBitmapRegion.

// 初始化原始图信息
public void initBitmapRegion(Uri uri) {
    BinaryResource binaryResource = Fresco.getImagePipelineFactory().getMainFileCache().getResource(new SimpleCacheKey(uri.toString()));
    if (binaryResource instanceof FileBinaryResource) {
        imgRotate = FileUntil.readPictureDegree(((FileBinaryResource) binaryResource).getFile().getPath());
    // Log.i("lzc", "imgRotate  " + imgRotate);
    }
    InputStream inputStream = null;
    try {
        if (binaryResource == null) {
            if (uri.getScheme().equals("res")) {
                inputStream = imageView.getResources().openRawResource(Integer.valueOf(uri.getPathSegments().get(uri.getPathSegments().size() - 1)));
            } else {
                String path = FileUntil.UriToFile(uri, null);
                if (path == null)
                    return;
                inputStream = new FileInputStream(new File(path));
            }
        } else {
            inputStream = binaryResource.openStream();
        }
        if (inputStream != null) {
            bitmapRegionDecoder = BitmapRegionDecoder.newInstance(inputStream, false);
            bigImgRealWidth = imgRotate == 0 || imgRotate == 180 ? bitmapRegionDecoder.getWidth() : bitmapRegionDecoder.getHeight();
            bigImgRealHeight = imgRotate == 0 || imgRotate == 180 ? bitmapRegionDecoder.getHeight() : bitmapRegionDecoder.getWidth();
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (inputStream != null)
                inputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Also used : BinaryResource(com.facebook.binaryresource.BinaryResource) FileBinaryResource(com.facebook.binaryresource.FileBinaryResource) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) SimpleCacheKey(com.facebook.cache.common.SimpleCacheKey) IOException(java.io.IOException) File(java.io.File) FileBinaryResource(com.facebook.binaryresource.FileBinaryResource) FileInputStream(java.io.FileInputStream)

Example 8 with BinaryResource

use of com.facebook.binaryresource.BinaryResource in project remusic by aa112901.

the class RadioDetailActivity method setAlbumart.

private void setAlbumart() {
    albumTitle.setText(albumName);
    albumArtSmall.setImageURI(Uri.parse(albumPath));
    try {
        ImageRequest imageRequest = ImageRequest.fromUri(albumPath);
        CacheKey cacheKey = DefaultCacheKeyFactory.getInstance().getEncodedCacheKey(imageRequest);
        BinaryResource resource = ImagePipelineFactory.getInstance().getMainDiskStorageCache().getResource(cacheKey);
        File file = ((FileBinaryResource) resource).getFile();
        if (file != null) {
            new setBlurredAlbumArt().execute(ImageUtils.getArtworkQuick(file, 300, 300));
            return;
        }
        imageRequest = ImageRequestBuilder.newBuilderWithSource(Uri.parse(albumPath)).setProgressiveRenderingEnabled(true).build();
        ImagePipeline imagePipeline = Fresco.getImagePipeline();
        DataSource<CloseableReference<CloseableImage>> dataSource = imagePipeline.fetchDecodedImage(imageRequest, RadioDetailActivity.this);
        dataSource.subscribe(new BaseBitmapDataSubscriber() {

            @Override
            public void onNewResultImpl(@Nullable Bitmap bitmap) {
                // No need to do any cleanup.
                if (bitmap != null) {
                    new setBlurredAlbumArt().execute(bitmap);
                }
                ;
            }

            @Override
            public void onFailureImpl(DataSource dataSource) {
            // No cleanup required here.
            }
        }, CallerThreadExecutor.getInstance());
    // drawable = Drawable.createFromStream( new URL(albumPath).openStream(),"src");
    } catch (Exception e) {
    }
}
Also used : CloseableReference(com.facebook.common.references.CloseableReference) FileBinaryResource(com.facebook.binaryresource.FileBinaryResource) BaseBitmapDataSubscriber(com.facebook.imagepipeline.datasource.BaseBitmapDataSubscriber) DataSource(com.facebook.datasource.DataSource) Bitmap(android.graphics.Bitmap) BinaryResource(com.facebook.binaryresource.BinaryResource) FileBinaryResource(com.facebook.binaryresource.FileBinaryResource) ImageRequest(com.facebook.imagepipeline.request.ImageRequest) ImagePipeline(com.facebook.imagepipeline.core.ImagePipeline) File(java.io.File) CacheKey(com.facebook.cache.common.CacheKey)

Example 9 with BinaryResource

use of com.facebook.binaryresource.BinaryResource in project SherlockAdapter by EvilBT.

the class FrescoUtil method save.

public static Boolean save(@NonNull String url, @NonNull File outputFile) throws IOException {
    ImagePipelineFactory factory = Fresco.getImagePipelineFactory();
    ImagePipeline pipeline = factory.getImagePipeline();
    boolean isInCache = pipeline.isInDiskCacheSync(Uri.parse(url));
    if (isInCache) {
        BinaryResource resource = factory.getMainFileCache().getResource(new SimpleCacheKey(url));
        if (resource instanceof FileBinaryResource) {
            FileBinaryResource fileResource = (FileBinaryResource) resource;
            FileChannel input = new FileInputStream(fileResource.getFile()).getChannel();
            FileChannel output = new FileOutputStream(outputFile).getChannel();
            output.transferFrom(input, 0, input.size());
            input.close();
            output.close();
            return true;
        }
    }
    boolean isMemoryCache = pipeline.isInBitmapMemoryCache(Uri.parse(url));
    if (!isMemoryCache) {
        return false;
    }
    ImageRequest request = ImageRequestBuilder.newBuilderWithSource(Uri.parse(url)).build();
    DataSource<CloseableReference<CloseableImage>> dataSource = pipeline.fetchImageFromBitmapCache(request, null);
    if (!dataSource.isFinished()) {
        return false;
    }
    CloseableReference<CloseableImage> closeableImageRef = dataSource.getResult();
    Bitmap bitmap = null;
    if (closeableImageRef != null && closeableImageRef.get() instanceof CloseableBitmap) {
        bitmap = ((CloseableBitmap) closeableImageRef.get()).getUnderlyingBitmap();
    }
    if (bitmap == null) {
        return false;
    }
    FileOutputStream outputStream = new FileOutputStream(outputFile);
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
    outputStream.flush();
    outputStream.close();
    return true;
}
Also used : ImagePipelineFactory(com.facebook.imagepipeline.core.ImagePipelineFactory) FileChannel(java.nio.channels.FileChannel) CloseableReference(com.facebook.common.references.CloseableReference) CloseableImage(com.facebook.imagepipeline.image.CloseableImage) FileBinaryResource(com.facebook.binaryresource.FileBinaryResource) FileInputStream(java.io.FileInputStream) CloseableBitmap(com.facebook.imagepipeline.image.CloseableBitmap) CloseableBitmap(com.facebook.imagepipeline.image.CloseableBitmap) Bitmap(android.graphics.Bitmap) BinaryResource(com.facebook.binaryresource.BinaryResource) FileBinaryResource(com.facebook.binaryresource.FileBinaryResource) FileOutputStream(java.io.FileOutputStream) ImageRequest(com.facebook.imagepipeline.request.ImageRequest) SimpleCacheKey(com.facebook.cache.common.SimpleCacheKey) ImagePipeline(com.facebook.imagepipeline.core.ImagePipeline)

Example 10 with BinaryResource

use of com.facebook.binaryresource.BinaryResource in project fresco by facebook.

the class BufferedDiskCache method readFromDiskCache.

/**
 * Performs disk cache read. In case of any exception null is returned.
 */
@Nullable
private PooledByteBuffer readFromDiskCache(final CacheKey key) throws IOException {
    try {
        FLog.v(TAG, "Disk cache read for %s", key.getUriString());
        final BinaryResource diskCacheResource = mFileCache.getResource(key);
        if (diskCacheResource == null) {
            FLog.v(TAG, "Disk cache miss for %s", key.getUriString());
            mImageCacheStatsTracker.onDiskCacheMiss(key);
            return null;
        } else {
            FLog.v(TAG, "Found entry in disk cache for %s", key.getUriString());
            mImageCacheStatsTracker.onDiskCacheHit(key);
        }
        PooledByteBuffer byteBuffer;
        final InputStream is = diskCacheResource.openStream();
        try {
            byteBuffer = mPooledByteBufferFactory.newByteBuffer(is, (int) diskCacheResource.size());
        } finally {
            is.close();
        }
        FLog.v(TAG, "Successful read from disk cache for %s", key.getUriString());
        return byteBuffer;
    } catch (IOException ioe) {
        // TODO: 3697790 log failures
        // TODO: 5258772 - uncomment line below
        // mFileCache.remove(key);
        FLog.w(TAG, ioe, "Exception reading from cache for %s", key.getUriString());
        mImageCacheStatsTracker.onDiskCacheGetFail(key);
        throw ioe;
    }
}
Also used : BinaryResource(com.facebook.binaryresource.BinaryResource) InputStream(java.io.InputStream) PooledByteBuffer(com.facebook.common.memory.PooledByteBuffer) IOException(java.io.IOException) Nullable(javax.annotation.Nullable)

Aggregations

BinaryResource (com.facebook.binaryresource.BinaryResource)13 FileBinaryResource (com.facebook.binaryresource.FileBinaryResource)6 CacheKey (com.facebook.cache.common.CacheKey)5 SimpleCacheKey (com.facebook.cache.common.SimpleCacheKey)5 IOException (java.io.IOException)5 Test (org.junit.Test)5 PrepareOnlyThisForTest (org.powermock.core.classloader.annotations.PrepareOnlyThisForTest)5 File (java.io.File)4 MultiCacheKey (com.facebook.cache.common.MultiCacheKey)3 ImageRequest (com.facebook.imagepipeline.request.ImageRequest)3 Bitmap (android.graphics.Bitmap)2 CloseableReference (com.facebook.common.references.CloseableReference)2 ImagePipeline (com.facebook.imagepipeline.core.ImagePipeline)2 FileInputStream (java.io.FileInputStream)2 InputStream (java.io.InputStream)2 Nullable (javax.annotation.Nullable)2 WriterCallback (com.facebook.cache.common.WriterCallback)1 PooledByteBuffer (com.facebook.common.memory.PooledByteBuffer)1 DataSource (com.facebook.datasource.DataSource)1 ImagePipelineFactory (com.facebook.imagepipeline.core.ImagePipelineFactory)1