Java Code Examples for com.facebook.binaryresource.FileBinaryResource

The following examples show how to use com.facebook.binaryresource.FileBinaryResource. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: materialup   Source File: ShareDribbbleImageTask.java    License: Apache License 2.0 6 votes vote down vote up
@Override
    protected File doInBackground(Void... params) {
        final String url = shot.getTeaserUrl();
        try {
            ImageRequest imageRequest = ImageRequest.fromUri(url);
            CacheKey cacheKey = DefaultCacheKeyFactory.getInstance().getEncodedCacheKey(imageRequest);
//            ImagePipeline imagePipeline = Fresco.getImagePipeline();
//            imagePipeline.prefetchToDiskCache(imageRequest,activity);
            BinaryResource resource = ImagePipelineFactory.getInstance().getMainDiskStorageCache().getResource(cacheKey);
            File file = ((FileBinaryResource) resource).getFile();

            String fileName = url;
            fileName = fileName.substring(fileName.lastIndexOf('/') + 1);
            File renamed = new File(file.getParent(), fileName);
            if (!renamed.exists()) {
                FileUtil.copy(file, renamed);
            }
            return renamed;
        } catch (Exception ex) {
            Log.w("SHARE", "Sharing " + url + " failed", ex);
            return null;
        }
    }
 
Example 2
@Override
public FileBinaryResource createTemporary(
    String resourceId,
    Object debugInfo)
    throws IOException {
  // ensure that the parent directory exists
  FileInfo info = new FileInfo(FileType.TEMP, resourceId);
  File parent = getSubdirectory(info.resourceId);
  if (!parent.exists()) {
    mkdirs(parent, "createTemporary");
  }

  try {
    File file = info.createTempFile(parent);
    return FileBinaryResource.createOrNull(file);
  } catch (IOException ioe) {
    mCacheErrorLogger.logError(
        CacheErrorLogger.CacheErrorCategory.WRITE_CREATE_TEMPFILE,
        TAG,
        "createTemporary",
        ioe);
    throw ioe;
  }
}
 
Example 3
@Override
public void viewImageMedia(int position, boolean loaded) {
    if(loaded){
        PostItem item = getItem(position);
        CacheKey cacheKey = DefaultCacheKeyFactory.getInstance()
                .getEncodedCacheKey(ImageRequest
                        .fromUri(Uri.parse(item.getUrl())));
        if(cacheKey != null){
            BinaryResource resource = ImagePipelineFactory.getInstance().getMainDiskStorageCache().getResource(cacheKey);

            File localFile;
            if(resource != null){
                localFile = ((FileBinaryResource) resource).getFile();

                Bundle bundle = new Bundle();

                bundle.putString(getResources().getString(R.string.local_cache_key), localFile.getPath());

                bundle.putString(getResources().getString(R.string.main_data_key), gson.toJson(item));

                ((SlidingUpPanelActivity)context).setPanelView(Fragments.IMAGE_PREVIEW, bundle);
            }
        }
    }
}
 
Example 4
private void displayCachedImageFromBackgroundThread(ImageRequest request){
    CacheKey cacheKey = DefaultCacheKeyFactory.getInstance().getEncodedCacheKey(ImageRequest.fromUri(request.getSourceUri()));

    if(cacheKey != null){
        BinaryResource resource = ImagePipelineFactory.getInstance().getMainDiskStorageCache().getResource(cacheKey);
        if(resource != null){
            File localFile = ((FileBinaryResource) resource).getFile();
            if(localFile != null){
                Handler handler = new Handler(Looper.getMainLooper());
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        imagePreview.setImage(ImageSource.uri(localFile.getPath()));
                    }
                });
            }
        }

    }
}
 
