Android Open Source - image-loader Basic File Manager






From Project

Back to project page image-loader.

License

The source code is released under:

Apache License

If you think the Android project image-loader listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/**
 * Copyright 2012 Novoda Ltd//www  . ja v a 2s . c  om
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.novoda.imageloader.core.file;

import android.graphics.Bitmap;

import com.novoda.imageloader.core.LoaderSettings;
import com.novoda.imageloader.core.file.util.FileUtil;
import com.novoda.imageloader.core.network.UrlUtil;
import com.novoda.imageloader.core.util.Log;

import java.io.File;
import java.io.FileOutputStream;

/**
 * This is a basic implementation for the file manager.
 * On Startup it is running a cleanup of all the files in the cache, and removing
 * old images based on the expirationPeriod.
 */
public class BasicFileManager implements FileManager {

    private LoaderSettings loaderSettings;

    public BasicFileManager(LoaderSettings settings) {
        this.loaderSettings = settings;
        if (settings.isCleanOnSetup()) {
            cleanOldFiles();
        }
    }

    /**
     * Clean is removing all the files in the cache directory.
     */
    @Override
    public void clean() {
        deleteOldFiles(-1);
    }

    /**
     * CleanOldFile is removing all the files in the cache directory where the
     * timestamp is older then the expiration time.
     */
    @Override
    public void cleanOldFiles() {
        deleteOldFiles(loaderSettings.getExpirationPeriod());
    }

    @Override
    public String getFilePath(String imageUrl) {
        File f = getFile(imageUrl);
        if (f.exists()) {
            return f.getAbsolutePath();
        }
        return null;
    }

    @Override
    public void saveBitmap(String fileName, Bitmap b, int width, int height) {
        try {
            FileOutputStream out = new FileOutputStream(fileName + "-" + width + "x" + height);
            b.compress(Bitmap.CompressFormat.PNG, 90, out);
        } catch (Exception e) {
            Log.warning("" + e.getMessage());
        }
    }

    @Override
    public File getFile(String url) {
        url = processUrl(url);
        String filename = String.valueOf(url.hashCode());
        return new File(loaderSettings.getCacheDir(), filename);
    }

    @Override
    public File getFile(String url, int width, int height) {
        url = processUrl(url);
        String filename = url.hashCode() + "-" + width + "x" + height;
        return new File(loaderSettings.getCacheDir(), filename);
    }

    private String processUrl(String url) {
        if (loaderSettings.isQueryIncludedInHash()) {
            return url;
        }
        return new UrlUtil().removeQuery(url);
    }

    private void deleteOldFiles(final long expirationPeriod) {
        final String cacheDir = loaderSettings.getCacheDir().getAbsolutePath();
        Thread cleaner = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    new FileUtil().reduceFileCache(cacheDir, expirationPeriod);
                } catch (Throwable t) {
                    // Don't have to fail in case there
                }
            }
        });
        cleaner.setPriority(android.os.Process.THREAD_PRIORITY_BACKGROUND);
        cleaner.start();
    }

}




Java Source Code List

com.novoda.imageloader.acceptance.BitmapUtilsInstrumentationTest.java
com.novoda.imageloader.acceptance.BitmapUtilsShould.java
com.novoda.imageloader.acceptance.ImageLoaderDemoActivityTest.java
com.novoda.imageloader.acceptance.ImageManagerInstrumentationTest.java
com.novoda.imageloader.acceptance.LruBitmapCacheInstrumentationTest.java
com.novoda.imageloader.core.ImageManager.java
com.novoda.imageloader.core.LoaderContext.java
com.novoda.imageloader.core.LoaderSettings.java
com.novoda.imageloader.core.OnImageLoadedListener.java
com.novoda.imageloader.core.bitmap.BitmapUtil.java
com.novoda.imageloader.core.cache.CacheManager.java
com.novoda.imageloader.core.cache.LruBitmapCache.java
com.novoda.imageloader.core.cache.NoCache.java
com.novoda.imageloader.core.cache.SoftMapCache.java
com.novoda.imageloader.core.cache.util.LruCache.java
com.novoda.imageloader.core.exception.ImageCopyException.java
com.novoda.imageloader.core.exception.ImageNotFoundException.java
com.novoda.imageloader.core.exception.MissingSettingException.java
com.novoda.imageloader.core.file.BasicFileManager.java
com.novoda.imageloader.core.file.FileManager.java
com.novoda.imageloader.core.file.util.AndroidFileContext.java
com.novoda.imageloader.core.file.util.FileUtil.java
com.novoda.imageloader.core.file.util.FlushedInputStream.java
com.novoda.imageloader.core.loader.ConcurrentLoader.java
com.novoda.imageloader.core.loader.Loader.java
com.novoda.imageloader.core.loader.SimpleLoader.java
com.novoda.imageloader.core.loader.util.AsyncResult.java
com.novoda.imageloader.core.loader.util.AsyncTask.java
com.novoda.imageloader.core.loader.util.BitmapDisplayer.java
com.novoda.imageloader.core.loader.util.BitmapRetriever.java
com.novoda.imageloader.core.loader.util.LoaderTask.java
com.novoda.imageloader.core.loader.util.SingleThreadedLoader.java
com.novoda.imageloader.core.model.ImageTagFactory.java
com.novoda.imageloader.core.model.ImageTag.java
com.novoda.imageloader.core.model.ImageWrapper.java
com.novoda.imageloader.core.network.NetworkManager.java
com.novoda.imageloader.core.network.UrlNetworkManager.java
com.novoda.imageloader.core.network.UrlUtil.java
com.novoda.imageloader.core.util.AnimationHelper.java
com.novoda.imageloader.core.util.DirectLoader.java
com.novoda.imageloader.core.util.Log.java
com.novoda.imageloader.demo.DemoApplication.java
com.novoda.imageloader.demo.activity.BigImages.java
com.novoda.imageloader.demo.activity.Demos.java
com.novoda.imageloader.demo.activity.DirectLoading.java
com.novoda.imageloader.demo.activity.ImageLongList.java
com.novoda.imageloader.demo.activity.LongSmallImageList.java
com.novoda.imageloader.demo.activity.base.ImageLoaderBaseActivity.java
com.novoda.imageloader.demo.provider.CustomUriMatcher.java
com.novoda.imageloader.demo.provider.DatabaseManager.java
com.novoda.imageloader.demo.provider.ImageLoaderDemoProvider.java
com.novoda.imageloader.demo.provider.SqlFile.java
com.novoda.imageloader.demo.util.BugSenseHelper.java
com.novoda.imageloader.demo.util.BugsenseApiKeyFailedException.java