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));
}
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();
}
}
}
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) {
}
}
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;
}
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;
}
}
Aggregations