Example 5
Source Project: fresco   Source File: DefaultDiskStorageTest.java    License: MIT License 6 votes vote down vote up
@Test
public void testBasicOperations() throws Exception {
  DefaultDiskStorage storage = getStorageSupplier(1).get();
  final String resourceId1 = "R1";
  final String resourceId2 = "R2";

  // no file - get should fail
  BinaryResource resource1 = storage.getResource(resourceId1, null);
  Assert.assertNull(resource1);

  // write out the file now
  byte[] key1Contents = new byte[] {0, 1, 2};
  writeToStorage(storage, resourceId1, key1Contents);
  // get should succeed now
  resource1 = storage.getResource(resourceId1, null);
  Assert.assertNotNull(resource1);
  File underlyingFile = ((FileBinaryResource) resource1).getFile();
  Assert.assertArrayEquals(key1Contents, Files.toByteArray(underlyingFile));
  // remove the file now - get should fail again
  Assert.assertTrue(underlyingFile.delete());
  resource1 = storage.getResource(resourceId1, null);
  Assert.assertNull(resource1);
  // no file
  BinaryResource resource2 = storage.getResource(resourceId2, null);
  Assert.assertNull(resource2);
}
 
Example 6
Source Project: ImageLoader   Source File: BigImageLoader.java    License: Apache License 2.0 5 votes vote down vote up
private File getCacheFile(final ImageRequest request) {
    FileCache mainFileCache = ImagePipelineFactory
            .getInstance()
            .getMainFileCache();
    final CacheKey cacheKey = DefaultCacheKeyFactory
            .getInstance()
            .getEncodedCacheKey(request, false); // we don't need context, but avoid null
    File cacheFile = request.getSourceFile();
    // http://crashes.to/s/ee10638fb31
    if (mainFileCache.hasKey(cacheKey) && mainFileCache.getResource(cacheKey) != null) {
        cacheFile = ((FileBinaryResource) mainFileCache.getResource(cacheKey)).getFile();
    }
    return cacheFile;
}
 
Example 7
Source Project: LiuAGeAndroid   Source File: FileCacheUtils.java    License: MIT License 5 votes vote down vote up
/**
 * 获取磁盘缓存的文件路径
 *
 * @param url 文件url
 * @return 文件路径
 */
private static String getDiskCacheFilePath(String url) {
    FileBinaryResource resource = (FileBinaryResource) Fresco.getImagePipelineFactory()
            .getMainFileCache()
            .getResource(new SimpleCacheKey(url));
    // 防止中途清除磁盘缓存 导致获取不到
    if (resource == null) {
        return "";
    }
    File file = resource.getFile();
    return file.getAbsolutePath();
}
 
Example 8
Source Project: BaoKanAndroid   Source File: FileCacheUtils.java    License: MIT License 5 votes vote down vote up
/**
 * 获取磁盘缓存的文件路径
 *
 * @param url 文件url
 * @return 文件路径
 */
private static String getDiskCacheFilePath(String url) {
    FileBinaryResource resource = (FileBinaryResource) Fresco.getImagePipelineFactory()
            .getMainFileCache()
            .getResource(new SimpleCacheKey(url));
    // 防止中途清除磁盘缓存 导致获取不到
    if (resource == null) {
        return "";
    }
    File file = resource.getFile();
    return file.getAbsolutePath();
}
 
Example 9
Source Project: materialup   Source File: PhotoActivity.java    License: Apache License 2.0 5 votes vote down vote up
private void save() {
        try {
            ImageRequest imageRequest = ImageRequest.fromUri(mUrl);
            CacheKey cacheKey = DefaultCacheKeyFactory.getInstance().getEncodedCacheKey(imageRequest);
            BinaryResource resource = ImagePipelineFactory.getInstance().getMainDiskStorageCache().getResource(cacheKey);
            File file = ((FileBinaryResource) resource).getFile();

            String fileName = mUrl;
            fileName = fileName.substring(fileName.lastIndexOf('/') + 1);
            if (mTitle != null) {
                fileName = mTitle + fileName;
            }
            File pic = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
            File dir = new File(pic, "material/");
            if (!dir.exists()) {
                dir.mkdirs();
            }
            File renamed = new File(dir, fileName);
            if (!renamed.exists()) {
                renamed.createNewFile();
                FileUtil.copy(file, renamed);
            }
            UI.showToast(this, getString(R.string.image_saved_to, renamed.getAbsolutePath()));
//            Snackbar.make(mDraweeView,R.string.image_is_saved, Snackbar.LENGTH_LONG);
        } catch (Exception ex) {
            Log.w("SHARE", "Sharing " + mUrl + " failed", ex);
        }
    }
 
