Android Open Source - Viz Image Utilities






From Project

Back to project page Viz.

License

The source code is released under:

GNU General Public License

If you think the Android project Viz 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 (C) 2008 Romain Guy//from w  ww .j  a  v a  2s  .  com
 *
 * 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.
 *
 * Addendum:
 *  Modified code from Romain's project Shelves @ code.google.com/p/shelves
 *  Removed everything but his cache.
 *  Added image sampling, i.e., calculateInSampleSize
 */
package com.first3.viz.utils;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.lang.ref.SoftReference;
import java.util.HashMap;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;

import com.first3.viz.ui.FastBitmapDrawable;
import com.first3.viz.Constants;

public class ImageUtilities {
    // Use a concurrent HashMap to support multiple threads?
    private static final HashMap<Uri, SoftReference<FastBitmapDrawable>> sArtCache = Maps.newHashMap();

    private ImageUtilities() {
    }

    /**
     * Deletes the specified drawable from the cache.
     *
     * @param id The uri of the drawable to delete from the cache
     */
    public static void deleteCachedCover(Uri uri) {
        sArtCache.remove(uri);
    }

    /**
     * Retrieves a drawable from the image cache, identified by the uri.
     * If the drawable does not exist in the cache, it is loaded and added to the cache.
     * If the drawable cannot be added to the cache, a null drawable is
     * returned.
     *
     * @param uri The id of the drawable to retrieve
     *
     * @return The drawable identified by the uri or a null drawable
     */
    public static FastBitmapDrawable getCachedBitmap(Context context, Uri uri,
            int width, int height) {
        FastBitmapDrawable drawable = null;

        SoftReference<FastBitmapDrawable> reference = sArtCache.get(uri);
        if (reference != null) {
            drawable = reference.get();
        }

        if (drawable == null) {
            final Bitmap bitmap = loadBitmap(context, uri, width, height);
            if (bitmap != null) {
                drawable = new FastBitmapDrawable(bitmap);
                sArtCache.put(uri, new SoftReference<FastBitmapDrawable>(drawable));
            }
        }

        return drawable;
    }

    /**
     * Removes all the callbacks from the drawables stored in the memory cache. This
     * method must be called from the onDestroy() method of any activity using the
     * cached drawables. Failure to do so will result in the entire activity being
     * leaked.
     */
    public static void cleanupCache() {
        for (SoftReference<FastBitmapDrawable> reference : sArtCache.values()) {
            final FastBitmapDrawable drawable = reference.get();
            if (drawable != null) {
                drawable.setCallback(null);
            }
        }
    }
    public static int calculateInSampleSize(BitmapFactory.Options options,
            int reqWidth, int reqHeight) {
        // Raw height and width of image
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;

        //Log.d("thumbnail width: " + width + " height: " + height);

        if (height > reqHeight || width > reqWidth) {
            if (width > height) {
                //Log.v("Calculating insample size: width greater than
                //height");
                inSampleSize = Math.round((float)height / (float)reqHeight);
            } else {
                //Log.v("Calculating insample size: width less than height");
                inSampleSize = Math.round((float)width / (float)reqWidth);
            }
        }
        return inSampleSize;
    }

    private static Uri getDefaultThumbnailUri() {
        File defaultThumbnail = new File(Constants.DEFAULT_THUMBNAIL_FILENAME);
        return Uri.fromFile(defaultThumbnail);
    }

    private static InputStream getInputStream(Context context, Uri uri) {
        InputStream in;

        if (getDefaultThumbnailUri().equals(uri)) {
            //Log.i("Found default thumbnail");
            try {
                in = context.getAssets().open("thumbnail_default.jpg");
            } catch (IOException e) {
                Log.w("Could not open default thumbnail");
                return null;
            }
        } else {
            try {
                in = context.getContentResolver().openInputStream(uri);
            } catch (FileNotFoundException e) {
                Log.w("Could not find thumbnail for " + uri);
                return null;
            };
        }

        return in;
    }

