fresco icon indicating copy to clipboard operation
fresco copied to clipboard

How to get a bitmap with url ? getWindow().setBackgroundDrawable?

Open liufsd opened this issue 11 years ago • 7 comments

HI,in my project .i need to fetch a image to set app window background .so the fresco support it ??? Thanks,

  private void setAppBackground() {
        ImageLoader.getInstance().loadImage(Constants.URL_API_APP_COVER, new DisplayImageOptions.Builder()
                .cacheOnDisk(true)
                .build(), new SimpleImageLoadingListener() {
            @Override
            public void onLoadingStarted(String imageUri, View view) {
                super.onLoadingStarted(imageUri, view);
                try {
                    String path = IOUtils.getAppBackgroundImageFilePath();
                    if (TextUtils.isEmpty(path)) {
                        return;
                    }

                    if (new File(path).exists() && System.currentTimeMillis() - AppKeyStringHelper.getInstance().getAppBackgroundLastUpdateTime() >= 2 * 24 * 60 * 60 * 1000 && CommonUtils.isOnline()) {
                        new File(path).delete();
                        AppKeyStringHelper.getInstance().setAppBackgroundLastUpdateTime(0);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
                super.onLoadingComplete(imageUri, view, loadedImage);
                String path = IOUtils.getAppBackgroundImageFilePath();
                if (TextUtils.isEmpty(path)) {
                    AppKeyStringHelper.getInstance().setAppBackgroundLastUpdateTime(System.currentTimeMillis());
                    return;
                }
                if (new File(path).exists() && AppKeyStringHelper.getInstance().getAppBackgroundLastUpdateTime() == 0) {
                    AppKeyStringHelper.getInstance().setAppBackgroundLastUpdateTime(System.currentTimeMillis());
                }
                try {
                    if (isFinishing()) {
                        return;
                    }
                    final Drawable drawable = new BitmapDrawable(loadedImage);
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if (isFinishing()) {
                                return;
                            }
                            getWindow().setBackgroundDrawable(drawable);
                        }
                    });
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
                super.onLoadingFailed(imageUri, view, failReason);
                try {
                    final Bitmap foundBitmap = ImageLoader.getInstance().loadImageSync("drawable://" + R.drawable.app_background);
                    if (foundBitmap != null) {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                if (!isFinishing()) {
                                    return;
                                }
                                getWindow().setBackgroundDrawable(new BitmapDrawable(foundBitmap));
                                foundBitmap.recycle();
                            }
                        });
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

liufsd avatar Apr 10 '15 07:04 liufsd

I assume this code lives in an activity. If so, you can have something like this:

class MyActivity {
  DraweeHolder<GenericDraweeHierarchy> mDraweeHolder;

  onCreate {
    Resources resources = getContext().getResources();
    Fresco.initialize(getContext());
    GenericDraweeHierarchy gdh = new GenericDraweeHierarchyBuilder(resources)
        .setPlaceholderImage(resources.getDrawable(R.drawable.app_background))
        .build();
    mDraweeHolder = DraweeHolder.create(gdh, getContext());
    getWindow().setBackgroundDrawable(mDraweeHolder.getTopLevelDrawable());
  }

  onStart() {
     ...
    DraweeController controller = Fresco.newDraweeControllerBuilder()
        .setUri(Uri.parse(Constants.URL_API_APP_COVER))
        .build();
     mDraweeHolder.setController(controller);
     mDraweeHolder.attach();
  }

  onStop() {
     ...
     mDraweeHolder.detach();
     mDraweeHolder.setController(null);
  }
}

plamenko avatar Apr 10 '15 15:04 plamenko

Good idea!i will check it! Thanks.

liufsd avatar Apr 10 '15 15:04 liufsd

Great job. But i have one question,my server image will be update ,but url is not change.so the client how to update it ?In my code ,you will find that i will delete old image cache after 2 * 24 * 60 * 60 * 1000.So if i use the Fresco,how o imp it ? Thanks.

liufsd avatar Apr 15 '15 05:04 liufsd

Unfortunately, we do not support invalidating/removing the files from disk cache yet.

plamenko avatar Apr 16 '15 00:04 plamenko

oh. But i think most of apps need this feature.

liufsd avatar Apr 16 '15 01:04 liufsd

https://github.com/facebook/fresco/issues/124 tracks that

plamenko avatar Apr 17 '15 01:04 plamenko

oh.Thanks.

liufsd avatar Apr 17 '15 01:04 liufsd