How to get a bitmap with url ? getWindow().setBackgroundDrawable?
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();
}
}
});
}
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);
}
}
Good idea!i will check it! Thanks.
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.
Unfortunately, we do not support invalidating/removing the files from disk cache yet.
oh. But i think most of apps need this feature.
https://github.com/facebook/fresco/issues/124 tracks that
oh.Thanks.