{"action":"created","issue":{"url":"https://api.github.com/repos/facebook/fresco/issues/1551","repository_url":"https://api.github.com/repos/facebook/fresco","labels_url":"https://api.github.com/repos/facebook/fresco/issues/1551/labels{/name}","comments_url":"https://api.github.com/repos/facebook/fresco/issues/1551/comments","events_url":"https://api.github.com/repos/facebook/fresco/issues/1551/events","html_url":"https://github.com/facebook/fresco/issues/1551","id":190976243,"number":1551,"title":"How do I judge the image has been really downloaded(cached)","user":{"login":"Freshman111","id":9150631,"avatar_url":"https://avatars.githubusercontent.com/u/9150631?v=3","gravatar_id":"","url":"https://api.github.com/users/Freshman111","html_url":"https://github.com/Freshman111","followers_url":"https://api.github.com/users/Freshman111/followers","following_url":"https://api.github.com/users/Freshman111/following{/other_user}","gists_url":"https://api.github.com/users/Freshman111/gists{/gist_id}","starred_url":"https://api.github.com/users/Freshman111/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Freshman111/subscriptions","organizations_url":"https://api.github.com/users/Freshman111/orgs","repos_url":"https://api.github.com/users/Freshman111/repos","events_url":"https://api.github.com/users/Freshman111/events{/privacy}","received_events_url":"https://api.github.com/users/Freshman111/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":2,"created_at":"2016-11-22T11:36:21Z","updated_at":"2016-11-23T02:36:04Z","closed_at":null,"body":"Hello\r\n\r\nI use fresco to download image in my project,and then I need to distinguish whether the image is a gif or a big image.\r\nI found these linsk from official documentation :\r\n[http://frescolib.org/docs/using-image-pipeline.html#_](url)\r\n[http://frescolib.org/docs/caching.html#_](url)\r\n\r\nSo I writed my own DownloadSubscriber according to the above:\r\n\r\n```\r\npublic abstract class ImageDownloadSubscriber extends BaseDataSubscriber<CloseableReference<PooledByteBuffer>> {\r\n\r\n    private volatile boolean mFinished;\r\n    private File tempFile;\r\n\r\n    private ImageRequest imageRequest;\r\n\r\n    public ImageDownloadSubscriber(ImageRequest imageRequest) {\r\n        this.imageRequest = imageRequest;\r\n    }\r\n\r\n    @Override\r\n    public void onProgressUpdate(DataSource<CloseableReference<PooledByteBuffer>> dataSource) {\r\n        if (!mFinished) {\r\n            onProgress((int) (dataSource.getProgress() * 100));\r\n        }\r\n    }\r\n\r\n    @Override\r\n    protected void onNewResultImpl(DataSource<CloseableReference<PooledByteBuffer>> dataSource) {\r\n        if (!dataSource.isFinished()) {\r\n            AppLog.e(\"====onNewResultImpl====isFinished==return\");\r\n            return;\r\n        }\r\n        CloseableReference<PooledByteBuffer> pooledByteBuffer = dataSource.getResult();\r\n        if (pooledByteBuffer == null) {\r\n            AppLog.e(\"====onNewResultImpl===pooledByteBuffer===return\");\r\n            return;\r\n        }\r\n        final PooledByteBufferInputStream is = new PooledByteBufferInputStream(pooledByteBuffer.get());\r\n        try {\r\n            CacheKey cacheKey = DefaultCacheKeyFactory.getInstance()\r\n                    .getEncodedCacheKey(imageRequest, null);\r\n            ImagePipelineFactory.getInstance().getMainFileCache().insert(cacheKey, new WriterCallback() {\r\n                @Override\r\n                public void write(OutputStream os) throws IOException {\r\n                    byte data[] = new byte[16 * 1024];\r\n                    int count;\r\n                    while ((count = is.read(data)) != -1) {\r\n                        os.write(data, 0, count);\r\n                    }\r\n                    tempFile = getCacheResource();\r\n                    mFinished = true;\r\n                    onSuccess(tempFile);\r\n                }\r\n            });\r\n        } catch (IOException e) {\r\n            onFail(new Throwable(e));\r\n        } finally {\r\n            try {\r\n                if (is != null) {\r\n                    is.close();\r\n                }\r\n            } catch (IOException e) {\r\n                onFail(new Throwable(e));\r\n            }\r\n            CloseableReference.closeSafely(pooledByteBuffer);\r\n        }\r\n    }\r\n\r\n    @Override\r\n    protected void onFailureImpl(DataSource<CloseableReference<PooledByteBuffer>> dataSource) {\r\n        mFinished = true;\r\n        onFail(new RuntimeException(\"onFailureImpl\"));\r\n    }\r\n\r\n    /**\r\n     * 获取cache文件\r\n     *\r\n     * @return File\r\n     */\r\n    private File getCacheResource() {\r\n        if (imageRequest == null) {\r\n            return null;\r\n        }\r\n        FileCache mainFileCache = ImagePipelineFactory\r\n                .getInstance()\r\n                .getMainFileCache();\r\n        CacheKey cacheKey = DefaultCacheKeyFactory.getInstance()\r\n                .getEncodedCacheKey(imageRequest, false);\r\n        File cacheFile = null;\r\n        if (mainFileCache.hasKey(cacheKey)){\r\n            cacheFile=((FileBinaryResource)mainFileCache.getResource(cacheKey)).getFile();\r\n            return cacheFile;\r\n        }\r\n        AppLog.e(\"===getCacheResource=tempFile======\");\r\n        return null;\r\n    }\r\n\r\n    @WorkerThread\r\n    protected abstract void onProgress(int progress);\r\n\r\n    @WorkerThread\r\n    protected abstract void onSuccess(File image);\r\n\r\n    @WorkerThread\r\n    protected abstract void onFail(Throwable t);\r\n}\r\n```\r\nuse \r\n```\r\nImageRequest imageRequest = ImageRequestBuilder\r\n                    .newBuilderWithSource(Uri.parse(AppImage.formatUrl(absUrl)))\r\n                    .setLowestPermittedRequestLevel(ImageRequest.RequestLevel.FULL_FETCH)\r\n                    .setCacheChoice(ImageRequest.CacheChoice.DEFAULT)\r\n                    .build();\r\n            File imageFile = getCacheFile(imageRequest);\r\n            if (imageFile.exists()) {\r\n                handler.sendEmptyMessage(2);\r\n                imagePath = imageFile.getAbsolutePath();\r\n                Uri uri = Uri.fromFile(imageFile);\r\n                Message msg = handler.obtainMessage(3);\r\n                msg.obj = uri;\r\n                handler.sendMessage(msg);\r\n                return;\r\n            }\r\n            //Download\r\n            Fresco.getImagePipeline().prefetchToDiskCache(imageRequest, false);\r\n            //monitor\r\n            DataSource<CloseableReference<PooledByteBuffer>> source = Fresco.getImagePipeline().fetchEncodedImage(imageRequest, true);\r\n            source.subscribe(new ImageDownloadSubscriber(imageRequest) {\r\n\r\n                @Override\r\n                protected void onProgress(int progress) {\r\n                }\r\n\r\n                @Override\r\n                protected void onSuccess(File image) {\r\n                    handler.sendEmptyMessage(2);\r\n                    if (image != null) {\r\n                        AppLog.d(\"ImageDownloadSubscriber image path:\" + image.getAbsolutePath());\r\n                        imagePath = image.getAbsolutePath();\r\n                        Uri uri = Uri.fromFile(image);\r\n                        Message msg = handler.obtainMessage(3);\r\n                        msg.obj = uri;\r\n                        handler.sendMessage(msg);\r\n                    } else {\r\n                        AppLog.d(\"ImageDownloadSubscriber image is null:\");\r\n                        handler.sendEmptyMessage(4);\r\n                    }\r\n                }\r\n\r\n                @Override\r\n                protected void onFail(Throwable t) {\r\n                    AppLog.d(\"ImageDownloadSubscriber errMsg:\" + t.getLocalizedMessage());\r\n                    handler.sendEmptyMessage(2);\r\n                    handler.sendEmptyMessage(4);\r\n                }\r\n            }, new DefaultExecutorSupplier(Runtime.getRuntime().availableProcessors()).forBackgroundTasks());\r\n        \r\n\r\nprivate File getCacheFile(final ImageRequest request) {\r\n        FileCache mainFileCache = ImagePipelineFactory\r\n                .getInstance()\r\n                .getMainFileCache();\r\n        final CacheKey cacheKey = DefaultCacheKeyFactory\r\n                .getInstance()\r\n                .getEncodedCacheKey(request, false);\r\n        File cacheFile = request.getSourceFile();\r\n        if (mainFileCache.hasKey(cacheKey)) {\r\n            cacheFile = ((FileBinaryResource) mainFileCache.getResource(cacheKey)).getFile();\r\n        }\r\n        return cacheFile;\r\n    }\r\n```\r\nIn most cases it is work properly,but sometimes the file from callback method of onSuccess(File image) image is null,and sometimes the file is not null but these codes \r\n```\r\nUri uri = Uri.fromFile(image);\r\n InputStream is = null;\r\n        try {\r\n            is = new FileInputStream(uri.getPath());\r\n        } catch (FileNotFoundException e) {\r\n            AppLog.e(\"getImageType errMsg:\" + e.getLocalizedMessage());\r\n        }\r\n```\r\n catch FileNotFoundException.\r\n\r\nIn the case of these failures,when I load the image second time,File imageFile = getCacheFile(imageRequest); the imageFile.exists() is true,It proves that the image has been cached successful. How do I monitor or judge the image has been really cached and get a not null imagefile\r\n\r\n"},"comment":{"url":"https://api.github.com/repos/facebook/fresco/issues/comments/262422680","html_url":"https://github.com/facebook/fresco/issues/1551#issuecomment-262422680","issue_url":"https://api.github.com/repos/facebook/fresco/issues/1551","id":262422680,"user":{"login":"yy1300326388","id":8764899,"avatar_url":"https://avatars.githubusercontent.com/u/8764899?v=3","gravatar_id":"","url":"https://api.github.com/users/yy1300326388","html_url":"https://github.com/yy1300326388","followers_url":"https://api.github.com/users/yy1300326388/followers","following_url":"https://api.github.com/users/yy1300326388/following{/other_user}","gists_url":"https://api.github.com/users/yy1300326388/gists{/gist_id}","starred_url":"https://api.github.com/users/yy1300326388/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/yy1300326388/subscriptions","organizations_url":"https://api.github.com/users/yy1300326388/orgs","repos_url":"https://api.github.com/users/yy1300326388/repos","events_url":"https://api.github.com/users/yy1300326388/events{/privacy}","received_events_url":"https://api.github.com/users/yy1300326388/received_events","type":"User","site_admin":false},"created_at":"2016-11-23T02:36:04Z","updated_at":"2016-11-23T02:36:04Z","body":"Hi @kirwan \r\n\r\nI need the hard disk cache to listener for events。\r\n\r\n```java\r\nFresco.getImagePipeline().prefetchToDiskCache(imageRequest, false);\r\n```\r\n\r\n"}}
{"actor":{"display_login":"yy1300326388"}}