Example 10
Source Project: FrescoUtils   Source File: FrescoHelper.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 本地缓存文件
 */
public static File getCache(Context context, Uri uri) {
    if (!isCached(context, uri))
        return null;
    ImageRequest imageRequest = ImageRequest.fromUri(uri);
    CacheKey cacheKey = DefaultCacheKeyFactory.getInstance()
            .getEncodedCacheKey(imageRequest, context);
    BinaryResource resource = ImagePipelineFactory.getInstance()
            .getMainFileCache().getResource(cacheKey);
    File file = ((FileBinaryResource) resource).getFile();
    return file;
}
 
Example 11
private void deleteTemporaryResource(BinaryResource temporaryResource) {
  if (!(temporaryResource instanceof FileBinaryResource)) {
    return;
  }
  FileBinaryResource fileResource = (FileBinaryResource)temporaryResource;
  File tempFile = fileResource.getFile();

  if (tempFile.exists()) {
    FLog.e(TAG, "Temp file still on disk: %s ", tempFile);
    if (!tempFile.delete()) {
      FLog.e(TAG, "Failed to delete temp file: %s", tempFile);
    }
  }
}
 
Example 12
@Override
public FileBinaryResource commit(String resourceId, BinaryResource temporary, Object debugInfo)
    throws IOException {
  // will cause a class-cast exception
  FileBinaryResource tempFileResource = (FileBinaryResource) temporary;

  File tempFile = tempFileResource.getFile();
  File targetFile = getContentFileFor(resourceId);

  try {
    FileUtils.rename(tempFile, targetFile);
  } catch (FileUtils.RenameException re) {
    CacheErrorLogger.CacheErrorCategory category;
    Throwable cause = re.getCause();
    if (cause == null) {
      category = CacheErrorLogger.CacheErrorCategory.WRITE_RENAME_FILE_OTHER;
    } else if (cause instanceof FileUtils.ParentDirNotFoundException) {
      category =
          CacheErrorLogger.CacheErrorCategory.WRITE_RENAME_FILE_TEMPFILE_PARENT_NOT_FOUND;
    } else if (cause instanceof FileNotFoundException) {
      category = CacheErrorLogger.CacheErrorCategory.WRITE_RENAME_FILE_TEMPFILE_NOT_FOUND;
    } else {
      category = CacheErrorLogger.CacheErrorCategory.WRITE_RENAME_FILE_OTHER;
    }
    mCacheErrorLogger.logError(
        category,
        TAG,
        "commit",
        re);
    throw re;
  }
  if (targetFile.exists()) {
    targetFile.setLastModified(mClock.now());
  }
  return FileBinaryResource.createOrNull(targetFile);
}
 
Example 13
@Override
public FileBinaryResource getResource(String resourceId, Object debugInfo) {
  final File file = getContentFileFor(resourceId);
  if (file.exists()) {
    file.setLastModified(mClock.now());
    return FileBinaryResource.createOrNull(file);
  }
  return null;
}
 
Example 14
@Override
public long remove(Entry entry) {
  // it should be one entry return by us :)
  EntryImpl entryImpl = (EntryImpl) entry;
  FileBinaryResource resource = entryImpl.getResource();
  return doRemove(resource.getFile());
}
 
Example 15
Source Project: fresco   Source File: DefaultDiskStorage.java    License: MIT License 5 votes vote down vote up
@Override
public @Nullable BinaryResource getResource(String resourceId, Object debugInfo) {
  final File file = getContentFileFor(resourceId);
  if (file.exists()) {
    file.setLastModified(mClock.now());
    return FileBinaryResource.createOrNull(file);
  }
  return null;
}
 