    private static Bitmap loadBitmap(Context context, Uri uri, int width, int height) {
        InputStream in = getInputStream(context, uri);
        if (in == null) {
            Log.w("getInputStream failed to open: " + uri);
            return null;
        }

        // First decode with inJustDecodeBounds=true to check dimensions
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(in, null, options);

        options.inSampleSize = calculateInSampleSize(options, width, height);
        // need to close and reopen input stream as we're now at the end of it
        IOUtilities.closeStream(in);

        //Log.v("Dimensions: height: " +  height + ", width: " + width);
        //Log.v("Calculated inSampleSize of " + options.inSampleSize);

        // repeated on purpose
        in = getInputStream(context, uri);
        if (in == null) {
            Log.w("Could not find thumbnail for " + uri);
            return null;
        }

        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;
        Bitmap bitmap = BitmapFactory.decodeStream(in, null, options);
        IOUtilities.closeStream(in);
        return bitmap;
    }
}




Java Source Code List

com.actionbarsherlock.BuildConfig.java
com.first3.viz.Config.java
com.first3.viz.Config.java
com.first3.viz.Constants.java
com.first3.viz.Preferences.java
com.first3.viz.VersionChangeNotifier.java
com.first3.viz.VizApp.java
com.first3.viz.browser.Browser.java
com.first3.viz.browser.VizWebChromeClient.java
com.first3.viz.browser.VizWebViewClient.java
com.first3.viz.builders.BlinkxResourceBuilder.java
com.first3.viz.builders.CombinedResourceBuilder.java
com.first3.viz.builders.ContainerResourceBuilder.java
com.first3.viz.builders.DailyMotionResourceBuilder.java
com.first3.viz.builders.FlashPlayerResourceBuilder.java
com.first3.viz.builders.FunnyOrDieResourceBuilder.java
com.first3.viz.builders.GenericResourceBuilder.java
com.first3.viz.builders.GoGoAnimeResourceBuilder.java
com.first3.viz.builders.JSResourceBuilder.java
com.first3.viz.builders.LiveleakResourceBuilder.java
com.first3.viz.builders.MetacafeResourceBuilder.java
com.first3.viz.builders.NovamovResourceBuilder.java
com.first3.viz.builders.Play44ResourceBuilder.java
com.first3.viz.builders.PornHubBuilder.java
com.first3.viz.builders.RedtubeBuilder.java
com.first3.viz.builders.ResourceBuilder.java
com.first3.viz.builders.VevoResourceBuilder.java
com.first3.viz.builders.Video44ResourceBuilder.java
com.first3.viz.builders.VideoFunResourceBuilder.java
com.first3.viz.builders.VidzurResourceBuilder.java
com.first3.viz.builders.VimeoResourceBuilder.java
com.first3.viz.builders.YouruploadResourceBuilder.java
com.first3.viz.content.ContentSource.java
com.first3.viz.content.ContentSources.java
com.first3.viz.content.ContentType.java
com.first3.viz.content.ContentTypes.java
com.first3.viz.download.Container.java
com.first3.viz.download.DownloadManager.java
com.first3.viz.download.StringContainer.java
com.first3.viz.models.Favorite.java
com.first3.viz.models.Resource.java
com.first3.viz.players.VideoPlayer.java
com.first3.viz.provider.VizContract.java
com.first3.viz.provider.VizDatabase.java
com.first3.viz.provider.VizProvider.java
com.first3.viz.ui.ActivityDelegate.java
com.first3.viz.ui.DirectoryListAdapter.java
com.first3.viz.ui.DownloadDirectoryDialogPreference.java
com.first3.viz.ui.Downloads.java
com.first3.viz.ui.FastBitmapDrawable.java
com.first3.viz.ui.Favorites.java
com.first3.viz.ui.FileManager.java
com.first3.viz.ui.PinSelectorDialogFragment.java
com.first3.viz.ui.ProgressDialogFragment.java
com.first3.viz.ui.Settings.java
com.first3.viz.ui.VizMediaPlayer.java
com.first3.viz.utils.AbstractPauseHandler.java
com.first3.viz.utils.ActivityParent.java
com.first3.viz.utils.DownloadTask.java
com.first3.viz.utils.FetchContainerTask.java
com.first3.viz.utils.FragmentParent.java
com.first3.viz.utils.IOUtilities.java
com.first3.viz.utils.ImageUtilities.java
com.first3.viz.utils.Lists.java
com.first3.viz.utils.Log.java
com.first3.viz.utils.Maps.java
com.first3.viz.utils.SelectionBuilder.java
com.first3.viz.utils.StringBuffer.java
com.first3.viz.utils.TabsAdapter.java
com.first3.viz.utils.Utils.java
com.first3.viz.utils.VizUtils.java