Coder Social home page Coder Social logo

Comments (2)

xifan-xf avatar xifan-xf commented on August 14, 2024

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.

from frescodemo.

xifan-xf avatar xifan-xf commented on August 14, 2024

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);

from frescodemo.

Related Issues (5)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.