Example 16
Source Project: fresco   Source File: DefaultDiskStorage.java    License: MIT License 5 votes vote down vote up
@Override
public long remove(Entry entry) {
  // it should be one entry return by us :)
  EntryImpl entryImpl = (EntryImpl) entry;
  FileBinaryResource resource = entryImpl.getResource();
  return doRemove(resource.getFile());
}
 
Example 17
Source Project: fresco   Source File: DefaultDiskStorage.java    License: MIT License 5 votes vote down vote up
private EntryImpl(String id, File cachedFile) {
  Preconditions.checkNotNull(cachedFile);
  this.id = Preconditions.checkNotNull(id);
  this.resource = FileBinaryResource.createOrNull(cachedFile);
  this.size = -1;
  this.timestamp = -1;
}
 
Example 18
Source Project: fresco   Source File: DefaultDiskStorage.java    License: MIT License 5 votes vote down vote up
@Override
public BinaryResource commit(Object debugInfo) throws IOException {
  // the temp resource must be ours!
  File targetFile = getContentFileFor(mResourceId);

  try {
    FileUtils.rename(mTemporaryFile, targetFile);
  } catch (FileUtils.RenameException re) {
    CacheErrorLogger.CacheErrorCategory category;
    Throwable cause = re.getCause();
    if (cause == null) {
      category = CacheErrorLogger.CacheErrorCategory.WRITE_RENAME_FILE_OTHER;
    } else if (cause instanceof FileUtils.ParentDirNotFoundException) {
      category =
          CacheErrorLogger.CacheErrorCategory.WRITE_RENAME_FILE_TEMPFILE_PARENT_NOT_FOUND;
    } else if (cause instanceof FileNotFoundException) {
      category = CacheErrorLogger.CacheErrorCategory.WRITE_RENAME_FILE_TEMPFILE_NOT_FOUND;
    } else {
      category = CacheErrorLogger.CacheErrorCategory.WRITE_RENAME_FILE_OTHER;
    }
    mCacheErrorLogger.logError(category, TAG, "commit", re);
    throw re;
  }
  if (targetFile.exists()) {
    targetFile.setLastModified(mClock.now());
  }
  return FileBinaryResource.createOrNull(targetFile);
}
 
Example 19
Source Project: fresco   Source File: DefaultDiskStorageTest.java    License: MIT License 5 votes vote down vote up
private static FileBinaryResource writeToStorage(
    final DefaultDiskStorage storage, final String resourceId, final byte[] value)
    throws IOException {
  DiskStorage.Inserter inserter = storage.insert(resourceId, null);
  writeToResource(inserter, value);
  return (FileBinaryResource) inserter.commit(null);
}
 
Example 20
Source Project: fresco   Source File: DefaultDiskStorageTest.java    License: MIT License 5 votes vote down vote up
private static File write(DefaultDiskStorage storage, String resourceId, byte[] content)
    throws IOException {
  DiskStorage.Inserter inserter = storage.insert(resourceId, null);
  File file = ((DefaultDiskStorage.InserterImpl) inserter).mTemporaryFile;
  FileOutputStream fos = new FileOutputStream(file);
  try {
    fos.write(content);
  } finally {
    fos.close();
  }
  return ((FileBinaryResource) inserter.commit(null)).getFile();
}
 
Example 21
private EntryImpl(File cachedFile) {
  Preconditions.checkNotNull(cachedFile);
  this.resource = FileBinaryResource.createOrNull(cachedFile);
  this.size = -1;
  this.timestamp = -1;
}
 
Example 22
@Override
public FileBinaryResource getResource() {
  return resource;
}
 
Example 23
Source Project: fresco   Source File: DefaultDiskStorage.java    License: MIT License 4 votes vote down vote up
@Override
public FileBinaryResource getResource() {
  return resource;
}