06peng / FrescoDemo

A demo use Fresco to load image and base on Chris Banes' s Android Design library
428 stars 106 forks source link

when use PooledByteBuffer Issue #2

Closed xifan-xf closed 8 years ago

xifan-xf commented 8 years ago

The code in fresco BaseControllerListener 's onFinalImageSetmethod

bytes = dataSource.getResult();
if (bytes != null) {
    ......

when load a largebitmap once ,then bytes alway return null. it just happen when the image is too large.

xifan-xf commented 8 years ago

In class SubsamplingScaleActivity you can set the url with a large jpg

http://att2.citysbs.com/hangzhou/2013/10/04/18/2523x3792-181028_20691380881428199_86191fef36fb0a7d82d22bf2e5164cde.jpg

when the image load once then you press back and reopen this activity again , it will never show.

xifan-xf commented 8 years ago

Ok,I sloved this issue.According to Fresco 's Doc dataSource only can used with subscribe to getResult .so here is my code in SubsamplingScaleImageView method setImageUri:

 ImageRequest imageRequest = ImageRequestBuilder.newBuilderWithSource(Uri.parse(url)).build();
        ImagePipeline imagePipeline = Fresco.getImagePipeline();
        DataSource<CloseableReference<PooledByteBuffer>> dataSource = imagePipeline.fetchEncodedImage(imageRequest, this);
        DraweeController controller = Fresco.newDraweeControllerBuilder()
                .setOldController(mDraweeHolder.getController())
                .setImageRequest(imageRequest)
                .build();
        DataSubscriber<CloseableReference<PooledByteBuffer>> dataSubscriber =
                new BaseDataSubscriber<CloseableReference<PooledByteBuffer>>() {
                    @Override
                    protected void onNewResultImpl(
                            DataSource<CloseableReference<PooledByteBuffer>> dataSource) {
                        if (!dataSource.isFinished()) {
                            return;
                        }
                        CloseableReference<PooledByteBuffer> bytes = dataSource.getResult();
                        if (bytes != null) {
                            PooledByteBuffer pooledByteBuffer = bytes.get();
                            PooledByteBufferInputStream sourceIs = new PooledByteBufferInputStream(pooledByteBuffer);
                            try {
                                BufferedInputStream bis = new BufferedInputStream(sourceIs);
                                String filename = String.valueOf(url.hashCode());
                                FileCache fileCache = new FileCache(getContext());
                                final File f = new File(fileCache.getCacheDir(), filename);
                                OutputStream os = null;
                                try {
                                    f.createNewFile();
                                    os = new FileOutputStream(f);
                                    byte[] buffer = new byte[1024];
                                    int bufferLength;
                                    while ((bufferLength = bis.read(buffer)) != -1) {
                                        os.write(buffer, 0, bufferLength);
                                    }
                                } catch (IOException e) {
                                    e.printStackTrace();
                                } finally {
                                    if (os != null) {
                                        try {
                                            os.close();
                                        } catch (IOException e) {
                                            e.printStackTrace();
                                        }
                                    }
                                    if (bis != null) {
                                        try {
                                            bis.close();
                                        } catch (IOException e) {
                                            e.printStackTrace();
                                        }
                                    }
                                }

                                new Handler(Looper.getMainLooper()).post(new Runnable() {
                                    @Override
                                    public void run() {
                                        final int wh[] = ImageHelp.getWithHeight(f.getAbsolutePath());
                                        setImage(ImageSource.uri(f.getAbsolutePath()));
                                        setMaxScale(SubsamplingScaleImageView.SCALE_TYPE_CUSTOM);
                                        int width = getWidth();
                                        int height = getHeight();
                                        if (width == 0 || height == 0) {
                                            width = DisplayUtil.getScreenWidth();
                                            height = DisplayUtil.getScreenHeight();
                                        }
                                        if ((wh[1]> height) && wh[1] / wh[0] > height / width) {
                                            PointF center = new PointF(getSWidth() / 2, 0);
                                            float targetScale = Math.max(width / (float) wh[0], height / (float)wh[1]);
                                            setScaleAndCenter(targetScale, center);
                                        }
                                    }
                                });
                            } catch (Exception e) {
                                e.printStackTrace();
                            } finally {
                                dataSource.close();
                                CloseableReference.closeSafely(bytes);
                            }
                        }
                    }

                    @Override
                    protected void onFailureImpl(
                            DataSource<CloseableReference<PooledByteBuffer>> dataSource) {
                    }
                };
        dataSource.subscribe(dataSubscriber, CallerThreadExecutor.getInstance());
        mDraweeHolder.setController(controller);