Question

I am using the Android-Universal-Image-Loader from https://github.com/nostra13/Android-Universal-Image-Loader to load and cache images async. The problem I am facing is that i would like to open the images with the gallery or sent them to friends. For some reason my images are cached without a file extension.

I am using the current configuration

public static void initImageLoader(Context context) {

        File cacheDir = StorageUtils.getOwnCacheDirectory(context, CommonUtilities.APP_DIR);

        DisplayImageOptions options = new DisplayImageOptions.Builder()
        .cacheInMemory(true)
        .cacheOnDisc(true)
        .considerExifParams(true)
        .build();

        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
                .threadPriority(Thread.MAX_PRIORITY)
                .discCacheExtraOptions(480, 800, CompressFormat.PNG, 75, null)
                .denyCacheImageMultipleSizesInMemory()
                .discCache(new UnlimitedDiscCache(cacheDir))
                .defaultDisplayImageOptions(options)
                .tasksProcessingOrder(QueueProcessingType.FIFO)
                .build();

        // Initialize ImageLoader with configuration.
        ImageLoader.getInstance().init(config);
    }

Hopefully anyone can help me out!

Was it helpful?

Solution

Create own FileNameGenerator which will generate file names with extension in the end and then set it into disc cache:

    ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
             ...
            .discCache(new UnlimitedDiscCache(cacheDir, myGenerator))
            ...
            .build();

OTHER TIPS

Create custom file name generator

import com.nostra13.universalimageloader.cache.disc.naming.FileNameGenerator;
import static java.lang.String.valueOf;

public class HashCodeFileNameWithDummyExtGenerator implements FileNameGenerator {
    @Override
    public String generate(String imageUri) {
        return valueOf(imageUri.hashCode()) + ".png";
    }
}

Use it

ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
    ...
    .diskCache(new UnlimitedDiskCache(cacheDir, null, new HashCodeFileNameWithDummyExtGenerator()))
    ...
    .build()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top