Android Open Source - RabbitEars File Helper






From Project

Back to project page RabbitEars.

License

The source code is released under:

MIT License

If you think the Android project RabbitEars 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

package com.fbs.rabbitears.helpers;
// w  ww .ja  va  2 s  .  c  o  m
import android.content.Context;
import android.content.ContextWrapper;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import com.fbs.rabbitears.RabbitEars;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

/**
 * File IO Helper Methods
 */
public class FileHelper
{
    private static final Context CONTEXT;
    private static final ContextWrapper WRAPPER;

    /**
     * Static Initializer
     */
    static
    {
        CONTEXT = RabbitEars.getContext();
        WRAPPER = new ContextWrapper(CONTEXT);
    }

    /**
     * Get cache image directory
     * @return File cached image directory
     */
    private static File getCacheDirectory()
    {
        return WRAPPER.getDir("ImageCache", Context.MODE_PRIVATE);
    }

    /**
     * Saves an image to image cache as PNG
     * @param name String name of image to save
     * @param bitmap Bitmap image to save
     * @return True if save was successful, false if not
     */
    public static boolean saveImageToCache(String name, Bitmap bitmap)
    {
        boolean saved = false;

        File dir  = getCacheDirectory();
        File file = new File(dir, name);

        FileOutputStream output;

        try
        {
            output = new FileOutputStream(file);
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, output);

            output.close();

            saved = true;
        }
        catch (Exception e) { }

        return saved;
    }

    /**
     * Clear an individual image from image cache
     * @param name String name of image to remove
     * @return True if removal was successful, false if not
     */
    public static boolean clearImageFromCache(String name)
    {
        boolean deleted = false;

        File dir  = getCacheDirectory();
        File file = new File(dir, name);

        if (file.exists())
        {
            deleted = file.isFile()
                    && file.canWrite()
                    && file.delete();
        }

        return deleted;
    }

    /**
     * Clear all images from image cache
     * @return True if all images were removed, false if not
     */
    public static boolean clearImageCache()
    {
        boolean allDeleted = false;

        File dir  = getCacheDirectory();

        for (File file : dir.listFiles())
        {
            if (file.canWrite())
            {
                allDeleted = file.isFile()
                        && file.canWrite()
                        && file.delete();

                if (! allDeleted) { break; }
            }
        }

        return allDeleted;
    }

    /**
     * Load an individual image from cache
     * @param name String name of image to load
     * @return Bitmap loaded image from cache or null
     */
    public static Bitmap loadImageFromCache(String name)
    {
        Bitmap bitmap = null;

        File dir  = getCacheDirectory();
        File file = new File(dir, name);

        try
        {
            bitmap = BitmapFactory.decodeStream(new FileInputStream(file));
        }
        catch (FileNotFoundException e) { }

        return bitmap;
    }
}




Java Source Code List

com.fbs.rabbitears.ApplicationTest.java
com.fbs.rabbitears.Config.java
com.fbs.rabbitears.RabbitEars.java
com.fbs.rabbitears.activities.BaseActivity.java
com.fbs.rabbitears.activities.FeedLister.java
com.fbs.rabbitears.activities.ItemLister.java
com.fbs.rabbitears.activities.ItemStreamer.java
com.fbs.rabbitears.activities.ItemViewer.java
com.fbs.rabbitears.activities.Settings.java
com.fbs.rabbitears.adapters.FeedArrayAdapter.java
com.fbs.rabbitears.adapters.FeedItemArrayAdapter.java
com.fbs.rabbitears.contracts.feed.Author.java
com.fbs.rabbitears.contracts.feed.Content.java
com.fbs.rabbitears.contracts.feed.Entry.java
com.fbs.rabbitears.contracts.feed.Feed.java
com.fbs.rabbitears.contracts.feed.Link.java
com.fbs.rabbitears.contracts.rss.Category.java
com.fbs.rabbitears.contracts.rss.Channel.java
com.fbs.rabbitears.contracts.rss.Enclosure.java
com.fbs.rabbitears.contracts.rss.Guid.java
com.fbs.rabbitears.contracts.rss.Image.java
com.fbs.rabbitears.contracts.rss.Item.java
com.fbs.rabbitears.contracts.rss.Rss.java
com.fbs.rabbitears.events.Event.java
com.fbs.rabbitears.events.FeedDownloadEvent.java
com.fbs.rabbitears.events.ItemProcessEvent.java
com.fbs.rabbitears.fragments.AddFeedDialog.java
com.fbs.rabbitears.fragments.BaseDialogFragment.java
com.fbs.rabbitears.helpers.DeserializationHelper.java
com.fbs.rabbitears.helpers.FileHelper.java
com.fbs.rabbitears.helpers.ModelHelper.java
com.fbs.rabbitears.helpers.ViewHelper.java
com.fbs.rabbitears.models.FeedItem.java
com.fbs.rabbitears.models.Feed.java
com.fbs.rabbitears.models.ItemMedia.java
com.fbs.rabbitears.tasks.DownloadFeedImageTask.java
com.fbs.rabbitears.tasks.DownloadFeedTask.java
com.fbs.rabbitears.tasks.ProcessFeedItemsTask.java
com.fbs.rabbitears.utils.RssParser.java
com.fbs.rabbitears.utils.Size.java
com.fbs.rabbitears.views.MediaStreamer.java