├── .google └── packaging.yaml ├── ABOUT.txt ├── Application ├── build.gradle ├── src │ └── main │ │ ├── AndroidManifest.xml │ │ ├── java │ │ └── com │ │ │ └── example │ │ │ └── android │ │ │ ├── common │ │ │ └── logger │ │ │ │ ├── Log.java │ │ │ │ ├── LogFragment.java │ │ │ │ ├── LogNode.java │ │ │ │ ├── LogView.java │ │ │ │ ├── LogWrapper.java │ │ │ │ └── MessageOnlyLogFilter.java │ │ │ └── displayingbitmaps │ │ │ ├── provider │ │ │ └── Images.java │ │ │ ├── ui │ │ │ ├── ImageDetailActivity.java │ │ │ ├── ImageDetailFragment.java │ │ │ ├── ImageGridActivity.java │ │ │ ├── ImageGridFragment.java │ │ │ └── RecyclingImageView.java │ │ │ └── util │ │ │ ├── AsyncTask.java │ │ │ ├── DiskLruCache.java │ │ │ ├── ImageCache.java │ │ │ ├── ImageFetcher.java │ │ │ ├── ImageResizer.java │ │ │ ├── ImageWorker.java │ │ │ ├── RecyclingBitmapDrawable.java │ │ │ └── Utils.java │ │ └── res │ │ ├── drawable-hdpi │ │ ├── ic_launcher.png │ │ └── tile.9.png │ │ ├── drawable-mdpi │ │ └── ic_launcher.png │ │ ├── drawable-nodpi │ │ └── empty_photo.png │ │ ├── drawable-xhdpi │ │ └── ic_launcher.png │ │ ├── drawable-xxhdpi │ │ └── ic_launcher.png │ │ ├── drawable │ │ └── photogrid_list_selector.xml │ │ ├── layout │ │ ├── image_detail_fragment.xml │ │ ├── image_detail_pager.xml │ │ └── image_grid_fragment.xml │ │ ├── menu │ │ └── main_menu.xml │ │ ├── values-large │ │ └── dimens.xml │ │ ├── values-sw600dp │ │ ├── template-dimens.xml │ │ └── template-styles.xml │ │ ├── values-v11 │ │ ├── styles.xml │ │ └── template-styles.xml │ │ ├── values-v21 │ │ ├── base-colors.xml │ │ └── base-template-styles.xml │ │ ├── values-xlarge │ │ └── dimens.xml │ │ └── values │ │ ├── base-strings.xml │ │ ├── colors.xml │ │ ├── dimens.xml │ │ ├── strings.xml │ │ ├── styles.xml │ │ ├── template-dimens.xml │ │ └── template-styles.xml └── tests │ ├── AndroidManifest.xml │ └── src │ └── com │ └── example │ └── android │ └── displayingbitmaps │ └── tests │ └── SampleTests.java ├── CONTRIB.md ├── CONTRIBUTING.md ├── LICENSE ├── README.md ├── build.gradle ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── packaging.yaml ├── screenshots ├── 1-gridview.png ├── 2-detail.png └── icon-web.png └── settings.gradle /.google/packaging.yaml: -------------------------------------------------------------------------------- 1 | 2 | # GOOGLE SAMPLE PACKAGING DATA 3 | # 4 | # This file is used by Google as part of our samples packaging process. 5 | # End users may safely ignore this file. It has no relevance to other systems. 6 | --- 7 | status: PUBLISHED 8 | technologies: [Android] 9 | categories: [UI, Background, Views] 10 | languages: [Java] 11 | solutions: [Mobile] 12 | github: android-DisplayingBitmaps 13 | level: ADVANCED 14 | icon: screenshots/icon-web.png 15 | apiRefs: 16 | - android:android.widget.ImageView 17 | - android:android.widget.GridView 18 | - android:android.graphics.BitmapFactory 19 | - android:android.os.AsyncTask 20 | - android:android.util.LruCache 21 | - android:android.support.v4.view.ViewPager 22 | license: apache2 23 | -------------------------------------------------------------------------------- /ABOUT.txt: -------------------------------------------------------------------------------- 1 | Copyright (C) 2014 The Android Open Source Project 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | 15 | --------------------------------------- 16 | 17 | This is a sample application for the Android Training class "Displaying Bitmaps 18 | Efficiently" (http://developer.android.com/training/displaying-bitmaps/). 19 | 20 | It demonstrates how to load large bitmaps efficiently off the main UI thread, 21 | caching bitmaps (both in memory and on disk), managing bitmap memory and 22 | displaying bitmaps in UI elements such as ViewPager and ListView/GridView. -------------------------------------------------------------------------------- /Application/build.gradle: -------------------------------------------------------------------------------- 1 | 2 | buildscript { 3 | repositories { 4 | jcenter() 5 | google() 6 | } 7 | 8 | dependencies { 9 | classpath 'com.android.tools.build:gradle:3.0.1' 10 | } 11 | } 12 | 13 | apply plugin: 'com.android.application' 14 | 15 | repositories { 16 | jcenter() 17 | google() 18 | } 19 | 20 | dependencies { 21 | compile "com.android.support:support-v4:27.0.2" 22 | compile "com.android.support:gridlayout-v7:27.0.2" 23 | compile "com.android.support:cardview-v7:27.0.2" 24 | compile "com.android.support:appcompat-v7:27.0.2" 25 | } 26 | 27 | // The sample build uses multiple directories to 28 | // keep boilerplate and common code separate from 29 | // the main sample code. 30 | List dirs = [ 31 | 'main', // main sample code; look here for the interesting stuff. 32 | 'common', // components that are reused by multiple samples 33 | 'template'] // boilerplate code that is generated by the sample template process 34 | 35 | android { 36 | compileSdkVersion 27 37 | 38 | buildToolsVersion "27.0.2" 39 | 40 | defaultConfig { 41 | minSdkVersion 9 42 | targetSdkVersion 22 43 | } 44 | 45 | compileOptions { 46 | sourceCompatibility JavaVersion.VERSION_1_7 47 | targetCompatibility JavaVersion.VERSION_1_7 48 | } 49 | 50 | sourceSets { 51 | main { 52 | dirs.each { dir -> 53 | java.srcDirs "src/${dir}/java" 54 | res.srcDirs "src/${dir}/res" 55 | } 56 | } 57 | androidTest.setRoot('tests') 58 | androidTest.java.srcDirs = ['tests/src'] 59 | 60 | } 61 | 62 | } 63 | -------------------------------------------------------------------------------- /Application/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 17 | 18 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 34 | 35 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 48 | 50 | 51 | 52 | 53 | 54 | 55 | -------------------------------------------------------------------------------- /Application/src/main/java/com/example/android/common/logger/Log.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2013 The Android Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.example.android.common.logger; 17 | 18 | /** 19 | * Helper class for a list (or tree) of LoggerNodes. 20 | * 21 | *

When this is set as the head of the list, 22 | * an instance of it can function as a drop-in replacement for {@link android.util.Log}. 23 | * Most of the methods in this class server only to map a method call in Log to its equivalent 24 | * in LogNode.

25 | */ 26 | public class Log { 27 | // Grabbing the native values from Android's native logging facilities, 28 | // to make for easy migration and interop. 29 | public static final int NONE = -1; 30 | public static final int VERBOSE = android.util.Log.VERBOSE; 31 | public static final int DEBUG = android.util.Log.DEBUG; 32 | public static final int INFO = android.util.Log.INFO; 33 | public static final int WARN = android.util.Log.WARN; 34 | public static final int ERROR = android.util.Log.ERROR; 35 | public static final int ASSERT = android.util.Log.ASSERT; 36 | 37 | // Stores the beginning of the LogNode topology. 38 | private static LogNode mLogNode; 39 | 40 | /** 41 | * Returns the next LogNode in the linked list. 42 | */ 43 | public static LogNode getLogNode() { 44 | return mLogNode; 45 | } 46 | 47 | /** 48 | * Sets the LogNode data will be sent to. 49 | */ 50 | public static void setLogNode(LogNode node) { 51 | mLogNode = node; 52 | } 53 | 54 | /** 55 | * Instructs the LogNode to print the log data provided. Other LogNodes can 56 | * be chained to the end of the LogNode as desired. 57 | * 58 | * @param priority Log level of the data being logged. Verbose, Error, etc. 59 | * @param tag Tag for for the log data. Can be used to organize log statements. 60 | * @param msg The actual message to be logged. 61 | * @param tr If an exception was thrown, this can be sent along for the logging facilities 62 | * to extract and print useful information. 63 | */ 64 | public static void println(int priority, String tag, String msg, Throwable tr) { 65 | if (mLogNode != null) { 66 | mLogNode.println(priority, tag, msg, tr); 67 | } 68 | } 69 | 70 | /** 71 | * Instructs the LogNode to print the log data provided. Other LogNodes can 72 | * be chained to the end of the LogNode as desired. 73 | * 74 | * @param priority Log level of the data being logged. Verbose, Error, etc. 75 | * @param tag Tag for for the log data. Can be used to organize log statements. 76 | * @param msg The actual message to be logged. The actual message to be logged. 77 | */ 78 | public static void println(int priority, String tag, String msg) { 79 | println(priority, tag, msg, null); 80 | } 81 | 82 | /** 83 | * Prints a message at VERBOSE priority. 84 | * 85 | * @param tag Tag for for the log data. Can be used to organize log statements. 86 | * @param msg The actual message to be logged. 87 | * @param tr If an exception was thrown, this can be sent along for the logging facilities 88 | * to extract and print useful information. 89 | */ 90 | public static void v(String tag, String msg, Throwable tr) { 91 | println(VERBOSE, tag, msg, tr); 92 | } 93 | 94 | /** 95 | * Prints a message at VERBOSE priority. 96 | * 97 | * @param tag Tag for for the log data. Can be used to organize log statements. 98 | * @param msg The actual message to be logged. 99 | */ 100 | public static void v(String tag, String msg) { 101 | v(tag, msg, null); 102 | } 103 | 104 | 105 | /** 106 | * Prints a message at DEBUG priority. 107 | * 108 | * @param tag Tag for for the log data. Can be used to organize log statements. 109 | * @param msg The actual message to be logged. 110 | * @param tr If an exception was thrown, this can be sent along for the logging facilities 111 | * to extract and print useful information. 112 | */ 113 | public static void d(String tag, String msg, Throwable tr) { 114 | println(DEBUG, tag, msg, tr); 115 | } 116 | 117 | /** 118 | * Prints a message at DEBUG priority. 119 | * 120 | * @param tag Tag for for the log data. Can be used to organize log statements. 121 | * @param msg The actual message to be logged. 122 | */ 123 | public static void d(String tag, String msg) { 124 | d(tag, msg, null); 125 | } 126 | 127 | /** 128 | * Prints a message at INFO priority. 129 | * 130 | * @param tag Tag for for the log data. Can be used to organize log statements. 131 | * @param msg The actual message to be logged. 132 | * @param tr If an exception was thrown, this can be sent along for the logging facilities 133 | * to extract and print useful information. 134 | */ 135 | public static void i(String tag, String msg, Throwable tr) { 136 | println(INFO, tag, msg, tr); 137 | } 138 | 139 | /** 140 | * Prints a message at INFO priority. 141 | * 142 | * @param tag Tag for for the log data. Can be used to organize log statements. 143 | * @param msg The actual message to be logged. 144 | */ 145 | public static void i(String tag, String msg) { 146 | i(tag, msg, null); 147 | } 148 | 149 | /** 150 | * Prints a message at WARN priority. 151 | * 152 | * @param tag Tag for for the log data. Can be used to organize log statements. 153 | * @param msg The actual message to be logged. 154 | * @param tr If an exception was thrown, this can be sent along for the logging facilities 155 | * to extract and print useful information. 156 | */ 157 | public static void w(String tag, String msg, Throwable tr) { 158 | println(WARN, tag, msg, tr); 159 | } 160 | 161 | /** 162 | * Prints a message at WARN priority. 163 | * 164 | * @param tag Tag for for the log data. Can be used to organize log statements. 165 | * @param msg The actual message to be logged. 166 | */ 167 | public static void w(String tag, String msg) { 168 | w(tag, msg, null); 169 | } 170 | 171 | /** 172 | * Prints a message at WARN priority. 173 | * 174 | * @param tag Tag for for the log data. Can be used to organize log statements. 175 | * @param tr If an exception was thrown, this can be sent along for the logging facilities 176 | * to extract and print useful information. 177 | */ 178 | public static void w(String tag, Throwable tr) { 179 | w(tag, null, tr); 180 | } 181 | 182 | /** 183 | * Prints a message at ERROR priority. 184 | * 185 | * @param tag Tag for for the log data. Can be used to organize log statements. 186 | * @param msg The actual message to be logged. 187 | * @param tr If an exception was thrown, this can be sent along for the logging facilities 188 | * to extract and print useful information. 189 | */ 190 | public static void e(String tag, String msg, Throwable tr) { 191 | println(ERROR, tag, msg, tr); 192 | } 193 | 194 | /** 195 | * Prints a message at ERROR priority. 196 | * 197 | * @param tag Tag for for the log data. Can be used to organize log statements. 198 | * @param msg The actual message to be logged. 199 | */ 200 | public static void e(String tag, String msg) { 201 | e(tag, msg, null); 202 | } 203 | 204 | /** 205 | * Prints a message at ASSERT priority. 206 | * 207 | * @param tag Tag for for the log data. Can be used to organize log statements. 208 | * @param msg The actual message to be logged. 209 | * @param tr If an exception was thrown, this can be sent along for the logging facilities 210 | * to extract and print useful information. 211 | */ 212 | public static void wtf(String tag, String msg, Throwable tr) { 213 | println(ASSERT, tag, msg, tr); 214 | } 215 | 216 | /** 217 | * Prints a message at ASSERT priority. 218 | * 219 | * @param tag Tag for for the log data. Can be used to organize log statements. 220 | * @param msg The actual message to be logged. 221 | */ 222 | public static void wtf(String tag, String msg) { 223 | wtf(tag, msg, null); 224 | } 225 | 226 | /** 227 | * Prints a message at ASSERT priority. 228 | * 229 | * @param tag Tag for for the log data. Can be used to organize log statements. 230 | * @param tr If an exception was thrown, this can be sent along for the logging facilities 231 | * to extract and print useful information. 232 | */ 233 | public static void wtf(String tag, Throwable tr) { 234 | wtf(tag, null, tr); 235 | } 236 | } 237 | -------------------------------------------------------------------------------- /Application/src/main/java/com/example/android/common/logger/LogFragment.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013 The Android Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | /* 17 | * Copyright 2013 The Android Open Source Project 18 | * 19 | * Licensed under the Apache License, Version 2.0 (the "License"); 20 | * you may not use this file except in compliance with the License. 21 | * You may obtain a copy of the License at 22 | * 23 | * http://www.apache.org/licenses/LICENSE-2.0 24 | * 25 | * Unless required by applicable law or agreed to in writing, software 26 | * distributed under the License is distributed on an "AS IS" BASIS, 27 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 28 | * See the License for the specific language governing permissions and 29 | * limitations under the License. 30 | */ 31 | 32 | package com.example.android.common.logger; 33 | 34 | import android.graphics.Typeface; 35 | import android.os.Bundle; 36 | import android.support.v4.app.Fragment; 37 | import android.text.Editable; 38 | import android.text.TextWatcher; 39 | import android.view.Gravity; 40 | import android.view.LayoutInflater; 41 | import android.view.View; 42 | import android.view.ViewGroup; 43 | import android.widget.ScrollView; 44 | 45 | /** 46 | * Simple fraggment which contains a LogView and uses is to output log data it receives 47 | * through the LogNode interface. 48 | */ 49 | public class LogFragment extends Fragment { 50 | 51 | private LogView mLogView; 52 | private ScrollView mScrollView; 53 | 54 | public LogFragment() {} 55 | 56 | public View inflateViews() { 57 | mScrollView = new ScrollView(getActivity()); 58 | ViewGroup.LayoutParams scrollParams = new ViewGroup.LayoutParams( 59 | ViewGroup.LayoutParams.MATCH_PARENT, 60 | ViewGroup.LayoutParams.MATCH_PARENT); 61 | mScrollView.setLayoutParams(scrollParams); 62 | 63 | mLogView = new LogView(getActivity()); 64 | ViewGroup.LayoutParams logParams = new ViewGroup.LayoutParams(scrollParams); 65 | logParams.height = ViewGroup.LayoutParams.WRAP_CONTENT; 66 | mLogView.setLayoutParams(logParams); 67 | mLogView.setClickable(true); 68 | mLogView.setFocusable(true); 69 | mLogView.setTypeface(Typeface.MONOSPACE); 70 | 71 | // Want to set padding as 16 dips, setPadding takes pixels. Hooray math! 72 | int paddingDips = 16; 73 | double scale = getResources().getDisplayMetrics().density; 74 | int paddingPixels = (int) ((paddingDips * (scale)) + .5); 75 | mLogView.setPadding(paddingPixels, paddingPixels, paddingPixels, paddingPixels); 76 | mLogView.setCompoundDrawablePadding(paddingPixels); 77 | 78 | mLogView.setGravity(Gravity.BOTTOM); 79 | mLogView.setTextAppearance(getActivity(), android.R.style.TextAppearance_Holo_Medium); 80 | 81 | mScrollView.addView(mLogView); 82 | return mScrollView; 83 | } 84 | 85 | @Override 86 | public View onCreateView(LayoutInflater inflater, ViewGroup container, 87 | Bundle savedInstanceState) { 88 | 89 | View result = inflateViews(); 90 | 91 | mLogView.addTextChangedListener(new TextWatcher() { 92 | @Override 93 | public void beforeTextChanged(CharSequence s, int start, int count, int after) {} 94 | 95 | @Override 96 | public void onTextChanged(CharSequence s, int start, int before, int count) {} 97 | 98 | @Override 99 | public void afterTextChanged(Editable s) { 100 | mScrollView.fullScroll(ScrollView.FOCUS_DOWN); 101 | } 102 | }); 103 | return result; 104 | } 105 | 106 | public LogView getLogView() { 107 | return mLogView; 108 | } 109 | } -------------------------------------------------------------------------------- /Application/src/main/java/com/example/android/common/logger/LogNode.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2012 The Android Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.example.android.common.logger; 17 | 18 | /** 19 | * Basic interface for a logging system that can output to one or more targets. 20 | * Note that in addition to classes that will output these logs in some format, 21 | * one can also implement this interface over a filter and insert that in the chain, 22 | * such that no targets further down see certain data, or see manipulated forms of the data. 23 | * You could, for instance, write a "ToHtmlLoggerNode" that just converted all the log data 24 | * it received to HTML and sent it along to the next node in the chain, without printing it 25 | * anywhere. 26 | */ 27 | public interface LogNode { 28 | 29 | /** 30 | * Instructs first LogNode in the list to print the log data provided. 31 | * @param priority Log level of the data being logged. Verbose, Error, etc. 32 | * @param tag Tag for for the log data. Can be used to organize log statements. 33 | * @param msg The actual message to be logged. The actual message to be logged. 34 | * @param tr If an exception was thrown, this can be sent along for the logging facilities 35 | * to extract and print useful information. 36 | */ 37 | public void println(int priority, String tag, String msg, Throwable tr); 38 | 39 | } 40 | -------------------------------------------------------------------------------- /Application/src/main/java/com/example/android/common/logger/LogView.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2013 The Android Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.example.android.common.logger; 17 | 18 | import android.app.Activity; 19 | import android.content.Context; 20 | import android.util.*; 21 | import android.widget.TextView; 22 | 23 | /** Simple TextView which is used to output log data received through the LogNode interface. 24 | */ 25 | public class LogView extends TextView implements LogNode { 26 | 27 | public LogView(Context context) { 28 | super(context); 29 | } 30 | 31 | public LogView(Context context, AttributeSet attrs) { 32 | super(context, attrs); 33 | } 34 | 35 | public LogView(Context context, AttributeSet attrs, int defStyle) { 36 | super(context, attrs, defStyle); 37 | } 38 | 39 | /** 40 | * Formats the log data and prints it out to the LogView. 41 | * @param priority Log level of the data being logged. Verbose, Error, etc. 42 | * @param tag Tag for for the log data. Can be used to organize log statements. 43 | * @param msg The actual message to be logged. The actual message to be logged. 44 | * @param tr If an exception was thrown, this can be sent along for the logging facilities 45 | * to extract and print useful information. 46 | */ 47 | @Override 48 | public void println(int priority, String tag, String msg, Throwable tr) { 49 | 50 | 51 | String priorityStr = null; 52 | 53 | // For the purposes of this View, we want to print the priority as readable text. 54 | switch(priority) { 55 | case android.util.Log.VERBOSE: 56 | priorityStr = "VERBOSE"; 57 | break; 58 | case android.util.Log.DEBUG: 59 | priorityStr = "DEBUG"; 60 | break; 61 | case android.util.Log.INFO: 62 | priorityStr = "INFO"; 63 | break; 64 | case android.util.Log.WARN: 65 | priorityStr = "WARN"; 66 | break; 67 | case android.util.Log.ERROR: 68 | priorityStr = "ERROR"; 69 | break; 70 | case android.util.Log.ASSERT: 71 | priorityStr = "ASSERT"; 72 | break; 73 | default: 74 | break; 75 | } 76 | 77 | // Handily, the Log class has a facility for converting a stack trace into a usable string. 78 | String exceptionStr = null; 79 | if (tr != null) { 80 | exceptionStr = android.util.Log.getStackTraceString(tr); 81 | } 82 | 83 | // Take the priority, tag, message, and exception, and concatenate as necessary 84 | // into one usable line of text. 85 | final StringBuilder outputBuilder = new StringBuilder(); 86 | 87 | String delimiter = "\t"; 88 | appendIfNotNull(outputBuilder, priorityStr, delimiter); 89 | appendIfNotNull(outputBuilder, tag, delimiter); 90 | appendIfNotNull(outputBuilder, msg, delimiter); 91 | appendIfNotNull(outputBuilder, exceptionStr, delimiter); 92 | 93 | // In case this was originally called from an AsyncTask or some other off-UI thread, 94 | // make sure the update occurs within the UI thread. 95 | ((Activity) getContext()).runOnUiThread( (new Thread(new Runnable() { 96 | @Override 97 | public void run() { 98 | // Display the text we just generated within the LogView. 99 | appendToLog(outputBuilder.toString()); 100 | } 101 | }))); 102 | 103 | if (mNext != null) { 104 | mNext.println(priority, tag, msg, tr); 105 | } 106 | } 107 | 108 | public LogNode getNext() { 109 | return mNext; 110 | } 111 | 112 | public void setNext(LogNode node) { 113 | mNext = node; 114 | } 115 | 116 | /** Takes a string and adds to it, with a separator, if the bit to be added isn't null. Since 117 | * the logger takes so many arguments that might be null, this method helps cut out some of the 118 | * agonizing tedium of writing the same 3 lines over and over. 119 | * @param source StringBuilder containing the text to append to. 120 | * @param addStr The String to append 121 | * @param delimiter The String to separate the source and appended strings. A tab or comma, 122 | * for instance. 123 | * @return The fully concatenated String as a StringBuilder 124 | */ 125 | private StringBuilder appendIfNotNull(StringBuilder source, String addStr, String delimiter) { 126 | if (addStr != null) { 127 | if (addStr.length() == 0) { 128 | delimiter = ""; 129 | } 130 | 131 | return source.append(addStr).append(delimiter); 132 | } 133 | return source; 134 | } 135 | 136 | // The next LogNode in the chain. 137 | LogNode mNext; 138 | 139 | /** Outputs the string as a new line of log data in the LogView. */ 140 | public void appendToLog(String s) { 141 | append("\n" + s); 142 | } 143 | 144 | 145 | } 146 | -------------------------------------------------------------------------------- /Application/src/main/java/com/example/android/common/logger/LogWrapper.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2012 The Android Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.example.android.common.logger; 17 | 18 | import android.util.Log; 19 | 20 | /** 21 | * Helper class which wraps Android's native Log utility in the Logger interface. This way 22 | * normal DDMS output can be one of the many targets receiving and outputting logs simultaneously. 23 | */ 24 | public class LogWrapper implements LogNode { 25 | 26 | // For piping: The next node to receive Log data after this one has done its work. 27 | private LogNode mNext; 28 | 29 | /** 30 | * Returns the next LogNode in the linked list. 31 | */ 32 | public LogNode getNext() { 33 | return mNext; 34 | } 35 | 36 | /** 37 | * Sets the LogNode data will be sent to.. 38 | */ 39 | public void setNext(LogNode node) { 40 | mNext = node; 41 | } 42 | 43 | /** 44 | * Prints data out to the console using Android's native log mechanism. 45 | * @param priority Log level of the data being logged. Verbose, Error, etc. 46 | * @param tag Tag for for the log data. Can be used to organize log statements. 47 | * @param msg The actual message to be logged. The actual message to be logged. 48 | * @param tr If an exception was thrown, this can be sent along for the logging facilities 49 | * to extract and print useful information. 50 | */ 51 | @Override 52 | public void println(int priority, String tag, String msg, Throwable tr) { 53 | // There actually are log methods that don't take a msg parameter. For now, 54 | // if that's the case, just convert null to the empty string and move on. 55 | String useMsg = msg; 56 | if (useMsg == null) { 57 | useMsg = ""; 58 | } 59 | 60 | // If an exeption was provided, convert that exception to a usable string and attach 61 | // it to the end of the msg method. 62 | if (tr != null) { 63 | msg += "\n" + Log.getStackTraceString(tr); 64 | } 65 | 66 | // This is functionally identical to Log.x(tag, useMsg); 67 | // For instance, if priority were Log.VERBOSE, this would be the same as Log.v(tag, useMsg) 68 | Log.println(priority, tag, useMsg); 69 | 70 | // If this isn't the last node in the chain, move things along. 71 | if (mNext != null) { 72 | mNext.println(priority, tag, msg, tr); 73 | } 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /Application/src/main/java/com/example/android/common/logger/MessageOnlyLogFilter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2013 The Android Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.example.android.common.logger; 17 | 18 | /** 19 | * Simple {@link LogNode} filter, removes everything except the message. 20 | * Useful for situations like on-screen log output where you don't want a lot of metadata displayed, 21 | * just easy-to-read message updates as they're happening. 22 | */ 23 | public class MessageOnlyLogFilter implements LogNode { 24 | 25 | LogNode mNext; 26 | 27 | /** 28 | * Takes the "next" LogNode as a parameter, to simplify chaining. 29 | * 30 | * @param next The next LogNode in the pipeline. 31 | */ 32 | public MessageOnlyLogFilter(LogNode next) { 33 | mNext = next; 34 | } 35 | 36 | public MessageOnlyLogFilter() { 37 | } 38 | 39 | @Override 40 | public void println(int priority, String tag, String msg, Throwable tr) { 41 | if (mNext != null) { 42 | getNext().println(Log.NONE, null, msg, null); 43 | } 44 | } 45 | 46 | /** 47 | * Returns the next LogNode in the chain. 48 | */ 49 | public LogNode getNext() { 50 | return mNext; 51 | } 52 | 53 | /** 54 | * Sets the LogNode data will be sent to.. 55 | */ 56 | public void setNext(LogNode node) { 57 | mNext = node; 58 | } 59 | 60 | } 61 | -------------------------------------------------------------------------------- /Application/src/main/java/com/example/android/displayingbitmaps/provider/Images.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2012 The Android Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.example.android.displayingbitmaps.provider; 18 | 19 | /** 20 | * Some simple test data to use for this sample app. 21 | */ 22 | public class Images { 23 | 24 | /** 25 | * This are PicasaWeb URLs and could potentially change. Ideally the PicasaWeb API should be 26 | * used to fetch the URLs. 27 | * 28 | * Credit to Romain Guy for the photos: 29 | * http://www.curious-creature.org/ 30 | * https://plus.google.com/109538161516040592207/about 31 | * http://www.flickr.com/photos/romainguy 32 | */ 33 | public final static String[] imageUrls = new String[] { 34 | "https://lh6.googleusercontent.com/-55osAWw3x0Q/URquUtcFr5I/AAAAAAAAAbs/rWlj1RUKrYI/s1024/A%252520Photographer.jpg", 35 | "https://lh4.googleusercontent.com/--dq8niRp7W4/URquVgmXvgI/AAAAAAAAAbs/-gnuLQfNnBA/s1024/A%252520Song%252520of%252520Ice%252520and%252520Fire.jpg", 36 | "https://lh5.googleusercontent.com/-7qZeDtRKFKc/URquWZT1gOI/AAAAAAAAAbs/hqWgteyNXsg/s1024/Another%252520Rockaway%252520Sunset.jpg", 37 | "https://lh3.googleusercontent.com/--L0Km39l5J8/URquXHGcdNI/AAAAAAAAAbs/3ZrSJNrSomQ/s1024/Antelope%252520Butte.jpg", 38 | "https://lh6.googleusercontent.com/-8HO-4vIFnlw/URquZnsFgtI/AAAAAAAAAbs/WT8jViTF7vw/s1024/Antelope%252520Hallway.jpg", 39 | "https://lh4.googleusercontent.com/-WIuWgVcU3Qw/URqubRVcj4I/AAAAAAAAAbs/YvbwgGjwdIQ/s1024/Antelope%252520Walls.jpg", 40 | "https://lh6.googleusercontent.com/-UBmLbPELvoQ/URqucCdv0kI/AAAAAAAAAbs/IdNhr2VQoQs/s1024/Apre%2525CC%252580s%252520la%252520Pluie.jpg", 41 | "https://lh3.googleusercontent.com/-s-AFpvgSeew/URquc6dF-JI/AAAAAAAAAbs/Mt3xNGRUd68/s1024/Backlit%252520Cloud.jpg", 42 | "https://lh5.googleusercontent.com/-bvmif9a9YOQ/URquea3heHI/AAAAAAAAAbs/rcr6wyeQtAo/s1024/Bee%252520and%252520Flower.jpg", 43 | "https://lh5.googleusercontent.com/-n7mdm7I7FGs/URqueT_BT-I/AAAAAAAAAbs/9MYmXlmpSAo/s1024/Bonzai%252520Rock%252520Sunset.jpg", 44 | "https://lh6.googleusercontent.com/-4CN4X4t0M1k/URqufPozWzI/AAAAAAAAAbs/8wK41lg1KPs/s1024/Caterpillar.jpg", 45 | "https://lh3.googleusercontent.com/-rrFnVC8xQEg/URqufdrLBaI/AAAAAAAAAbs/s69WYy_fl1E/s1024/Chess.jpg", 46 | "https://lh5.googleusercontent.com/-WVpRptWH8Yw/URqugh-QmDI/AAAAAAAAAbs/E-MgBgtlUWU/s1024/Chihuly.jpg", 47 | "https://lh5.googleusercontent.com/-0BDXkYmckbo/URquhKFW84I/AAAAAAAAAbs/ogQtHCTk2JQ/s1024/Closed%252520Door.jpg", 48 | "https://lh3.googleusercontent.com/-PyggXXZRykM/URquh-kVvoI/AAAAAAAAAbs/hFtDwhtrHHQ/s1024/Colorado%252520River%252520Sunset.jpg", 49 | "https://lh3.googleusercontent.com/-ZAs4dNZtALc/URquikvOCWI/AAAAAAAAAbs/DXz4h3dll1Y/s1024/Colors%252520of%252520Autumn.jpg", 50 | "https://lh4.googleusercontent.com/-GztnWEIiMz8/URqukVCU7bI/AAAAAAAAAbs/jo2Hjv6MZ6M/s1024/Countryside.jpg", 51 | "https://lh4.googleusercontent.com/-bEg9EZ9QoiM/URquklz3FGI/AAAAAAAAAbs/UUuv8Ac2BaE/s1024/Death%252520Valley%252520-%252520Dunes.jpg", 52 | "https://lh6.googleusercontent.com/-ijQJ8W68tEE/URqulGkvFEI/AAAAAAAAAbs/zPXvIwi_rFw/s1024/Delicate%252520Arch.jpg", 53 | "https://lh5.googleusercontent.com/-Oh8mMy2ieng/URqullDwehI/AAAAAAAAAbs/TbdeEfsaIZY/s1024/Despair.jpg", 54 | "https://lh5.googleusercontent.com/-gl0y4UiAOlk/URqumC_KjBI/AAAAAAAAAbs/PM1eT7dn4oo/s1024/Eagle%252520Fall%252520Sunrise.jpg", 55 | "https://lh3.googleusercontent.com/-hYYHd2_vXPQ/URqumtJa9eI/AAAAAAAAAbs/wAalXVkbSh0/s1024/Electric%252520Storm.jpg", 56 | "https://lh5.googleusercontent.com/-PyY_yiyjPTo/URqunUOhHFI/AAAAAAAAAbs/azZoULNuJXc/s1024/False%252520Kiva.jpg", 57 | "https://lh6.googleusercontent.com/-PYvLVdvXywk/URqunwd8hfI/AAAAAAAAAbs/qiMwgkFvf6I/s1024/Fitzgerald%252520Streaks.jpg", 58 | "https://lh4.googleusercontent.com/-KIR_UobIIqY/URquoCZ9SlI/AAAAAAAAAbs/Y4d4q8sXu4c/s1024/Foggy%252520Sunset.jpg", 59 | "https://lh6.googleusercontent.com/-9lzOk_OWZH0/URquoo4xYoI/AAAAAAAAAbs/AwgzHtNVCwU/s1024/Frantic.jpg", 60 | "https://lh3.googleusercontent.com/-0X3JNaKaz48/URqupH78wpI/AAAAAAAAAbs/lHXxu_zbH8s/s1024/Golden%252520Gate%252520Afternoon.jpg", 61 | "https://lh6.googleusercontent.com/-95sb5ag7ABc/URqupl95RDI/AAAAAAAAAbs/g73R20iVTRA/s1024/Golden%252520Gate%252520Fog.jpg", 62 | "https://lh3.googleusercontent.com/-JB9v6rtgHhk/URqup21F-zI/AAAAAAAAAbs/64Fb8qMZWXk/s1024/Golden%252520Grass.jpg", 63 | "https://lh4.googleusercontent.com/-EIBGfnuLtII/URquqVHwaRI/AAAAAAAAAbs/FA4McV2u8VE/s1024/Grand%252520Teton.jpg", 64 | "https://lh4.googleusercontent.com/-WoMxZvmN9nY/URquq1v2AoI/AAAAAAAAAbs/grj5uMhL6NA/s1024/Grass%252520Closeup.jpg", 65 | "https://lh3.googleusercontent.com/-6hZiEHXx64Q/URqurxvNdqI/AAAAAAAAAbs/kWMXM3o5OVI/s1024/Green%252520Grass.jpg", 66 | "https://lh5.googleusercontent.com/-6LVb9OXtQ60/URquteBFuKI/AAAAAAAAAbs/4F4kRgecwFs/s1024/Hanging%252520Leaf.jpg", 67 | "https://lh4.googleusercontent.com/-zAvf__52ONk/URqutT_IuxI/AAAAAAAAAbs/D_bcuc0thoU/s1024/Highway%2525201.jpg", 68 | "https://lh6.googleusercontent.com/-H4SrUg615rA/URquuL27fXI/AAAAAAAAAbs/4aEqJfiMsOU/s1024/Horseshoe%252520Bend%252520Sunset.jpg", 69 | "https://lh4.googleusercontent.com/-JhFi4fb_Pqw/URquuX-QXbI/AAAAAAAAAbs/IXpYUxuweYM/s1024/Horseshoe%252520Bend.jpg", 70 | "https://lh5.googleusercontent.com/-UGgssvFRJ7g/URquueyJzGI/AAAAAAAAAbs/yYIBlLT0toM/s1024/Into%252520the%252520Blue.jpg", 71 | "https://lh3.googleusercontent.com/-CH7KoupI7uI/URquu0FF__I/AAAAAAAAAbs/R7GDmI7v_G0/s1024/Jelly%252520Fish%2525202.jpg", 72 | "https://lh4.googleusercontent.com/-pwuuw6yhg8U/URquvPxR3FI/AAAAAAAAAbs/VNGk6f-tsGE/s1024/Jelly%252520Fish%2525203.jpg", 73 | "https://lh5.googleusercontent.com/-GoUQVw1fnFw/URquv6xbC0I/AAAAAAAAAbs/zEUVTQQ43Zc/s1024/Kauai.jpg", 74 | "https://lh6.googleusercontent.com/-8QdYYQEpYjw/URquwvdh88I/AAAAAAAAAbs/cktDy-ysfHo/s1024/Kyoto%252520Sunset.jpg", 75 | "https://lh4.googleusercontent.com/-vPeekyDjOE0/URquwzJ28qI/AAAAAAAAAbs/qxcyXULsZrg/s1024/Lake%252520Tahoe%252520Colors.jpg", 76 | "https://lh4.googleusercontent.com/-xBPxWpD4yxU/URquxWHk8AI/AAAAAAAAAbs/ARDPeDYPiMY/s1024/Lava%252520from%252520the%252520Sky.jpg", 77 | "https://lh3.googleusercontent.com/-897VXrJB6RE/URquxxxd-5I/AAAAAAAAAbs/j-Cz4T4YvIw/s1024/Leica%25252050mm%252520Summilux.jpg", 78 | "https://lh5.googleusercontent.com/-qSJ4D4iXzGo/URquyDWiJ1I/AAAAAAAAAbs/k2pBXeWehOA/s1024/Leica%25252050mm%252520Summilux.jpg", 79 | "https://lh6.googleusercontent.com/-dwlPg83vzLg/URquylTVuFI/AAAAAAAAAbs/G6SyQ8b4YsI/s1024/Leica%252520M8%252520%252528Front%252529.jpg", 80 | "https://lh3.googleusercontent.com/-R3_EYAyJvfk/URquzQBv8eI/AAAAAAAAAbs/b9xhpUM3pEI/s1024/Light%252520to%252520Sand.jpg", 81 | "https://lh3.googleusercontent.com/-fHY5h67QPi0/URqu0Cp4J1I/AAAAAAAAAbs/0lG6m94Z6vM/s1024/Little%252520Bit%252520of%252520Paradise.jpg", 82 | "https://lh5.googleusercontent.com/-TzF_LwrCnRM/URqu0RddPOI/AAAAAAAAAbs/gaj2dLiuX0s/s1024/Lone%252520Pine%252520Sunset.jpg", 83 | "https://lh3.googleusercontent.com/-4HdpJ4_DXU4/URqu046dJ9I/AAAAAAAAAbs/eBOodtk2_uk/s1024/Lonely%252520Rock.jpg", 84 | "https://lh6.googleusercontent.com/-erbF--z-W4s/URqu1ajSLkI/AAAAAAAAAbs/xjDCDO1INzM/s1024/Longue%252520Vue.jpg", 85 | "https://lh6.googleusercontent.com/-0CXJRdJaqvc/URqu1opNZNI/AAAAAAAAAbs/PFB2oPUU7Lk/s1024/Look%252520Me%252520in%252520the%252520Eye.jpg", 86 | "https://lh3.googleusercontent.com/-D_5lNxnDN6g/URqu2Tk7HVI/AAAAAAAAAbs/p0ddca9W__Y/s1024/Lost%252520in%252520a%252520Field.jpg", 87 | "https://lh6.googleusercontent.com/-flsqwMrIk2Q/URqu24PcmjI/AAAAAAAAAbs/5ocIH85XofM/s1024/Marshall%252520Beach%252520Sunset.jpg", 88 | "https://lh4.googleusercontent.com/-Y4lgryEVTmU/URqu28kG3gI/AAAAAAAAAbs/OjXpekqtbJ4/s1024/Mono%252520Lake%252520Blue.jpg", 89 | "https://lh4.googleusercontent.com/-AaHAJPmcGYA/URqu3PIldHI/AAAAAAAAAbs/lcTqk1SIcRs/s1024/Monument%252520Valley%252520Overlook.jpg", 90 | "https://lh4.googleusercontent.com/-vKxfdQ83dQA/URqu31Yq_BI/AAAAAAAAAbs/OUoGk_2AyfM/s1024/Moving%252520Rock.jpg", 91 | "https://lh5.googleusercontent.com/-CG62QiPpWXg/URqu4ia4vRI/AAAAAAAAAbs/0YOdqLAlcAc/s1024/Napali%252520Coast.jpg", 92 | "https://lh6.googleusercontent.com/-wdGrP5PMmJQ/URqu5PZvn7I/AAAAAAAAAbs/m0abEcdPXe4/s1024/One%252520Wheel.jpg", 93 | "https://lh6.googleusercontent.com/-6WS5DoCGuOA/URqu5qx1UgI/AAAAAAAAAbs/giMw2ixPvrY/s1024/Open%252520Sky.jpg", 94 | "https://lh6.googleusercontent.com/-u8EHKj8G8GQ/URqu55sM6yI/AAAAAAAAAbs/lIXX_GlTdmI/s1024/Orange%252520Sunset.jpg", 95 | "https://lh6.googleusercontent.com/-74Z5qj4bTDE/URqu6LSrJrI/AAAAAAAAAbs/XzmVkw90szQ/s1024/Orchid.jpg", 96 | "https://lh6.googleusercontent.com/-lEQE4h6TePE/URqu6t_lSkI/AAAAAAAAAbs/zvGYKOea_qY/s1024/Over%252520there.jpg", 97 | "https://lh5.googleusercontent.com/-cauH-53JH2M/URqu66v_USI/AAAAAAAAAbs/EucwwqclfKQ/s1024/Plumes.jpg", 98 | "https://lh3.googleusercontent.com/-eDLT2jHDoy4/URqu7axzkAI/AAAAAAAAAbs/iVZE-xJ7lZs/s1024/Rainbokeh.jpg", 99 | "https://lh5.googleusercontent.com/-j1NLqEFIyco/URqu8L1CGcI/AAAAAAAAAbs/aqZkgX66zlI/s1024/Rainbow.jpg", 100 | "https://lh5.googleusercontent.com/-DRnqmK0t4VU/URqu8XYN9yI/AAAAAAAAAbs/LgvF_592WLU/s1024/Rice%252520Fields.jpg", 101 | "https://lh3.googleusercontent.com/-hwh1v3EOGcQ/URqu8qOaKwI/AAAAAAAAAbs/IljRJRnbJGw/s1024/Rockaway%252520Fire%252520Sky.jpg", 102 | "https://lh5.googleusercontent.com/-wjV6FQk7tlk/URqu9jCQ8sI/AAAAAAAAAbs/RyYUpdo-c9o/s1024/Rockaway%252520Flow.jpg", 103 | "https://lh6.googleusercontent.com/-6cAXNfo7D20/URqu-BdzgPI/AAAAAAAAAbs/OmsYllzJqwo/s1024/Rockaway%252520Sunset%252520Sky.jpg", 104 | "https://lh3.googleusercontent.com/-sl8fpGPS-RE/URqu_BOkfgI/AAAAAAAAAbs/Dg2Fv-JxOeg/s1024/Russian%252520Ridge%252520Sunset.jpg", 105 | "https://lh6.googleusercontent.com/-gVtY36mMBIg/URqu_q91lkI/AAAAAAAAAbs/3CiFMBcy5MA/s1024/Rust%252520Knot.jpg", 106 | "https://lh6.googleusercontent.com/-GHeImuHqJBE/URqu_FKfVLI/AAAAAAAAAbs/axuEJeqam7Q/s1024/Sailing%252520Stones.jpg", 107 | "https://lh3.googleusercontent.com/-hBbYZjTOwGc/URqu_ycpIrI/AAAAAAAAAbs/nAdJUXnGJYE/s1024/Seahorse.jpg", 108 | "https://lh3.googleusercontent.com/-Iwi6-i6IexY/URqvAYZHsVI/AAAAAAAAAbs/5ETWl4qXsFE/s1024/Shinjuku%252520Street.jpg", 109 | "https://lh6.googleusercontent.com/-amhnySTM_MY/URqvAlb5KoI/AAAAAAAAAbs/pFCFgzlKsn0/s1024/Sierra%252520Heavens.jpg", 110 | "https://lh5.googleusercontent.com/-dJgjepFrYSo/URqvBVJZrAI/AAAAAAAAAbs/v-F5QWpYO6s/s1024/Sierra%252520Sunset.jpg", 111 | "https://lh4.googleusercontent.com/-Z4zGiC5nWdc/URqvBdEwivI/AAAAAAAAAbs/ZRZR1VJ84QA/s1024/Sin%252520Lights.jpg", 112 | "https://lh4.googleusercontent.com/-_0cYiWW8ccY/URqvBz3iM4I/AAAAAAAAAbs/9N_Wq8MhLTY/s1024/Starry%252520Lake.jpg", 113 | "https://lh3.googleusercontent.com/-A9LMoRyuQUA/URqvCYx_JoI/AAAAAAAAAbs/s7sde1Bz9cI/s1024/Starry%252520Night.jpg", 114 | "https://lh3.googleusercontent.com/-KtLJ3k858eY/URqvC_2h_bI/AAAAAAAAAbs/zzEBImwDA_g/s1024/Stream.jpg", 115 | "https://lh5.googleusercontent.com/-dFB7Lad6RcA/URqvDUftwWI/AAAAAAAAAbs/BrhoUtXTN7o/s1024/Strip%252520Sunset.jpg", 116 | "https://lh5.googleusercontent.com/-at6apgFiN20/URqvDyffUZI/AAAAAAAAAbs/clABCx171bE/s1024/Sunset%252520Hills.jpg", 117 | "https://lh4.googleusercontent.com/-7-EHhtQthII/URqvEYTk4vI/AAAAAAAAAbs/QSJZoB3YjVg/s1024/Tenaya%252520Lake%2525202.jpg", 118 | "https://lh6.googleusercontent.com/-8MrjV_a-Pok/URqvFC5repI/AAAAAAAAAbs/9inKTg9fbCE/s1024/Tenaya%252520Lake.jpg", 119 | "https://lh5.googleusercontent.com/-B1HW-z4zwao/URqvFWYRwUI/AAAAAAAAAbs/8Peli53Bs8I/s1024/The%252520Cave%252520BW.jpg", 120 | "https://lh3.googleusercontent.com/-PO4E-xZKAnQ/URqvGRqjYkI/AAAAAAAAAbs/42nyADFsXag/s1024/The%252520Fisherman.jpg", 121 | "https://lh4.googleusercontent.com/-iLyZlzfdy7s/URqvG0YScdI/AAAAAAAAAbs/1J9eDKmkXtk/s1024/The%252520Night%252520is%252520Coming.jpg", 122 | "https://lh6.googleusercontent.com/-G-k7YkkUco0/URqvHhah6fI/AAAAAAAAAbs/_taQQG7t0vo/s1024/The%252520Road.jpg", 123 | "https://lh6.googleusercontent.com/-h-ALJt7kSus/URqvIThqYfI/AAAAAAAAAbs/ejiv35olWS8/s1024/Tokyo%252520Heights.jpg", 124 | "https://lh5.googleusercontent.com/-Hy9k-TbS7xg/URqvIjQMOxI/AAAAAAAAAbs/RSpmmOATSkg/s1024/Tokyo%252520Highway.jpg", 125 | "https://lh6.googleusercontent.com/-83oOvMb4OZs/URqvJL0T7lI/AAAAAAAAAbs/c5TECZ6RONM/s1024/Tokyo%252520Smog.jpg", 126 | "https://lh3.googleusercontent.com/-FB-jfgREEfI/URqvJI3EXAI/AAAAAAAAAbs/XfyweiRF4v8/s1024/Tufa%252520at%252520Night.jpg", 127 | "https://lh4.googleusercontent.com/-vngKD5Z1U8w/URqvJUCEgPI/AAAAAAAAAbs/ulxCMVcU6EU/s1024/Valley%252520Sunset.jpg", 128 | "https://lh6.googleusercontent.com/-DOz5I2E2oMQ/URqvKMND1kI/AAAAAAAAAbs/Iqf0IsInleo/s1024/Windmill%252520Sunrise.jpg", 129 | "https://lh5.googleusercontent.com/-biyiyWcJ9MU/URqvKculiAI/AAAAAAAAAbs/jyPsCplJOpE/s1024/Windmill.jpg", 130 | "https://lh4.googleusercontent.com/-PDT167_xRdA/URqvK36mLcI/AAAAAAAAAbs/oi2ik9QseMI/s1024/Windmills.jpg", 131 | "https://lh5.googleusercontent.com/-kI_QdYx7VlU/URqvLXCB6gI/AAAAAAAAAbs/N31vlZ6u89o/s1024/Yet%252520Another%252520Rockaway%252520Sunset.jpg", 132 | "https://lh4.googleusercontent.com/-e9NHZ5k5MSs/URqvMIBZjtI/AAAAAAAAAbs/1fV810rDNfQ/s1024/Yosemite%252520Tree.jpg", 133 | }; 134 | 135 | /** 136 | * This are PicasaWeb thumbnail URLs and could potentially change. Ideally the PicasaWeb API 137 | * should be used to fetch the URLs. 138 | * 139 | * Credit to Romain Guy for the photos: 140 | * http://www.curious-creature.org/ 141 | * https://plus.google.com/109538161516040592207/about 142 | * http://www.flickr.com/photos/romainguy 143 | */ 144 | public final static String[] imageThumbUrls = new String[] { 145 | "https://lh6.googleusercontent.com/-55osAWw3x0Q/URquUtcFr5I/AAAAAAAAAbs/rWlj1RUKrYI/s240-c/A%252520Photographer.jpg", 146 | "https://lh4.googleusercontent.com/--dq8niRp7W4/URquVgmXvgI/AAAAAAAAAbs/-gnuLQfNnBA/s240-c/A%252520Song%252520of%252520Ice%252520and%252520Fire.jpg", 147 | "https://lh5.googleusercontent.com/-7qZeDtRKFKc/URquWZT1gOI/AAAAAAAAAbs/hqWgteyNXsg/s240-c/Another%252520Rockaway%252520Sunset.jpg", 148 | "https://lh3.googleusercontent.com/--L0Km39l5J8/URquXHGcdNI/AAAAAAAAAbs/3ZrSJNrSomQ/s240-c/Antelope%252520Butte.jpg", 149 | "https://lh6.googleusercontent.com/-8HO-4vIFnlw/URquZnsFgtI/AAAAAAAAAbs/WT8jViTF7vw/s240-c/Antelope%252520Hallway.jpg", 150 | "https://lh4.googleusercontent.com/-WIuWgVcU3Qw/URqubRVcj4I/AAAAAAAAAbs/YvbwgGjwdIQ/s240-c/Antelope%252520Walls.jpg", 151 | "https://lh6.googleusercontent.com/-UBmLbPELvoQ/URqucCdv0kI/AAAAAAAAAbs/IdNhr2VQoQs/s240-c/Apre%2525CC%252580s%252520la%252520Pluie.jpg", 152 | "https://lh3.googleusercontent.com/-s-AFpvgSeew/URquc6dF-JI/AAAAAAAAAbs/Mt3xNGRUd68/s240-c/Backlit%252520Cloud.jpg", 153 | "https://lh5.googleusercontent.com/-bvmif9a9YOQ/URquea3heHI/AAAAAAAAAbs/rcr6wyeQtAo/s240-c/Bee%252520and%252520Flower.jpg", 154 | "https://lh5.googleusercontent.com/-n7mdm7I7FGs/URqueT_BT-I/AAAAAAAAAbs/9MYmXlmpSAo/s240-c/Bonzai%252520Rock%252520Sunset.jpg", 155 | "https://lh6.googleusercontent.com/-4CN4X4t0M1k/URqufPozWzI/AAAAAAAAAbs/8wK41lg1KPs/s240-c/Caterpillar.jpg", 156 | "https://lh3.googleusercontent.com/-rrFnVC8xQEg/URqufdrLBaI/AAAAAAAAAbs/s69WYy_fl1E/s240-c/Chess.jpg", 157 | "https://lh5.googleusercontent.com/-WVpRptWH8Yw/URqugh-QmDI/AAAAAAAAAbs/E-MgBgtlUWU/s240-c/Chihuly.jpg", 158 | "https://lh5.googleusercontent.com/-0BDXkYmckbo/URquhKFW84I/AAAAAAAAAbs/ogQtHCTk2JQ/s240-c/Closed%252520Door.jpg", 159 | "https://lh3.googleusercontent.com/-PyggXXZRykM/URquh-kVvoI/AAAAAAAAAbs/hFtDwhtrHHQ/s240-c/Colorado%252520River%252520Sunset.jpg", 160 | "https://lh3.googleusercontent.com/-ZAs4dNZtALc/URquikvOCWI/AAAAAAAAAbs/DXz4h3dll1Y/s240-c/Colors%252520of%252520Autumn.jpg", 161 | "https://lh4.googleusercontent.com/-GztnWEIiMz8/URqukVCU7bI/AAAAAAAAAbs/jo2Hjv6MZ6M/s240-c/Countryside.jpg", 162 | "https://lh4.googleusercontent.com/-bEg9EZ9QoiM/URquklz3FGI/AAAAAAAAAbs/UUuv8Ac2BaE/s240-c/Death%252520Valley%252520-%252520Dunes.jpg", 163 | "https://lh6.googleusercontent.com/-ijQJ8W68tEE/URqulGkvFEI/AAAAAAAAAbs/zPXvIwi_rFw/s240-c/Delicate%252520Arch.jpg", 164 | "https://lh5.googleusercontent.com/-Oh8mMy2ieng/URqullDwehI/AAAAAAAAAbs/TbdeEfsaIZY/s240-c/Despair.jpg", 165 | "https://lh5.googleusercontent.com/-gl0y4UiAOlk/URqumC_KjBI/AAAAAAAAAbs/PM1eT7dn4oo/s240-c/Eagle%252520Fall%252520Sunrise.jpg", 166 | "https://lh3.googleusercontent.com/-hYYHd2_vXPQ/URqumtJa9eI/AAAAAAAAAbs/wAalXVkbSh0/s240-c/Electric%252520Storm.jpg", 167 | "https://lh5.googleusercontent.com/-PyY_yiyjPTo/URqunUOhHFI/AAAAAAAAAbs/azZoULNuJXc/s240-c/False%252520Kiva.jpg", 168 | "https://lh6.googleusercontent.com/-PYvLVdvXywk/URqunwd8hfI/AAAAAAAAAbs/qiMwgkFvf6I/s240-c/Fitzgerald%252520Streaks.jpg", 169 | "https://lh4.googleusercontent.com/-KIR_UobIIqY/URquoCZ9SlI/AAAAAAAAAbs/Y4d4q8sXu4c/s240-c/Foggy%252520Sunset.jpg", 170 | "https://lh6.googleusercontent.com/-9lzOk_OWZH0/URquoo4xYoI/AAAAAAAAAbs/AwgzHtNVCwU/s240-c/Frantic.jpg", 171 | "https://lh3.googleusercontent.com/-0X3JNaKaz48/URqupH78wpI/AAAAAAAAAbs/lHXxu_zbH8s/s240-c/Golden%252520Gate%252520Afternoon.jpg", 172 | "https://lh6.googleusercontent.com/-95sb5ag7ABc/URqupl95RDI/AAAAAAAAAbs/g73R20iVTRA/s240-c/Golden%252520Gate%252520Fog.jpg", 173 | "https://lh3.googleusercontent.com/-JB9v6rtgHhk/URqup21F-zI/AAAAAAAAAbs/64Fb8qMZWXk/s240-c/Golden%252520Grass.jpg", 174 | "https://lh4.googleusercontent.com/-EIBGfnuLtII/URquqVHwaRI/AAAAAAAAAbs/FA4McV2u8VE/s240-c/Grand%252520Teton.jpg", 175 | "https://lh4.googleusercontent.com/-WoMxZvmN9nY/URquq1v2AoI/AAAAAAAAAbs/grj5uMhL6NA/s240-c/Grass%252520Closeup.jpg", 176 | "https://lh3.googleusercontent.com/-6hZiEHXx64Q/URqurxvNdqI/AAAAAAAAAbs/kWMXM3o5OVI/s240-c/Green%252520Grass.jpg", 177 | "https://lh5.googleusercontent.com/-6LVb9OXtQ60/URquteBFuKI/AAAAAAAAAbs/4F4kRgecwFs/s240-c/Hanging%252520Leaf.jpg", 178 | "https://lh4.googleusercontent.com/-zAvf__52ONk/URqutT_IuxI/AAAAAAAAAbs/D_bcuc0thoU/s240-c/Highway%2525201.jpg", 179 | "https://lh6.googleusercontent.com/-H4SrUg615rA/URquuL27fXI/AAAAAAAAAbs/4aEqJfiMsOU/s240-c/Horseshoe%252520Bend%252520Sunset.jpg", 180 | "https://lh4.googleusercontent.com/-JhFi4fb_Pqw/URquuX-QXbI/AAAAAAAAAbs/IXpYUxuweYM/s240-c/Horseshoe%252520Bend.jpg", 181 | "https://lh5.googleusercontent.com/-UGgssvFRJ7g/URquueyJzGI/AAAAAAAAAbs/yYIBlLT0toM/s240-c/Into%252520the%252520Blue.jpg", 182 | "https://lh3.googleusercontent.com/-CH7KoupI7uI/URquu0FF__I/AAAAAAAAAbs/R7GDmI7v_G0/s240-c/Jelly%252520Fish%2525202.jpg", 183 | "https://lh4.googleusercontent.com/-pwuuw6yhg8U/URquvPxR3FI/AAAAAAAAAbs/VNGk6f-tsGE/s240-c/Jelly%252520Fish%2525203.jpg", 184 | "https://lh5.googleusercontent.com/-GoUQVw1fnFw/URquv6xbC0I/AAAAAAAAAbs/zEUVTQQ43Zc/s240-c/Kauai.jpg", 185 | "https://lh6.googleusercontent.com/-8QdYYQEpYjw/URquwvdh88I/AAAAAAAAAbs/cktDy-ysfHo/s240-c/Kyoto%252520Sunset.jpg", 186 | "https://lh4.googleusercontent.com/-vPeekyDjOE0/URquwzJ28qI/AAAAAAAAAbs/qxcyXULsZrg/s240-c/Lake%252520Tahoe%252520Colors.jpg", 187 | "https://lh4.googleusercontent.com/-xBPxWpD4yxU/URquxWHk8AI/AAAAAAAAAbs/ARDPeDYPiMY/s240-c/Lava%252520from%252520the%252520Sky.jpg", 188 | "https://lh3.googleusercontent.com/-897VXrJB6RE/URquxxxd-5I/AAAAAAAAAbs/j-Cz4T4YvIw/s240-c/Leica%25252050mm%252520Summilux.jpg", 189 | "https://lh5.googleusercontent.com/-qSJ4D4iXzGo/URquyDWiJ1I/AAAAAAAAAbs/k2pBXeWehOA/s240-c/Leica%25252050mm%252520Summilux.jpg", 190 | "https://lh6.googleusercontent.com/-dwlPg83vzLg/URquylTVuFI/AAAAAAAAAbs/G6SyQ8b4YsI/s240-c/Leica%252520M8%252520%252528Front%252529.jpg", 191 | "https://lh3.googleusercontent.com/-R3_EYAyJvfk/URquzQBv8eI/AAAAAAAAAbs/b9xhpUM3pEI/s240-c/Light%252520to%252520Sand.jpg", 192 | "https://lh3.googleusercontent.com/-fHY5h67QPi0/URqu0Cp4J1I/AAAAAAAAAbs/0lG6m94Z6vM/s240-c/Little%252520Bit%252520of%252520Paradise.jpg", 193 | "https://lh5.googleusercontent.com/-TzF_LwrCnRM/URqu0RddPOI/AAAAAAAAAbs/gaj2dLiuX0s/s240-c/Lone%252520Pine%252520Sunset.jpg", 194 | "https://lh3.googleusercontent.com/-4HdpJ4_DXU4/URqu046dJ9I/AAAAAAAAAbs/eBOodtk2_uk/s240-c/Lonely%252520Rock.jpg", 195 | "https://lh6.googleusercontent.com/-erbF--z-W4s/URqu1ajSLkI/AAAAAAAAAbs/xjDCDO1INzM/s240-c/Longue%252520Vue.jpg", 196 | "https://lh6.googleusercontent.com/-0CXJRdJaqvc/URqu1opNZNI/AAAAAAAAAbs/PFB2oPUU7Lk/s240-c/Look%252520Me%252520in%252520the%252520Eye.jpg", 197 | "https://lh3.googleusercontent.com/-D_5lNxnDN6g/URqu2Tk7HVI/AAAAAAAAAbs/p0ddca9W__Y/s240-c/Lost%252520in%252520a%252520Field.jpg", 198 | "https://lh6.googleusercontent.com/-flsqwMrIk2Q/URqu24PcmjI/AAAAAAAAAbs/5ocIH85XofM/s240-c/Marshall%252520Beach%252520Sunset.jpg", 199 | "https://lh4.googleusercontent.com/-Y4lgryEVTmU/URqu28kG3gI/AAAAAAAAAbs/OjXpekqtbJ4/s240-c/Mono%252520Lake%252520Blue.jpg", 200 | "https://lh4.googleusercontent.com/-AaHAJPmcGYA/URqu3PIldHI/AAAAAAAAAbs/lcTqk1SIcRs/s240-c/Monument%252520Valley%252520Overlook.jpg", 201 | "https://lh4.googleusercontent.com/-vKxfdQ83dQA/URqu31Yq_BI/AAAAAAAAAbs/OUoGk_2AyfM/s240-c/Moving%252520Rock.jpg", 202 | "https://lh5.googleusercontent.com/-CG62QiPpWXg/URqu4ia4vRI/AAAAAAAAAbs/0YOdqLAlcAc/s240-c/Napali%252520Coast.jpg", 203 | "https://lh6.googleusercontent.com/-wdGrP5PMmJQ/URqu5PZvn7I/AAAAAAAAAbs/m0abEcdPXe4/s240-c/One%252520Wheel.jpg", 204 | "https://lh6.googleusercontent.com/-6WS5DoCGuOA/URqu5qx1UgI/AAAAAAAAAbs/giMw2ixPvrY/s240-c/Open%252520Sky.jpg", 205 | "https://lh6.googleusercontent.com/-u8EHKj8G8GQ/URqu55sM6yI/AAAAAAAAAbs/lIXX_GlTdmI/s240-c/Orange%252520Sunset.jpg", 206 | "https://lh6.googleusercontent.com/-74Z5qj4bTDE/URqu6LSrJrI/AAAAAAAAAbs/XzmVkw90szQ/s240-c/Orchid.jpg", 207 | "https://lh6.googleusercontent.com/-lEQE4h6TePE/URqu6t_lSkI/AAAAAAAAAbs/zvGYKOea_qY/s240-c/Over%252520there.jpg", 208 | "https://lh5.googleusercontent.com/-cauH-53JH2M/URqu66v_USI/AAAAAAAAAbs/EucwwqclfKQ/s240-c/Plumes.jpg", 209 | "https://lh3.googleusercontent.com/-eDLT2jHDoy4/URqu7axzkAI/AAAAAAAAAbs/iVZE-xJ7lZs/s240-c/Rainbokeh.jpg", 210 | "https://lh5.googleusercontent.com/-j1NLqEFIyco/URqu8L1CGcI/AAAAAAAAAbs/aqZkgX66zlI/s240-c/Rainbow.jpg", 211 | "https://lh5.googleusercontent.com/-DRnqmK0t4VU/URqu8XYN9yI/AAAAAAAAAbs/LgvF_592WLU/s240-c/Rice%252520Fields.jpg", 212 | "https://lh3.googleusercontent.com/-hwh1v3EOGcQ/URqu8qOaKwI/AAAAAAAAAbs/IljRJRnbJGw/s240-c/Rockaway%252520Fire%252520Sky.jpg", 213 | "https://lh5.googleusercontent.com/-wjV6FQk7tlk/URqu9jCQ8sI/AAAAAAAAAbs/RyYUpdo-c9o/s240-c/Rockaway%252520Flow.jpg", 214 | "https://lh6.googleusercontent.com/-6cAXNfo7D20/URqu-BdzgPI/AAAAAAAAAbs/OmsYllzJqwo/s240-c/Rockaway%252520Sunset%252520Sky.jpg", 215 | "https://lh3.googleusercontent.com/-sl8fpGPS-RE/URqu_BOkfgI/AAAAAAAAAbs/Dg2Fv-JxOeg/s240-c/Russian%252520Ridge%252520Sunset.jpg", 216 | "https://lh6.googleusercontent.com/-gVtY36mMBIg/URqu_q91lkI/AAAAAAAAAbs/3CiFMBcy5MA/s240-c/Rust%252520Knot.jpg", 217 | "https://lh6.googleusercontent.com/-GHeImuHqJBE/URqu_FKfVLI/AAAAAAAAAbs/axuEJeqam7Q/s240-c/Sailing%252520Stones.jpg", 218 | "https://lh3.googleusercontent.com/-hBbYZjTOwGc/URqu_ycpIrI/AAAAAAAAAbs/nAdJUXnGJYE/s240-c/Seahorse.jpg", 219 | "https://lh3.googleusercontent.com/-Iwi6-i6IexY/URqvAYZHsVI/AAAAAAAAAbs/5ETWl4qXsFE/s240-c/Shinjuku%252520Street.jpg", 220 | "https://lh6.googleusercontent.com/-amhnySTM_MY/URqvAlb5KoI/AAAAAAAAAbs/pFCFgzlKsn0/s240-c/Sierra%252520Heavens.jpg", 221 | "https://lh5.googleusercontent.com/-dJgjepFrYSo/URqvBVJZrAI/AAAAAAAAAbs/v-F5QWpYO6s/s240-c/Sierra%252520Sunset.jpg", 222 | "https://lh4.googleusercontent.com/-Z4zGiC5nWdc/URqvBdEwivI/AAAAAAAAAbs/ZRZR1VJ84QA/s240-c/Sin%252520Lights.jpg", 223 | "https://lh4.googleusercontent.com/-_0cYiWW8ccY/URqvBz3iM4I/AAAAAAAAAbs/9N_Wq8MhLTY/s240-c/Starry%252520Lake.jpg", 224 | "https://lh3.googleusercontent.com/-A9LMoRyuQUA/URqvCYx_JoI/AAAAAAAAAbs/s7sde1Bz9cI/s240-c/Starry%252520Night.jpg", 225 | "https://lh3.googleusercontent.com/-KtLJ3k858eY/URqvC_2h_bI/AAAAAAAAAbs/zzEBImwDA_g/s240-c/Stream.jpg", 226 | "https://lh5.googleusercontent.com/-dFB7Lad6RcA/URqvDUftwWI/AAAAAAAAAbs/BrhoUtXTN7o/s240-c/Strip%252520Sunset.jpg", 227 | "https://lh5.googleusercontent.com/-at6apgFiN20/URqvDyffUZI/AAAAAAAAAbs/clABCx171bE/s240-c/Sunset%252520Hills.jpg", 228 | "https://lh4.googleusercontent.com/-7-EHhtQthII/URqvEYTk4vI/AAAAAAAAAbs/QSJZoB3YjVg/s240-c/Tenaya%252520Lake%2525202.jpg", 229 | "https://lh6.googleusercontent.com/-8MrjV_a-Pok/URqvFC5repI/AAAAAAAAAbs/9inKTg9fbCE/s240-c/Tenaya%252520Lake.jpg", 230 | "https://lh5.googleusercontent.com/-B1HW-z4zwao/URqvFWYRwUI/AAAAAAAAAbs/8Peli53Bs8I/s240-c/The%252520Cave%252520BW.jpg", 231 | "https://lh3.googleusercontent.com/-PO4E-xZKAnQ/URqvGRqjYkI/AAAAAAAAAbs/42nyADFsXag/s240-c/The%252520Fisherman.jpg", 232 | "https://lh4.googleusercontent.com/-iLyZlzfdy7s/URqvG0YScdI/AAAAAAAAAbs/1J9eDKmkXtk/s240-c/The%252520Night%252520is%252520Coming.jpg", 233 | "https://lh6.googleusercontent.com/-G-k7YkkUco0/URqvHhah6fI/AAAAAAAAAbs/_taQQG7t0vo/s240-c/The%252520Road.jpg", 234 | "https://lh6.googleusercontent.com/-h-ALJt7kSus/URqvIThqYfI/AAAAAAAAAbs/ejiv35olWS8/s240-c/Tokyo%252520Heights.jpg", 235 | "https://lh5.googleusercontent.com/-Hy9k-TbS7xg/URqvIjQMOxI/AAAAAAAAAbs/RSpmmOATSkg/s240-c/Tokyo%252520Highway.jpg", 236 | "https://lh6.googleusercontent.com/-83oOvMb4OZs/URqvJL0T7lI/AAAAAAAAAbs/c5TECZ6RONM/s240-c/Tokyo%252520Smog.jpg", 237 | "https://lh3.googleusercontent.com/-FB-jfgREEfI/URqvJI3EXAI/AAAAAAAAAbs/XfyweiRF4v8/s240-c/Tufa%252520at%252520Night.jpg", 238 | "https://lh4.googleusercontent.com/-vngKD5Z1U8w/URqvJUCEgPI/AAAAAAAAAbs/ulxCMVcU6EU/s240-c/Valley%252520Sunset.jpg", 239 | "https://lh6.googleusercontent.com/-DOz5I2E2oMQ/URqvKMND1kI/AAAAAAAAAbs/Iqf0IsInleo/s240-c/Windmill%252520Sunrise.jpg", 240 | "https://lh5.googleusercontent.com/-biyiyWcJ9MU/URqvKculiAI/AAAAAAAAAbs/jyPsCplJOpE/s240-c/Windmill.jpg", 241 | "https://lh4.googleusercontent.com/-PDT167_xRdA/URqvK36mLcI/AAAAAAAAAbs/oi2ik9QseMI/s240-c/Windmills.jpg", 242 | "https://lh5.googleusercontent.com/-kI_QdYx7VlU/URqvLXCB6gI/AAAAAAAAAbs/N31vlZ6u89o/s240-c/Yet%252520Another%252520Rockaway%252520Sunset.jpg", 243 | "https://lh4.googleusercontent.com/-e9NHZ5k5MSs/URqvMIBZjtI/AAAAAAAAAbs/1fV810rDNfQ/s240-c/Yosemite%252520Tree.jpg", 244 | }; 245 | } 246 | -------------------------------------------------------------------------------- /Application/src/main/java/com/example/android/displayingbitmaps/ui/ImageDetailActivity.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2012 The Android Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.example.android.displayingbitmaps.ui; 18 | 19 | import android.annotation.TargetApi; 20 | import android.app.ActionBar; 21 | import android.os.Build.VERSION_CODES; 22 | import android.os.Bundle; 23 | import android.support.v4.app.Fragment; 24 | import android.support.v4.app.FragmentActivity; 25 | import android.support.v4.app.FragmentManager; 26 | import android.support.v4.app.FragmentStatePagerAdapter; 27 | import android.support.v4.app.NavUtils; 28 | import android.support.v4.view.ViewPager; 29 | import android.util.DisplayMetrics; 30 | import android.view.Menu; 31 | import android.view.MenuItem; 32 | import android.view.View; 33 | import android.view.View.OnClickListener; 34 | import android.view.WindowManager.LayoutParams; 35 | import android.widget.Toast; 36 | 37 | import com.example.android.displayingbitmaps.BuildConfig; 38 | import com.example.android.displayingbitmaps.R; 39 | import com.example.android.displayingbitmaps.provider.Images; 40 | import com.example.android.displayingbitmaps.util.ImageCache; 41 | import com.example.android.displayingbitmaps.util.ImageFetcher; 42 | import com.example.android.displayingbitmaps.util.Utils; 43 | 44 | public class ImageDetailActivity extends FragmentActivity implements OnClickListener { 45 | private static final String IMAGE_CACHE_DIR = "images"; 46 | public static final String EXTRA_IMAGE = "extra_image"; 47 | 48 | private ImagePagerAdapter mAdapter; 49 | private ImageFetcher mImageFetcher; 50 | private ViewPager mPager; 51 | 52 | @TargetApi(VERSION_CODES.HONEYCOMB) 53 | @Override 54 | public void onCreate(Bundle savedInstanceState) { 55 | if (BuildConfig.DEBUG) { 56 | Utils.enableStrictMode(); 57 | } 58 | super.onCreate(savedInstanceState); 59 | setContentView(R.layout.image_detail_pager); 60 | 61 | // Fetch screen height and width, to use as our max size when loading images as this 62 | // activity runs full screen 63 | final DisplayMetrics displayMetrics = new DisplayMetrics(); 64 | getWindowManager().getDefaultDisplay().getMetrics(displayMetrics); 65 | final int height = displayMetrics.heightPixels; 66 | final int width = displayMetrics.widthPixels; 67 | 68 | // For this sample we'll use half of the longest width to resize our images. As the 69 | // image scaling ensures the image is larger than this, we should be left with a 70 | // resolution that is appropriate for both portrait and landscape. For best image quality 71 | // we shouldn't divide by 2, but this will use more memory and require a larger memory 72 | // cache. 73 | final int longest = (height > width ? height : width) / 2; 74 | 75 | ImageCache.ImageCacheParams cacheParams = 76 | new ImageCache.ImageCacheParams(this, IMAGE_CACHE_DIR); 77 | cacheParams.setMemCacheSizePercent(0.25f); // Set memory cache to 25% of app memory 78 | 79 | // The ImageFetcher takes care of loading images into our ImageView children asynchronously 80 | mImageFetcher = new ImageFetcher(this, longest); 81 | mImageFetcher.addImageCache(getSupportFragmentManager(), cacheParams); 82 | mImageFetcher.setImageFadeIn(false); 83 | 84 | // Set up ViewPager and backing adapter 85 | mAdapter = new ImagePagerAdapter(getSupportFragmentManager(), Images.imageUrls.length); 86 | mPager = (ViewPager) findViewById(R.id.pager); 87 | mPager.setAdapter(mAdapter); 88 | mPager.setPageMargin((int) getResources().getDimension(R.dimen.horizontal_page_margin)); 89 | mPager.setOffscreenPageLimit(2); 90 | 91 | // Set up activity to go full screen 92 | getWindow().addFlags(LayoutParams.FLAG_FULLSCREEN); 93 | 94 | // Enable some additional newer visibility and ActionBar features to create a more 95 | // immersive photo viewing experience 96 | if (Utils.hasHoneycomb()) { 97 | final ActionBar actionBar = getActionBar(); 98 | 99 | // Hide title text and set home as up 100 | actionBar.setDisplayShowTitleEnabled(false); 101 | actionBar.setDisplayHomeAsUpEnabled(true); 102 | 103 | // Hide and show the ActionBar as the visibility changes 104 | mPager.setOnSystemUiVisibilityChangeListener( 105 | new View.OnSystemUiVisibilityChangeListener() { 106 | @Override 107 | public void onSystemUiVisibilityChange(int vis) { 108 | if ((vis & View.SYSTEM_UI_FLAG_LOW_PROFILE) != 0) { 109 | actionBar.hide(); 110 | } else { 111 | actionBar.show(); 112 | } 113 | } 114 | }); 115 | 116 | // Start low profile mode and hide ActionBar 117 | mPager.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE); 118 | actionBar.hide(); 119 | } 120 | 121 | // Set the current item based on the extra passed in to this activity 122 | final int extraCurrentItem = getIntent().getIntExtra(EXTRA_IMAGE, -1); 123 | if (extraCurrentItem != -1) { 124 | mPager.setCurrentItem(extraCurrentItem); 125 | } 126 | } 127 | 128 | @Override 129 | public void onResume() { 130 | super.onResume(); 131 | mImageFetcher.setExitTasksEarly(false); 132 | } 133 | 134 | @Override 135 | protected void onPause() { 136 | super.onPause(); 137 | mImageFetcher.setExitTasksEarly(true); 138 | mImageFetcher.flushCache(); 139 | } 140 | 141 | @Override 142 | protected void onDestroy() { 143 | super.onDestroy(); 144 | mImageFetcher.closeCache(); 145 | } 146 | 147 | @Override 148 | public boolean onOptionsItemSelected(MenuItem item) { 149 | switch (item.getItemId()) { 150 | case android.R.id.home: 151 | NavUtils.navigateUpFromSameTask(this); 152 | return true; 153 | case R.id.clear_cache: 154 | mImageFetcher.clearCache(); 155 | Toast.makeText( 156 | this, R.string.clear_cache_complete_toast,Toast.LENGTH_SHORT).show(); 157 | return true; 158 | } 159 | return super.onOptionsItemSelected(item); 160 | } 161 | 162 | @Override 163 | public boolean onCreateOptionsMenu(Menu menu) { 164 | getMenuInflater().inflate(R.menu.main_menu, menu); 165 | return true; 166 | } 167 | 168 | /** 169 | * Called by the ViewPager child fragments to load images via the one ImageFetcher 170 | */ 171 | public ImageFetcher getImageFetcher() { 172 | return mImageFetcher; 173 | } 174 | 175 | /** 176 | * The main adapter that backs the ViewPager. A subclass of FragmentStatePagerAdapter as there 177 | * could be a large number of items in the ViewPager and we don't want to retain them all in 178 | * memory at once but create/destroy them on the fly. 179 | */ 180 | private class ImagePagerAdapter extends FragmentStatePagerAdapter { 181 | private final int mSize; 182 | 183 | public ImagePagerAdapter(FragmentManager fm, int size) { 184 | super(fm); 185 | mSize = size; 186 | } 187 | 188 | @Override 189 | public int getCount() { 190 | return mSize; 191 | } 192 | 193 | @Override 194 | public Fragment getItem(int position) { 195 | return ImageDetailFragment.newInstance(Images.imageUrls[position]); 196 | } 197 | } 198 | 199 | /** 200 | * Set on the ImageView in the ViewPager children fragments, to enable/disable low profile mode 201 | * when the ImageView is touched. 202 | */ 203 | @TargetApi(VERSION_CODES.HONEYCOMB) 204 | @Override 205 | public void onClick(View v) { 206 | final int vis = mPager.getSystemUiVisibility(); 207 | if ((vis & View.SYSTEM_UI_FLAG_LOW_PROFILE) != 0) { 208 | mPager.setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE); 209 | } else { 210 | mPager.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE); 211 | } 212 | } 213 | } 214 | -------------------------------------------------------------------------------- /Application/src/main/java/com/example/android/displayingbitmaps/ui/ImageDetailFragment.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2012 The Android Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.example.android.displayingbitmaps.ui; 18 | 19 | import android.os.Bundle; 20 | import android.support.v4.app.Fragment; 21 | import android.view.LayoutInflater; 22 | import android.view.View; 23 | import android.view.View.OnClickListener; 24 | import android.view.ViewGroup; 25 | import android.widget.ImageView; 26 | import android.widget.ProgressBar; 27 | 28 | import com.example.android.displayingbitmaps.R; 29 | import com.example.android.displayingbitmaps.util.ImageFetcher; 30 | import com.example.android.displayingbitmaps.util.ImageWorker; 31 | import com.example.android.displayingbitmaps.util.Utils; 32 | 33 | /** 34 | * This fragment will populate the children of the ViewPager from {@link ImageDetailActivity}. 35 | */ 36 | public class ImageDetailFragment extends Fragment implements ImageWorker.OnImageLoadedListener { 37 | private static final String IMAGE_DATA_EXTRA = "extra_image_data"; 38 | private String mImageUrl; 39 | private ImageView mImageView; 40 | private ProgressBar mProgressBar; 41 | private ImageFetcher mImageFetcher; 42 | 43 | /** 44 | * Factory method to generate a new instance of the fragment given an image number. 45 | * 46 | * @param imageUrl The image url to load 47 | * @return A new instance of ImageDetailFragment with imageNum extras 48 | */ 49 | public static ImageDetailFragment newInstance(String imageUrl) { 50 | final ImageDetailFragment f = new ImageDetailFragment(); 51 | 52 | final Bundle args = new Bundle(); 53 | args.putString(IMAGE_DATA_EXTRA, imageUrl); 54 | f.setArguments(args); 55 | 56 | return f; 57 | } 58 | 59 | /** 60 | * Empty constructor as per the Fragment documentation 61 | */ 62 | public ImageDetailFragment() {} 63 | 64 | /** 65 | * Populate image using a url from extras, use the convenience factory method 66 | * {@link ImageDetailFragment#newInstance(String)} to create this fragment. 67 | */ 68 | @Override 69 | public void onCreate(Bundle savedInstanceState) { 70 | super.onCreate(savedInstanceState); 71 | mImageUrl = getArguments() != null ? getArguments().getString(IMAGE_DATA_EXTRA) : null; 72 | } 73 | 74 | @Override 75 | public View onCreateView(LayoutInflater inflater, ViewGroup container, 76 | Bundle savedInstanceState) { 77 | // Inflate and locate the main ImageView 78 | final View v = inflater.inflate(R.layout.image_detail_fragment, container, false); 79 | mImageView = (ImageView) v.findViewById(R.id.imageView); 80 | mProgressBar = (ProgressBar) v.findViewById(R.id.progressbar); 81 | return v; 82 | } 83 | 84 | @Override 85 | public void onActivityCreated(Bundle savedInstanceState) { 86 | super.onActivityCreated(savedInstanceState); 87 | 88 | // Use the parent activity to load the image asynchronously into the ImageView (so a single 89 | // cache can be used over all pages in the ViewPager 90 | if (ImageDetailActivity.class.isInstance(getActivity())) { 91 | mImageFetcher = ((ImageDetailActivity) getActivity()).getImageFetcher(); 92 | mImageFetcher.loadImage(mImageUrl, mImageView, this); 93 | } 94 | 95 | // Pass clicks on the ImageView to the parent activity to handle 96 | if (OnClickListener.class.isInstance(getActivity()) && Utils.hasHoneycomb()) { 97 | mImageView.setOnClickListener((OnClickListener) getActivity()); 98 | } 99 | } 100 | 101 | @Override 102 | public void onDestroy() { 103 | super.onDestroy(); 104 | if (mImageView != null) { 105 | // Cancel any pending image work 106 | ImageWorker.cancelWork(mImageView); 107 | mImageView.setImageDrawable(null); 108 | } 109 | } 110 | 111 | @Override 112 | public void onImageLoaded(boolean success) { 113 | // Set loading spinner to gone once image has loaded. Cloud also show 114 | // an error view here if needed. 115 | mProgressBar.setVisibility(View.GONE); 116 | } 117 | } 118 | -------------------------------------------------------------------------------- /Application/src/main/java/com/example/android/displayingbitmaps/ui/ImageGridActivity.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2012 The Android Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.example.android.displayingbitmaps.ui; 18 | 19 | import android.os.Bundle; 20 | import android.support.v4.app.FragmentActivity; 21 | import android.support.v4.app.FragmentTransaction; 22 | 23 | import com.example.android.displayingbitmaps.BuildConfig; 24 | import com.example.android.displayingbitmaps.util.Utils; 25 | 26 | /** 27 | * Simple FragmentActivity to hold the main {@link ImageGridFragment} and not much else. 28 | */ 29 | public class ImageGridActivity extends FragmentActivity { 30 | private static final String TAG = "ImageGridActivity"; 31 | 32 | @Override 33 | protected void onCreate(Bundle savedInstanceState) { 34 | if (BuildConfig.DEBUG) { 35 | Utils.enableStrictMode(); 36 | } 37 | super.onCreate(savedInstanceState); 38 | 39 | if (getSupportFragmentManager().findFragmentByTag(TAG) == null) { 40 | final FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); 41 | ft.add(android.R.id.content, new ImageGridFragment(), TAG); 42 | ft.commit(); 43 | } 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /Application/src/main/java/com/example/android/displayingbitmaps/ui/ImageGridFragment.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2012 The Android Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.example.android.displayingbitmaps.ui; 18 | 19 | import android.annotation.TargetApi; 20 | import android.app.ActivityOptions; 21 | import android.content.Context; 22 | import android.content.Intent; 23 | import android.os.Build.VERSION_CODES; 24 | import android.os.Bundle; 25 | import android.support.v4.app.Fragment; 26 | import android.util.TypedValue; 27 | import android.view.LayoutInflater; 28 | import android.view.Menu; 29 | import android.view.MenuInflater; 30 | import android.view.MenuItem; 31 | import android.view.View; 32 | import android.view.ViewGroup; 33 | import android.view.ViewGroup.LayoutParams; 34 | import android.view.ViewTreeObserver; 35 | import android.widget.AbsListView; 36 | import android.widget.AdapterView; 37 | import android.widget.BaseAdapter; 38 | import android.widget.GridView; 39 | import android.widget.ImageView; 40 | import android.widget.Toast; 41 | 42 | import com.example.android.common.logger.Log; 43 | import com.example.android.displayingbitmaps.BuildConfig; 44 | import com.example.android.displayingbitmaps.R; 45 | import com.example.android.displayingbitmaps.provider.Images; 46 | import com.example.android.displayingbitmaps.util.ImageCache; 47 | import com.example.android.displayingbitmaps.util.ImageFetcher; 48 | import com.example.android.displayingbitmaps.util.Utils; 49 | 50 | /** 51 | * The main fragment that powers the ImageGridActivity screen. Fairly straight forward GridView 52 | * implementation with the key addition being the ImageWorker class w/ImageCache to load children 53 | * asynchronously, keeping the UI nice and smooth and caching thumbnails for quick retrieval. The 54 | * cache is retained over configuration changes like orientation change so the images are populated 55 | * quickly if, for example, the user rotates the device. 56 | */ 57 | public class ImageGridFragment extends Fragment implements AdapterView.OnItemClickListener { 58 | private static final String TAG = "ImageGridFragment"; 59 | private static final String IMAGE_CACHE_DIR = "thumbs"; 60 | 61 | private int mImageThumbSize; 62 | private int mImageThumbSpacing; 63 | private ImageAdapter mAdapter; 64 | private ImageFetcher mImageFetcher; 65 | 66 | /** 67 | * Empty constructor as per the Fragment documentation 68 | */ 69 | public ImageGridFragment() {} 70 | 71 | @Override 72 | public void onCreate(Bundle savedInstanceState) { 73 | super.onCreate(savedInstanceState); 74 | setHasOptionsMenu(true); 75 | 76 | mImageThumbSize = getResources().getDimensionPixelSize(R.dimen.image_thumbnail_size); 77 | mImageThumbSpacing = getResources().getDimensionPixelSize(R.dimen.image_thumbnail_spacing); 78 | 79 | mAdapter = new ImageAdapter(getActivity()); 80 | 81 | ImageCache.ImageCacheParams cacheParams = 82 | new ImageCache.ImageCacheParams(getActivity(), IMAGE_CACHE_DIR); 83 | 84 | cacheParams.setMemCacheSizePercent(0.25f); // Set memory cache to 25% of app memory 85 | 86 | // The ImageFetcher takes care of loading images into our ImageView children asynchronously 87 | mImageFetcher = new ImageFetcher(getActivity(), mImageThumbSize); 88 | mImageFetcher.setLoadingImage(R.drawable.empty_photo); 89 | mImageFetcher.addImageCache(getActivity().getSupportFragmentManager(), cacheParams); 90 | } 91 | 92 | @Override 93 | public View onCreateView( 94 | LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 95 | 96 | final View v = inflater.inflate(R.layout.image_grid_fragment, container, false); 97 | final GridView mGridView = (GridView) v.findViewById(R.id.gridView); 98 | mGridView.setAdapter(mAdapter); 99 | mGridView.setOnItemClickListener(this); 100 | mGridView.setOnScrollListener(new AbsListView.OnScrollListener() { 101 | @Override 102 | public void onScrollStateChanged(AbsListView absListView, int scrollState) { 103 | // Pause fetcher to ensure smoother scrolling when flinging 104 | if (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_FLING) { 105 | // Before Honeycomb pause image loading on scroll to help with performance 106 | if (!Utils.hasHoneycomb()) { 107 | mImageFetcher.setPauseWork(true); 108 | } 109 | } else { 110 | mImageFetcher.setPauseWork(false); 111 | } 112 | } 113 | 114 | @Override 115 | public void onScroll(AbsListView absListView, int firstVisibleItem, 116 | int visibleItemCount, int totalItemCount) { 117 | } 118 | }); 119 | 120 | // This listener is used to get the final width of the GridView and then calculate the 121 | // number of columns and the width of each column. The width of each column is variable 122 | // as the GridView has stretchMode=columnWidth. The column width is used to set the height 123 | // of each view so we get nice square thumbnails. 124 | mGridView.getViewTreeObserver().addOnGlobalLayoutListener( 125 | new ViewTreeObserver.OnGlobalLayoutListener() { 126 | @TargetApi(VERSION_CODES.JELLY_BEAN) 127 | @Override 128 | public void onGlobalLayout() { 129 | if (mAdapter.getNumColumns() == 0) { 130 | final int numColumns = (int) Math.floor( 131 | mGridView.getWidth() / (mImageThumbSize + mImageThumbSpacing)); 132 | if (numColumns > 0) { 133 | final int columnWidth = 134 | (mGridView.getWidth() / numColumns) - mImageThumbSpacing; 135 | mAdapter.setNumColumns(numColumns); 136 | mAdapter.setItemHeight(columnWidth); 137 | if (BuildConfig.DEBUG) { 138 | Log.d(TAG, "onCreateView - numColumns set to " + numColumns); 139 | } 140 | if (Utils.hasJellyBean()) { 141 | mGridView.getViewTreeObserver() 142 | .removeOnGlobalLayoutListener(this); 143 | } else { 144 | mGridView.getViewTreeObserver() 145 | .removeGlobalOnLayoutListener(this); 146 | } 147 | } 148 | } 149 | } 150 | }); 151 | 152 | return v; 153 | } 154 | 155 | @Override 156 | public void onResume() { 157 | super.onResume(); 158 | mImageFetcher.setExitTasksEarly(false); 159 | mAdapter.notifyDataSetChanged(); 160 | } 161 | 162 | @Override 163 | public void onPause() { 164 | super.onPause(); 165 | mImageFetcher.setPauseWork(false); 166 | mImageFetcher.setExitTasksEarly(true); 167 | mImageFetcher.flushCache(); 168 | } 169 | 170 | @Override 171 | public void onDestroy() { 172 | super.onDestroy(); 173 | mImageFetcher.closeCache(); 174 | } 175 | 176 | @TargetApi(VERSION_CODES.JELLY_BEAN) 177 | @Override 178 | public void onItemClick(AdapterView parent, View v, int position, long id) { 179 | final Intent i = new Intent(getActivity(), ImageDetailActivity.class); 180 | i.putExtra(ImageDetailActivity.EXTRA_IMAGE, (int) id); 181 | if (Utils.hasJellyBean()) { 182 | // makeThumbnailScaleUpAnimation() looks kind of ugly here as the loading spinner may 183 | // show plus the thumbnail image in GridView is cropped. so using 184 | // makeScaleUpAnimation() instead. 185 | ActivityOptions options = 186 | ActivityOptions.makeScaleUpAnimation(v, 0, 0, v.getWidth(), v.getHeight()); 187 | getActivity().startActivity(i, options.toBundle()); 188 | } else { 189 | startActivity(i); 190 | } 191 | } 192 | 193 | @Override 194 | public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { 195 | inflater.inflate(R.menu.main_menu, menu); 196 | } 197 | 198 | @Override 199 | public boolean onOptionsItemSelected(MenuItem item) { 200 | switch (item.getItemId()) { 201 | case R.id.clear_cache: 202 | mImageFetcher.clearCache(); 203 | Toast.makeText(getActivity(), R.string.clear_cache_complete_toast, 204 | Toast.LENGTH_SHORT).show(); 205 | return true; 206 | } 207 | return super.onOptionsItemSelected(item); 208 | } 209 | 210 | /** 211 | * The main adapter that backs the GridView. This is fairly standard except the number of 212 | * columns in the GridView is used to create a fake top row of empty views as we use a 213 | * transparent ActionBar and don't want the real top row of images to start off covered by it. 214 | */ 215 | private class ImageAdapter extends BaseAdapter { 216 | 217 | private final Context mContext; 218 | private int mItemHeight = 0; 219 | private int mNumColumns = 0; 220 | private int mActionBarHeight = 0; 221 | private GridView.LayoutParams mImageViewLayoutParams; 222 | 223 | public ImageAdapter(Context context) { 224 | super(); 225 | mContext = context; 226 | mImageViewLayoutParams = new GridView.LayoutParams( 227 | LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); 228 | // Calculate ActionBar height 229 | TypedValue tv = new TypedValue(); 230 | if (context.getTheme().resolveAttribute( 231 | android.R.attr.actionBarSize, tv, true)) { 232 | mActionBarHeight = TypedValue.complexToDimensionPixelSize( 233 | tv.data, context.getResources().getDisplayMetrics()); 234 | } 235 | } 236 | 237 | @Override 238 | public int getCount() { 239 | // If columns have yet to be determined, return no items 240 | if (getNumColumns() == 0) { 241 | return 0; 242 | } 243 | 244 | // Size + number of columns for top empty row 245 | return Images.imageThumbUrls.length + mNumColumns; 246 | } 247 | 248 | @Override 249 | public Object getItem(int position) { 250 | return position < mNumColumns ? 251 | null : Images.imageThumbUrls[position - mNumColumns]; 252 | } 253 | 254 | @Override 255 | public long getItemId(int position) { 256 | return position < mNumColumns ? 0 : position - mNumColumns; 257 | } 258 | 259 | @Override 260 | public int getViewTypeCount() { 261 | // Two types of views, the normal ImageView and the top row of empty views 262 | return 2; 263 | } 264 | 265 | @Override 266 | public int getItemViewType(int position) { 267 | return (position < mNumColumns) ? 1 : 0; 268 | } 269 | 270 | @Override 271 | public boolean hasStableIds() { 272 | return true; 273 | } 274 | 275 | @Override 276 | public View getView(int position, View convertView, ViewGroup container) { 277 | //BEGIN_INCLUDE(load_gridview_item) 278 | // First check if this is the top row 279 | if (position < mNumColumns) { 280 | if (convertView == null) { 281 | convertView = new View(mContext); 282 | } 283 | // Set empty view with height of ActionBar 284 | convertView.setLayoutParams(new AbsListView.LayoutParams( 285 | LayoutParams.MATCH_PARENT, mActionBarHeight)); 286 | return convertView; 287 | } 288 | 289 | // Now handle the main ImageView thumbnails 290 | ImageView imageView; 291 | if (convertView == null) { // if it's not recycled, instantiate and initialize 292 | imageView = new RecyclingImageView(mContext); 293 | imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); 294 | imageView.setLayoutParams(mImageViewLayoutParams); 295 | } else { // Otherwise re-use the converted view 296 | imageView = (ImageView) convertView; 297 | } 298 | 299 | // Check the height matches our calculated column width 300 | if (imageView.getLayoutParams().height != mItemHeight) { 301 | imageView.setLayoutParams(mImageViewLayoutParams); 302 | } 303 | 304 | // Finally load the image asynchronously into the ImageView, this also takes care of 305 | // setting a placeholder image while the background thread runs 306 | mImageFetcher.loadImage(Images.imageThumbUrls[position - mNumColumns], imageView); 307 | return imageView; 308 | //END_INCLUDE(load_gridview_item) 309 | } 310 | 311 | /** 312 | * Sets the item height. Useful for when we know the column width so the height can be set 313 | * to match. 314 | * 315 | * @param height 316 | */ 317 | public void setItemHeight(int height) { 318 | if (height == mItemHeight) { 319 | return; 320 | } 321 | mItemHeight = height; 322 | mImageViewLayoutParams = 323 | new GridView.LayoutParams(LayoutParams.MATCH_PARENT, mItemHeight); 324 | mImageFetcher.setImageSize(height); 325 | notifyDataSetChanged(); 326 | } 327 | 328 | public void setNumColumns(int numColumns) { 329 | mNumColumns = numColumns; 330 | } 331 | 332 | public int getNumColumns() { 333 | return mNumColumns; 334 | } 335 | } 336 | } 337 | -------------------------------------------------------------------------------- /Application/src/main/java/com/example/android/displayingbitmaps/ui/RecyclingImageView.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2013 The Android Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.example.android.displayingbitmaps.ui; 18 | 19 | import android.content.Context; 20 | import android.graphics.drawable.Drawable; 21 | import android.graphics.drawable.LayerDrawable; 22 | import android.util.AttributeSet; 23 | import android.widget.ImageView; 24 | 25 | import com.example.android.displayingbitmaps.util.RecyclingBitmapDrawable; 26 | 27 | /** 28 | * Sub-class of ImageView which automatically notifies the drawable when it is 29 | * being displayed. 30 | */ 31 | public class RecyclingImageView extends ImageView { 32 | 33 | public RecyclingImageView(Context context) { 34 | super(context); 35 | } 36 | 37 | public RecyclingImageView(Context context, AttributeSet attrs) { 38 | super(context, attrs); 39 | } 40 | 41 | /** 42 | * @see android.widget.ImageView#onDetachedFromWindow() 43 | */ 44 | @Override 45 | protected void onDetachedFromWindow() { 46 | // This has been detached from Window, so clear the drawable 47 | setImageDrawable(null); 48 | 49 | super.onDetachedFromWindow(); 50 | } 51 | 52 | /** 53 | * @see android.widget.ImageView#setImageDrawable(android.graphics.drawable.Drawable) 54 | */ 55 | @Override 56 | public void setImageDrawable(Drawable drawable) { 57 | // Keep hold of previous Drawable 58 | final Drawable previousDrawable = getDrawable(); 59 | 60 | // Call super to set new Drawable 61 | super.setImageDrawable(drawable); 62 | 63 | // Notify new Drawable that it is being displayed 64 | notifyDrawable(drawable, true); 65 | 66 | // Notify old Drawable so it is no longer being displayed 67 | notifyDrawable(previousDrawable, false); 68 | } 69 | 70 | /** 71 | * Notifies the drawable that it's displayed state has changed. 72 | * 73 | * @param drawable 74 | * @param isDisplayed 75 | */ 76 | private static void notifyDrawable(Drawable drawable, final boolean isDisplayed) { 77 | if (drawable instanceof RecyclingBitmapDrawable) { 78 | // The drawable is a CountingBitmapDrawable, so notify it 79 | ((RecyclingBitmapDrawable) drawable).setIsDisplayed(isDisplayed); 80 | } else if (drawable instanceof LayerDrawable) { 81 | // The drawable is a LayerDrawable, so recurse on each layer 82 | LayerDrawable layerDrawable = (LayerDrawable) drawable; 83 | for (int i = 0, z = layerDrawable.getNumberOfLayers(); i < z; i++) { 84 | notifyDrawable(layerDrawable.getDrawable(i), isDisplayed); 85 | } 86 | } 87 | } 88 | 89 | } 90 | -------------------------------------------------------------------------------- /Application/src/main/java/com/example/android/displayingbitmaps/util/ImageFetcher.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2012 The Android Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.example.android.displayingbitmaps.util; 18 | 19 | import android.content.Context; 20 | import android.graphics.Bitmap; 21 | import android.net.ConnectivityManager; 22 | import android.net.NetworkInfo; 23 | import android.os.Build; 24 | import android.widget.Toast; 25 | 26 | import com.example.android.common.logger.Log; 27 | import com.example.android.displayingbitmaps.BuildConfig; 28 | import com.example.android.displayingbitmaps.R; 29 | 30 | import java.io.BufferedInputStream; 31 | import java.io.BufferedOutputStream; 32 | import java.io.File; 33 | import java.io.FileDescriptor; 34 | import java.io.FileInputStream; 35 | import java.io.IOException; 36 | import java.io.OutputStream; 37 | import java.net.HttpURLConnection; 38 | import java.net.URL; 39 | 40 | /** 41 | * A simple subclass of {@link ImageResizer} that fetches and resizes images fetched from a URL. 42 | */ 43 | public class ImageFetcher extends ImageResizer { 44 | private static final String TAG = "ImageFetcher"; 45 | private static final int HTTP_CACHE_SIZE = 10 * 1024 * 1024; // 10MB 46 | private static final String HTTP_CACHE_DIR = "http"; 47 | private static final int IO_BUFFER_SIZE = 8 * 1024; 48 | 49 | private DiskLruCache mHttpDiskCache; 50 | private File mHttpCacheDir; 51 | private boolean mHttpDiskCacheStarting = true; 52 | private final Object mHttpDiskCacheLock = new Object(); 53 | private static final int DISK_CACHE_INDEX = 0; 54 | 55 | /** 56 | * Initialize providing a target image width and height for the processing images. 57 | * 58 | * @param context 59 | * @param imageWidth 60 | * @param imageHeight 61 | */ 62 | public ImageFetcher(Context context, int imageWidth, int imageHeight) { 63 | super(context, imageWidth, imageHeight); 64 | init(context); 65 | } 66 | 67 | /** 68 | * Initialize providing a single target image size (used for both width and height); 69 | * 70 | * @param context 71 | * @param imageSize 72 | */ 73 | public ImageFetcher(Context context, int imageSize) { 74 | super(context, imageSize); 75 | init(context); 76 | } 77 | 78 | private void init(Context context) { 79 | checkConnection(context); 80 | mHttpCacheDir = ImageCache.getDiskCacheDir(context, HTTP_CACHE_DIR); 81 | } 82 | 83 | @Override 84 | protected void initDiskCacheInternal() { 85 | super.initDiskCacheInternal(); 86 | initHttpDiskCache(); 87 | } 88 | 89 | private void initHttpDiskCache() { 90 | if (!mHttpCacheDir.exists()) { 91 | mHttpCacheDir.mkdirs(); 92 | } 93 | synchronized (mHttpDiskCacheLock) { 94 | if (ImageCache.getUsableSpace(mHttpCacheDir) > HTTP_CACHE_SIZE) { 95 | try { 96 | mHttpDiskCache = DiskLruCache.open(mHttpCacheDir, 1, 1, HTTP_CACHE_SIZE); 97 | if (BuildConfig.DEBUG) { 98 | Log.d(TAG, "HTTP cache initialized"); 99 | } 100 | } catch (IOException e) { 101 | mHttpDiskCache = null; 102 | } 103 | } 104 | mHttpDiskCacheStarting = false; 105 | mHttpDiskCacheLock.notifyAll(); 106 | } 107 | } 108 | 109 | @Override 110 | protected void clearCacheInternal() { 111 | super.clearCacheInternal(); 112 | synchronized (mHttpDiskCacheLock) { 113 | if (mHttpDiskCache != null && !mHttpDiskCache.isClosed()) { 114 | try { 115 | mHttpDiskCache.delete(); 116 | if (BuildConfig.DEBUG) { 117 | Log.d(TAG, "HTTP cache cleared"); 118 | } 119 | } catch (IOException e) { 120 | Log.e(TAG, "clearCacheInternal - " + e); 121 | } 122 | mHttpDiskCache = null; 123 | mHttpDiskCacheStarting = true; 124 | initHttpDiskCache(); 125 | } 126 | } 127 | } 128 | 129 | @Override 130 | protected void flushCacheInternal() { 131 | super.flushCacheInternal(); 132 | synchronized (mHttpDiskCacheLock) { 133 | if (mHttpDiskCache != null) { 134 | try { 135 | mHttpDiskCache.flush(); 136 | if (BuildConfig.DEBUG) { 137 | Log.d(TAG, "HTTP cache flushed"); 138 | } 139 | } catch (IOException e) { 140 | Log.e(TAG, "flush - " + e); 141 | } 142 | } 143 | } 144 | } 145 | 146 | @Override 147 | protected void closeCacheInternal() { 148 | super.closeCacheInternal(); 149 | synchronized (mHttpDiskCacheLock) { 150 | if (mHttpDiskCache != null) { 151 | try { 152 | if (!mHttpDiskCache.isClosed()) { 153 | mHttpDiskCache.close(); 154 | mHttpDiskCache = null; 155 | if (BuildConfig.DEBUG) { 156 | Log.d(TAG, "HTTP cache closed"); 157 | } 158 | } 159 | } catch (IOException e) { 160 | Log.e(TAG, "closeCacheInternal - " + e); 161 | } 162 | } 163 | } 164 | } 165 | 166 | /** 167 | * Simple network connection check. 168 | * 169 | * @param context 170 | */ 171 | private void checkConnection(Context context) { 172 | final ConnectivityManager cm = 173 | (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); 174 | final NetworkInfo networkInfo = cm.getActiveNetworkInfo(); 175 | if (networkInfo == null || !networkInfo.isConnectedOrConnecting()) { 176 | Toast.makeText(context, R.string.no_network_connection_toast, Toast.LENGTH_LONG).show(); 177 | Log.e(TAG, "checkConnection - no connection found"); 178 | } 179 | } 180 | 181 | /** 182 | * The main process method, which will be called by the ImageWorker in the AsyncTask background 183 | * thread. 184 | * 185 | * @param data The data to load the bitmap, in this case, a regular http URL 186 | * @return The downloaded and resized bitmap 187 | */ 188 | private Bitmap processBitmap(String data) { 189 | if (BuildConfig.DEBUG) { 190 | Log.d(TAG, "processBitmap - " + data); 191 | } 192 | 193 | final String key = ImageCache.hashKeyForDisk(data); 194 | FileDescriptor fileDescriptor = null; 195 | FileInputStream fileInputStream = null; 196 | DiskLruCache.Snapshot snapshot; 197 | synchronized (mHttpDiskCacheLock) { 198 | // Wait for disk cache to initialize 199 | while (mHttpDiskCacheStarting) { 200 | try { 201 | mHttpDiskCacheLock.wait(); 202 | } catch (InterruptedException e) {} 203 | } 204 | 205 | if (mHttpDiskCache != null) { 206 | try { 207 | snapshot = mHttpDiskCache.get(key); 208 | if (snapshot == null) { 209 | if (BuildConfig.DEBUG) { 210 | Log.d(TAG, "processBitmap, not found in http cache, downloading..."); 211 | } 212 | DiskLruCache.Editor editor = mHttpDiskCache.edit(key); 213 | if (editor != null) { 214 | if (downloadUrlToStream(data, 215 | editor.newOutputStream(DISK_CACHE_INDEX))) { 216 | editor.commit(); 217 | } else { 218 | editor.abort(); 219 | } 220 | } 221 | snapshot = mHttpDiskCache.get(key); 222 | } 223 | if (snapshot != null) { 224 | fileInputStream = 225 | (FileInputStream) snapshot.getInputStream(DISK_CACHE_INDEX); 226 | fileDescriptor = fileInputStream.getFD(); 227 | } 228 | } catch (IOException e) { 229 | Log.e(TAG, "processBitmap - " + e); 230 | } catch (IllegalStateException e) { 231 | Log.e(TAG, "processBitmap - " + e); 232 | } finally { 233 | if (fileDescriptor == null && fileInputStream != null) { 234 | try { 235 | fileInputStream.close(); 236 | } catch (IOException e) {} 237 | } 238 | } 239 | } 240 | } 241 | 242 | Bitmap bitmap = null; 243 | if (fileDescriptor != null) { 244 | bitmap = decodeSampledBitmapFromDescriptor(fileDescriptor, mImageWidth, 245 | mImageHeight, getImageCache()); 246 | } 247 | if (fileInputStream != null) { 248 | try { 249 | fileInputStream.close(); 250 | } catch (IOException e) {} 251 | } 252 | return bitmap; 253 | } 254 | 255 | @Override 256 | protected Bitmap processBitmap(Object data) { 257 | return processBitmap(String.valueOf(data)); 258 | } 259 | 260 | /** 261 | * Download a bitmap from a URL and write the content to an output stream. 262 | * 263 | * @param urlString The URL to fetch 264 | * @return true if successful, false otherwise 265 | */ 266 | public boolean downloadUrlToStream(String urlString, OutputStream outputStream) { 267 | disableConnectionReuseIfNecessary(); 268 | HttpURLConnection urlConnection = null; 269 | BufferedOutputStream out = null; 270 | BufferedInputStream in = null; 271 | 272 | try { 273 | final URL url = new URL(urlString); 274 | urlConnection = (HttpURLConnection) url.openConnection(); 275 | in = new BufferedInputStream(urlConnection.getInputStream(), IO_BUFFER_SIZE); 276 | out = new BufferedOutputStream(outputStream, IO_BUFFER_SIZE); 277 | 278 | int b; 279 | while ((b = in.read()) != -1) { 280 | out.write(b); 281 | } 282 | return true; 283 | } catch (final IOException e) { 284 | Log.e(TAG, "Error in downloadBitmap - " + e); 285 | } finally { 286 | if (urlConnection != null) { 287 | urlConnection.disconnect(); 288 | } 289 | try { 290 | if (out != null) { 291 | out.close(); 292 | } 293 | if (in != null) { 294 | in.close(); 295 | } 296 | } catch (final IOException e) {} 297 | } 298 | return false; 299 | } 300 | 301 | /** 302 | * Workaround for bug pre-Froyo, see here for more info: 303 | * http://android-developers.blogspot.com/2011/09/androids-http-clients.html 304 | */ 305 | public static void disableConnectionReuseIfNecessary() { 306 | // HTTP connection reuse which was buggy pre-froyo 307 | if (Build.VERSION.SDK_INT < Build.VERSION_CODES.FROYO) { 308 | System.setProperty("http.keepAlive", "false"); 309 | } 310 | } 311 | } 312 | -------------------------------------------------------------------------------- /Application/src/main/java/com/example/android/displayingbitmaps/util/ImageResizer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2012 The Android Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.example.android.displayingbitmaps.util; 18 | 19 | import android.annotation.TargetApi; 20 | import android.content.Context; 21 | import android.content.res.Resources; 22 | import android.graphics.Bitmap; 23 | import android.graphics.BitmapFactory; 24 | import android.os.Build; 25 | 26 | import com.example.android.common.logger.Log; 27 | import com.example.android.displayingbitmaps.BuildConfig; 28 | 29 | import java.io.FileDescriptor; 30 | 31 | /** 32 | * A simple subclass of {@link ImageWorker} that resizes images from resources given a target width 33 | * and height. Useful for when the input images might be too large to simply load directly into 34 | * memory. 35 | */ 36 | public class ImageResizer extends ImageWorker { 37 | private static final String TAG = "ImageResizer"; 38 | protected int mImageWidth; 39 | protected int mImageHeight; 40 | 41 | /** 42 | * Initialize providing a single target image size (used for both width and height); 43 | * 44 | * @param context 45 | * @param imageWidth 46 | * @param imageHeight 47 | */ 48 | public ImageResizer(Context context, int imageWidth, int imageHeight) { 49 | super(context); 50 | setImageSize(imageWidth, imageHeight); 51 | } 52 | 53 | /** 54 | * Initialize providing a single target image size (used for both width and height); 55 | * 56 | * @param context 57 | * @param imageSize 58 | */ 59 | public ImageResizer(Context context, int imageSize) { 60 | super(context); 61 | setImageSize(imageSize); 62 | } 63 | 64 | /** 65 | * Set the target image width and height. 66 | * 67 | * @param width 68 | * @param height 69 | */ 70 | public void setImageSize(int width, int height) { 71 | mImageWidth = width; 72 | mImageHeight = height; 73 | } 74 | 75 | /** 76 | * Set the target image size (width and height will be the same). 77 | * 78 | * @param size 79 | */ 80 | public void setImageSize(int size) { 81 | setImageSize(size, size); 82 | } 83 | 84 | /** 85 | * The main processing method. This happens in a background task. In this case we are just 86 | * sampling down the bitmap and returning it from a resource. 87 | * 88 | * @param resId 89 | * @return 90 | */ 91 | private Bitmap processBitmap(int resId) { 92 | if (BuildConfig.DEBUG) { 93 | Log.d(TAG, "processBitmap - " + resId); 94 | } 95 | return decodeSampledBitmapFromResource(mResources, resId, mImageWidth, 96 | mImageHeight, getImageCache()); 97 | } 98 | 99 | @Override 100 | protected Bitmap processBitmap(Object data) { 101 | return processBitmap(Integer.parseInt(String.valueOf(data))); 102 | } 103 | 104 | /** 105 | * Decode and sample down a bitmap from resources to the requested width and height. 106 | * 107 | * @param res The resources object containing the image data 108 | * @param resId The resource id of the image data 109 | * @param reqWidth The requested width of the resulting bitmap 110 | * @param reqHeight The requested height of the resulting bitmap 111 | * @param cache The ImageCache used to find candidate bitmaps for use with inBitmap 112 | * @return A bitmap sampled down from the original with the same aspect ratio and dimensions 113 | * that are equal to or greater than the requested width and height 114 | */ 115 | public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId, 116 | int reqWidth, int reqHeight, ImageCache cache) { 117 | 118 | // BEGIN_INCLUDE (read_bitmap_dimensions) 119 | // First decode with inJustDecodeBounds=true to check dimensions 120 | final BitmapFactory.Options options = new BitmapFactory.Options(); 121 | options.inJustDecodeBounds = true; 122 | BitmapFactory.decodeResource(res, resId, options); 123 | 124 | // Calculate inSampleSize 125 | options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); 126 | // END_INCLUDE (read_bitmap_dimensions) 127 | 128 | // If we're running on Honeycomb or newer, try to use inBitmap 129 | if (Utils.hasHoneycomb()) { 130 | addInBitmapOptions(options, cache); 131 | } 132 | 133 | // Decode bitmap with inSampleSize set 134 | options.inJustDecodeBounds = false; 135 | return BitmapFactory.decodeResource(res, resId, options); 136 | } 137 | 138 | /** 139 | * Decode and sample down a bitmap from a file to the requested width and height. 140 | * 141 | * @param filename The full path of the file to decode 142 | * @param reqWidth The requested width of the resulting bitmap 143 | * @param reqHeight The requested height of the resulting bitmap 144 | * @param cache The ImageCache used to find candidate bitmaps for use with inBitmap 145 | * @return A bitmap sampled down from the original with the same aspect ratio and dimensions 146 | * that are equal to or greater than the requested width and height 147 | */ 148 | public static Bitmap decodeSampledBitmapFromFile(String filename, 149 | int reqWidth, int reqHeight, ImageCache cache) { 150 | 151 | // First decode with inJustDecodeBounds=true to check dimensions 152 | final BitmapFactory.Options options = new BitmapFactory.Options(); 153 | options.inJustDecodeBounds = true; 154 | BitmapFactory.decodeFile(filename, options); 155 | 156 | // Calculate inSampleSize 157 | options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); 158 | 159 | // If we're running on Honeycomb or newer, try to use inBitmap 160 | if (Utils.hasHoneycomb()) { 161 | addInBitmapOptions(options, cache); 162 | } 163 | 164 | // Decode bitmap with inSampleSize set 165 | options.inJustDecodeBounds = false; 166 | return BitmapFactory.decodeFile(filename, options); 167 | } 168 | 169 | /** 170 | * Decode and sample down a bitmap from a file input stream to the requested width and height. 171 | * 172 | * @param fileDescriptor The file descriptor to read from 173 | * @param reqWidth The requested width of the resulting bitmap 174 | * @param reqHeight The requested height of the resulting bitmap 175 | * @param cache The ImageCache used to find candidate bitmaps for use with inBitmap 176 | * @return A bitmap sampled down from the original with the same aspect ratio and dimensions 177 | * that are equal to or greater than the requested width and height 178 | */ 179 | public static Bitmap decodeSampledBitmapFromDescriptor( 180 | FileDescriptor fileDescriptor, int reqWidth, int reqHeight, ImageCache cache) { 181 | 182 | // First decode with inJustDecodeBounds=true to check dimensions 183 | final BitmapFactory.Options options = new BitmapFactory.Options(); 184 | options.inJustDecodeBounds = true; 185 | BitmapFactory.decodeFileDescriptor(fileDescriptor, null, options); 186 | 187 | // Calculate inSampleSize 188 | options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); 189 | 190 | // Decode bitmap with inSampleSize set 191 | options.inJustDecodeBounds = false; 192 | 193 | // If we're running on Honeycomb or newer, try to use inBitmap 194 | if (Utils.hasHoneycomb()) { 195 | addInBitmapOptions(options, cache); 196 | } 197 | 198 | return BitmapFactory.decodeFileDescriptor(fileDescriptor, null, options); 199 | } 200 | 201 | @TargetApi(Build.VERSION_CODES.HONEYCOMB) 202 | private static void addInBitmapOptions(BitmapFactory.Options options, ImageCache cache) { 203 | //BEGIN_INCLUDE(add_bitmap_options) 204 | // inBitmap only works with mutable bitmaps so force the decoder to 205 | // return mutable bitmaps. 206 | options.inMutable = true; 207 | 208 | if (cache != null) { 209 | // Try and find a bitmap to use for inBitmap 210 | Bitmap inBitmap = cache.getBitmapFromReusableSet(options); 211 | 212 | if (inBitmap != null) { 213 | options.inBitmap = inBitmap; 214 | } 215 | } 216 | //END_INCLUDE(add_bitmap_options) 217 | } 218 | 219 | /** 220 | * Calculate an inSampleSize for use in a {@link android.graphics.BitmapFactory.Options} object when decoding 221 | * bitmaps using the decode* methods from {@link android.graphics.BitmapFactory}. This implementation calculates 222 | * the closest inSampleSize that is a power of 2 and will result in the final decoded bitmap 223 | * having a width and height equal to or larger than the requested width and height. 224 | * 225 | * @param options An options object with out* params already populated (run through a decode* 226 | * method with inJustDecodeBounds==true 227 | * @param reqWidth The requested width of the resulting bitmap 228 | * @param reqHeight The requested height of the resulting bitmap 229 | * @return The value to be used for inSampleSize 230 | */ 231 | public static int calculateInSampleSize(BitmapFactory.Options options, 232 | int reqWidth, int reqHeight) { 233 | // BEGIN_INCLUDE (calculate_sample_size) 234 | // Raw height and width of image 235 | final int height = options.outHeight; 236 | final int width = options.outWidth; 237 | int inSampleSize = 1; 238 | 239 | if (height > reqHeight || width > reqWidth) { 240 | 241 | final int halfHeight = height / 2; 242 | final int halfWidth = width / 2; 243 | 244 | // Calculate the largest inSampleSize value that is a power of 2 and keeps both 245 | // height and width larger than the requested height and width. 246 | while ((halfHeight / inSampleSize) > reqHeight 247 | && (halfWidth / inSampleSize) > reqWidth) { 248 | inSampleSize *= 2; 249 | } 250 | 251 | // This offers some additional logic in case the image has a strange 252 | // aspect ratio. For example, a panorama may have a much larger 253 | // width than height. In these cases the total pixels might still 254 | // end up being too large to fit comfortably in memory, so we should 255 | // be more aggressive with sample down the image (=larger inSampleSize). 256 | 257 | long totalPixels = width * height / inSampleSize; 258 | 259 | // Anything more than 2x the requested pixels we'll sample down further 260 | final long totalReqPixelsCap = reqWidth * reqHeight * 2; 261 | 262 | while (totalPixels > totalReqPixelsCap) { 263 | inSampleSize *= 2; 264 | totalPixels /= 2; 265 | } 266 | } 267 | return inSampleSize; 268 | // END_INCLUDE (calculate_sample_size) 269 | } 270 | } 271 | -------------------------------------------------------------------------------- /Application/src/main/java/com/example/android/displayingbitmaps/util/ImageWorker.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2012 The Android Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.example.android.displayingbitmaps.util; 18 | 19 | import android.content.Context; 20 | import android.content.res.Resources; 21 | import android.graphics.Bitmap; 22 | import android.graphics.BitmapFactory; 23 | import android.graphics.drawable.BitmapDrawable; 24 | import android.graphics.drawable.ColorDrawable; 25 | import android.graphics.drawable.Drawable; 26 | import android.graphics.drawable.TransitionDrawable; 27 | import android.support.v4.app.FragmentActivity; 28 | import android.support.v4.app.FragmentManager; 29 | import android.widget.ImageView; 30 | 31 | import com.example.android.common.logger.Log; 32 | import com.example.android.displayingbitmaps.BuildConfig; 33 | 34 | import java.lang.ref.WeakReference; 35 | 36 | /** 37 | * This class wraps up completing some arbitrary long running work when loading a bitmap to an 38 | * ImageView. It handles things like using a memory and disk cache, running the work in a background 39 | * thread and setting a placeholder image. 40 | */ 41 | public abstract class ImageWorker { 42 | private static final String TAG = "ImageWorker"; 43 | private static final int FADE_IN_TIME = 200; 44 | 45 | private ImageCache mImageCache; 46 | private ImageCache.ImageCacheParams mImageCacheParams; 47 | private Bitmap mLoadingBitmap; 48 | private boolean mFadeInBitmap = true; 49 | private boolean mExitTasksEarly = false; 50 | protected boolean mPauseWork = false; 51 | private final Object mPauseWorkLock = new Object(); 52 | 53 | protected Resources mResources; 54 | 55 | private static final int MESSAGE_CLEAR = 0; 56 | private static final int MESSAGE_INIT_DISK_CACHE = 1; 57 | private static final int MESSAGE_FLUSH = 2; 58 | private static final int MESSAGE_CLOSE = 3; 59 | 60 | protected ImageWorker(Context context) { 61 | mResources = context.getResources(); 62 | } 63 | 64 | /** 65 | * Load an image specified by the data parameter into an ImageView (override 66 | * {@link ImageWorker#processBitmap(Object)} to define the processing logic). A memory and 67 | * disk cache will be used if an {@link ImageCache} has been added using 68 | * {@link ImageWorker#addImageCache(android.support.v4.app.FragmentManager, ImageCache.ImageCacheParams)}. If the 69 | * image is found in the memory cache, it is set immediately, otherwise an {@link AsyncTask} 70 | * will be created to asynchronously load the bitmap. 71 | * 72 | * @param data The URL of the image to download. 73 | * @param imageView The ImageView to bind the downloaded image to. 74 | * @param listener A listener that will be called back once the image has been loaded. 75 | */ 76 | public void loadImage(Object data, ImageView imageView, OnImageLoadedListener listener) { 77 | if (data == null) { 78 | return; 79 | } 80 | 81 | BitmapDrawable value = null; 82 | 83 | if (mImageCache != null) { 84 | value = mImageCache.getBitmapFromMemCache(String.valueOf(data)); 85 | } 86 | 87 | if (value != null) { 88 | // Bitmap found in memory cache 89 | imageView.setImageDrawable(value); 90 | if (listener != null) { 91 | listener.onImageLoaded(true); 92 | } 93 | } else if (cancelPotentialWork(data, imageView)) { 94 | //BEGIN_INCLUDE(execute_background_task) 95 | final BitmapWorkerTask task = new BitmapWorkerTask(data, imageView, listener); 96 | final AsyncDrawable asyncDrawable = 97 | new AsyncDrawable(mResources, mLoadingBitmap, task); 98 | imageView.setImageDrawable(asyncDrawable); 99 | 100 | // NOTE: This uses a custom version of AsyncTask that has been pulled from the 101 | // framework and slightly modified. Refer to the docs at the top of the class 102 | // for more info on what was changed. 103 | task.executeOnExecutor(AsyncTask.DUAL_THREAD_EXECUTOR); 104 | //END_INCLUDE(execute_background_task) 105 | } 106 | } 107 | 108 | /** 109 | * Load an image specified by the data parameter into an ImageView (override 110 | * {@link ImageWorker#processBitmap(Object)} to define the processing logic). A memory and 111 | * disk cache will be used if an {@link ImageCache} has been added using 112 | * {@link ImageWorker#addImageCache(android.support.v4.app.FragmentManager, ImageCache.ImageCacheParams)}. If the 113 | * image is found in the memory cache, it is set immediately, otherwise an {@link AsyncTask} 114 | * will be created to asynchronously load the bitmap. 115 | * 116 | * @param data The URL of the image to download. 117 | * @param imageView The ImageView to bind the downloaded image to. 118 | */ 119 | public void loadImage(Object data, ImageView imageView) { 120 | loadImage(data, imageView, null); 121 | } 122 | 123 | /** 124 | * Set placeholder bitmap that shows when the the background thread is running. 125 | * 126 | * @param bitmap 127 | */ 128 | public void setLoadingImage(Bitmap bitmap) { 129 | mLoadingBitmap = bitmap; 130 | } 131 | 132 | /** 133 | * Set placeholder bitmap that shows when the the background thread is running. 134 | * 135 | * @param resId 136 | */ 137 | public void setLoadingImage(int resId) { 138 | mLoadingBitmap = BitmapFactory.decodeResource(mResources, resId); 139 | } 140 | 141 | /** 142 | * Adds an {@link ImageCache} to this {@link ImageWorker} to handle disk and memory bitmap 143 | * caching. 144 | * @param fragmentManager 145 | * @param cacheParams The cache parameters to use for the image cache. 146 | */ 147 | public void addImageCache(FragmentManager fragmentManager, 148 | ImageCache.ImageCacheParams cacheParams) { 149 | mImageCacheParams = cacheParams; 150 | mImageCache = ImageCache.getInstance(fragmentManager, mImageCacheParams); 151 | new CacheAsyncTask().execute(MESSAGE_INIT_DISK_CACHE); 152 | } 153 | 154 | /** 155 | * Adds an {@link ImageCache} to this {@link ImageWorker} to handle disk and memory bitmap 156 | * caching. 157 | * @param activity 158 | * @param diskCacheDirectoryName See 159 | * {@link ImageCache.ImageCacheParams#ImageCacheParams(android.content.Context, String)}. 160 | */ 161 | public void addImageCache(FragmentActivity activity, String diskCacheDirectoryName) { 162 | mImageCacheParams = new ImageCache.ImageCacheParams(activity, diskCacheDirectoryName); 163 | mImageCache = ImageCache.getInstance(activity.getSupportFragmentManager(), mImageCacheParams); 164 | new CacheAsyncTask().execute(MESSAGE_INIT_DISK_CACHE); 165 | } 166 | 167 | /** 168 | * If set to true, the image will fade-in once it has been loaded by the background thread. 169 | */ 170 | public void setImageFadeIn(boolean fadeIn) { 171 | mFadeInBitmap = fadeIn; 172 | } 173 | 174 | public void setExitTasksEarly(boolean exitTasksEarly) { 175 | mExitTasksEarly = exitTasksEarly; 176 | setPauseWork(false); 177 | } 178 | 179 | /** 180 | * Subclasses should override this to define any processing or work that must happen to produce 181 | * the final bitmap. This will be executed in a background thread and be long running. For 182 | * example, you could resize a large bitmap here, or pull down an image from the network. 183 | * 184 | * @param data The data to identify which image to process, as provided by 185 | * {@link ImageWorker#loadImage(Object, android.widget.ImageView)} 186 | * @return The processed bitmap 187 | */ 188 | protected abstract Bitmap processBitmap(Object data); 189 | 190 | /** 191 | * @return The {@link ImageCache} object currently being used by this ImageWorker. 192 | */ 193 | protected ImageCache getImageCache() { 194 | return mImageCache; 195 | } 196 | 197 | /** 198 | * Cancels any pending work attached to the provided ImageView. 199 | * @param imageView 200 | */ 201 | public static void cancelWork(ImageView imageView) { 202 | final BitmapWorkerTask bitmapWorkerTask = getBitmapWorkerTask(imageView); 203 | if (bitmapWorkerTask != null) { 204 | bitmapWorkerTask.cancel(true); 205 | if (BuildConfig.DEBUG) { 206 | final Object bitmapData = bitmapWorkerTask.mData; 207 | Log.d(TAG, "cancelWork - cancelled work for " + bitmapData); 208 | } 209 | } 210 | } 211 | 212 | /** 213 | * Returns true if the current work has been canceled or if there was no work in 214 | * progress on this image view. 215 | * Returns false if the work in progress deals with the same data. The work is not 216 | * stopped in that case. 217 | */ 218 | public static boolean cancelPotentialWork(Object data, ImageView imageView) { 219 | //BEGIN_INCLUDE(cancel_potential_work) 220 | final BitmapWorkerTask bitmapWorkerTask = getBitmapWorkerTask(imageView); 221 | 222 | if (bitmapWorkerTask != null) { 223 | final Object bitmapData = bitmapWorkerTask.mData; 224 | if (bitmapData == null || !bitmapData.equals(data)) { 225 | bitmapWorkerTask.cancel(true); 226 | if (BuildConfig.DEBUG) { 227 | Log.d(TAG, "cancelPotentialWork - cancelled work for " + data); 228 | } 229 | } else { 230 | // The same work is already in progress. 231 | return false; 232 | } 233 | } 234 | return true; 235 | //END_INCLUDE(cancel_potential_work) 236 | } 237 | 238 | /** 239 | * @param imageView Any imageView 240 | * @return Retrieve the currently active work task (if any) associated with this imageView. 241 | * null if there is no such task. 242 | */ 243 | private static BitmapWorkerTask getBitmapWorkerTask(ImageView imageView) { 244 | if (imageView != null) { 245 | final Drawable drawable = imageView.getDrawable(); 246 | if (drawable instanceof AsyncDrawable) { 247 | final AsyncDrawable asyncDrawable = (AsyncDrawable) drawable; 248 | return asyncDrawable.getBitmapWorkerTask(); 249 | } 250 | } 251 | return null; 252 | } 253 | 254 | /** 255 | * The actual AsyncTask that will asynchronously process the image. 256 | */ 257 | private class BitmapWorkerTask extends AsyncTask { 258 | private Object mData; 259 | private final WeakReference imageViewReference; 260 | private final OnImageLoadedListener mOnImageLoadedListener; 261 | 262 | public BitmapWorkerTask(Object data, ImageView imageView) { 263 | mData = data; 264 | imageViewReference = new WeakReference(imageView); 265 | mOnImageLoadedListener = null; 266 | } 267 | 268 | public BitmapWorkerTask(Object data, ImageView imageView, OnImageLoadedListener listener) { 269 | mData = data; 270 | imageViewReference = new WeakReference(imageView); 271 | mOnImageLoadedListener = listener; 272 | } 273 | 274 | /** 275 | * Background processing. 276 | */ 277 | @Override 278 | protected BitmapDrawable doInBackground(Void... params) { 279 | //BEGIN_INCLUDE(load_bitmap_in_background) 280 | if (BuildConfig.DEBUG) { 281 | Log.d(TAG, "doInBackground - starting work"); 282 | } 283 | 284 | final String dataString = String.valueOf(mData); 285 | Bitmap bitmap = null; 286 | BitmapDrawable drawable = null; 287 | 288 | // Wait here if work is paused and the task is not cancelled 289 | synchronized (mPauseWorkLock) { 290 | while (mPauseWork && !isCancelled()) { 291 | try { 292 | mPauseWorkLock.wait(); 293 | } catch (InterruptedException e) {} 294 | } 295 | } 296 | 297 | // If the image cache is available and this task has not been cancelled by another 298 | // thread and the ImageView that was originally bound to this task is still bound back 299 | // to this task and our "exit early" flag is not set then try and fetch the bitmap from 300 | // the cache 301 | if (mImageCache != null && !isCancelled() && getAttachedImageView() != null 302 | && !mExitTasksEarly) { 303 | bitmap = mImageCache.getBitmapFromDiskCache(dataString); 304 | } 305 | 306 | // If the bitmap was not found in the cache and this task has not been cancelled by 307 | // another thread and the ImageView that was originally bound to this task is still 308 | // bound back to this task and our "exit early" flag is not set, then call the main 309 | // process method (as implemented by a subclass) 310 | if (bitmap == null && !isCancelled() && getAttachedImageView() != null 311 | && !mExitTasksEarly) { 312 | bitmap = processBitmap(mData); 313 | } 314 | 315 | // If the bitmap was processed and the image cache is available, then add the processed 316 | // bitmap to the cache for future use. Note we don't check if the task was cancelled 317 | // here, if it was, and the thread is still running, we may as well add the processed 318 | // bitmap to our cache as it might be used again in the future 319 | if (bitmap != null) { 320 | if (Utils.hasHoneycomb()) { 321 | // Running on Honeycomb or newer, so wrap in a standard BitmapDrawable 322 | drawable = new BitmapDrawable(mResources, bitmap); 323 | } else { 324 | // Running on Gingerbread or older, so wrap in a RecyclingBitmapDrawable 325 | // which will recycle automagically 326 | drawable = new RecyclingBitmapDrawable(mResources, bitmap); 327 | } 328 | 329 | if (mImageCache != null) { 330 | mImageCache.addBitmapToCache(dataString, drawable); 331 | } 332 | } 333 | 334 | if (BuildConfig.DEBUG) { 335 | Log.d(TAG, "doInBackground - finished work"); 336 | } 337 | 338 | return drawable; 339 | //END_INCLUDE(load_bitmap_in_background) 340 | } 341 | 342 | /** 343 | * Once the image is processed, associates it to the imageView 344 | */ 345 | @Override 346 | protected void onPostExecute(BitmapDrawable value) { 347 | //BEGIN_INCLUDE(complete_background_work) 348 | boolean success = false; 349 | // if cancel was called on this task or the "exit early" flag is set then we're done 350 | if (isCancelled() || mExitTasksEarly) { 351 | value = null; 352 | } 353 | 354 | final ImageView imageView = getAttachedImageView(); 355 | if (value != null && imageView != null) { 356 | if (BuildConfig.DEBUG) { 357 | Log.d(TAG, "onPostExecute - setting bitmap"); 358 | } 359 | success = true; 360 | setImageDrawable(imageView, value); 361 | } 362 | if (mOnImageLoadedListener != null) { 363 | mOnImageLoadedListener.onImageLoaded(success); 364 | } 365 | //END_INCLUDE(complete_background_work) 366 | } 367 | 368 | @Override 369 | protected void onCancelled(BitmapDrawable value) { 370 | super.onCancelled(value); 371 | synchronized (mPauseWorkLock) { 372 | mPauseWorkLock.notifyAll(); 373 | } 374 | } 375 | 376 | /** 377 | * Returns the ImageView associated with this task as long as the ImageView's task still 378 | * points to this task as well. Returns null otherwise. 379 | */ 380 | private ImageView getAttachedImageView() { 381 | final ImageView imageView = imageViewReference.get(); 382 | final BitmapWorkerTask bitmapWorkerTask = getBitmapWorkerTask(imageView); 383 | 384 | if (this == bitmapWorkerTask) { 385 | return imageView; 386 | } 387 | 388 | return null; 389 | } 390 | } 391 | 392 | /** 393 | * Interface definition for callback on image loaded successfully. 394 | */ 395 | public interface OnImageLoadedListener { 396 | 397 | /** 398 | * Called once the image has been loaded. 399 | * @param success True if the image was loaded successfully, false if 400 | * there was an error. 401 | */ 402 | void onImageLoaded(boolean success); 403 | } 404 | 405 | /** 406 | * A custom Drawable that will be attached to the imageView while the work is in progress. 407 | * Contains a reference to the actual worker task, so that it can be stopped if a new binding is 408 | * required, and makes sure that only the last started worker process can bind its result, 409 | * independently of the finish order. 410 | */ 411 | private static class AsyncDrawable extends BitmapDrawable { 412 | private final WeakReference bitmapWorkerTaskReference; 413 | 414 | public AsyncDrawable(Resources res, Bitmap bitmap, BitmapWorkerTask bitmapWorkerTask) { 415 | super(res, bitmap); 416 | bitmapWorkerTaskReference = 417 | new WeakReference(bitmapWorkerTask); 418 | } 419 | 420 | public BitmapWorkerTask getBitmapWorkerTask() { 421 | return bitmapWorkerTaskReference.get(); 422 | } 423 | } 424 | 425 | /** 426 | * Called when the processing is complete and the final drawable should be 427 | * set on the ImageView. 428 | * 429 | * @param imageView 430 | * @param drawable 431 | */ 432 | private void setImageDrawable(ImageView imageView, Drawable drawable) { 433 | if (mFadeInBitmap) { 434 | // Transition drawable with a transparent drawable and the final drawable 435 | final TransitionDrawable td = 436 | new TransitionDrawable(new Drawable[] { 437 | new ColorDrawable(android.R.color.transparent), 438 | drawable 439 | }); 440 | // Set background to loading bitmap 441 | imageView.setBackgroundDrawable( 442 | new BitmapDrawable(mResources, mLoadingBitmap)); 443 | 444 | imageView.setImageDrawable(td); 445 | td.startTransition(FADE_IN_TIME); 446 | } else { 447 | imageView.setImageDrawable(drawable); 448 | } 449 | } 450 | 451 | /** 452 | * Pause any ongoing background work. This can be used as a temporary 453 | * measure to improve performance. For example background work could 454 | * be paused when a ListView or GridView is being scrolled using a 455 | * {@link android.widget.AbsListView.OnScrollListener} to keep 456 | * scrolling smooth. 457 | *

458 | * If work is paused, be sure setPauseWork(false) is called again 459 | * before your fragment or activity is destroyed (for example during 460 | * {@link android.app.Activity#onPause()}), or there is a risk the 461 | * background thread will never finish. 462 | */ 463 | public void setPauseWork(boolean pauseWork) { 464 | synchronized (mPauseWorkLock) { 465 | mPauseWork = pauseWork; 466 | if (!mPauseWork) { 467 | mPauseWorkLock.notifyAll(); 468 | } 469 | } 470 | } 471 | 472 | protected class CacheAsyncTask extends AsyncTask { 473 | 474 | @Override 475 | protected Void doInBackground(Object... params) { 476 | switch ((Integer)params[0]) { 477 | case MESSAGE_CLEAR: 478 | clearCacheInternal(); 479 | break; 480 | case MESSAGE_INIT_DISK_CACHE: 481 | initDiskCacheInternal(); 482 | break; 483 | case MESSAGE_FLUSH: 484 | flushCacheInternal(); 485 | break; 486 | case MESSAGE_CLOSE: 487 | closeCacheInternal(); 488 | break; 489 | } 490 | return null; 491 | } 492 | } 493 | 494 | protected void initDiskCacheInternal() { 495 | if (mImageCache != null) { 496 | mImageCache.initDiskCache(); 497 | } 498 | } 499 | 500 | protected void clearCacheInternal() { 501 | if (mImageCache != null) { 502 | mImageCache.clearCache(); 503 | } 504 | } 505 | 506 | protected void flushCacheInternal() { 507 | if (mImageCache != null) { 508 | mImageCache.flush(); 509 | } 510 | } 511 | 512 | protected void closeCacheInternal() { 513 | if (mImageCache != null) { 514 | mImageCache.close(); 515 | mImageCache = null; 516 | } 517 | } 518 | 519 | public void clearCache() { 520 | new CacheAsyncTask().execute(MESSAGE_CLEAR); 521 | } 522 | 523 | public void flushCache() { 524 | new CacheAsyncTask().execute(MESSAGE_FLUSH); 525 | } 526 | 527 | public void closeCache() { 528 | new CacheAsyncTask().execute(MESSAGE_CLOSE); 529 | } 530 | } 531 | -------------------------------------------------------------------------------- /Application/src/main/java/com/example/android/displayingbitmaps/util/RecyclingBitmapDrawable.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2013 The Android Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.example.android.displayingbitmaps.util; 18 | 19 | import android.content.res.Resources; 20 | import android.graphics.Bitmap; 21 | import android.graphics.drawable.BitmapDrawable; 22 | 23 | import com.example.android.common.logger.Log; 24 | import com.example.android.displayingbitmaps.BuildConfig; 25 | 26 | /** 27 | * A BitmapDrawable that keeps track of whether it is being displayed or cached. 28 | * When the drawable is no longer being displayed or cached, 29 | * {@link android.graphics.Bitmap#recycle() recycle()} will be called on this drawable's bitmap. 30 | */ 31 | public class RecyclingBitmapDrawable extends BitmapDrawable { 32 | 33 | static final String TAG = "CountingBitmapDrawable"; 34 | 35 | private int mCacheRefCount = 0; 36 | private int mDisplayRefCount = 0; 37 | 38 | private boolean mHasBeenDisplayed; 39 | 40 | public RecyclingBitmapDrawable(Resources res, Bitmap bitmap) { 41 | super(res, bitmap); 42 | } 43 | 44 | /** 45 | * Notify the drawable that the displayed state has changed. Internally a 46 | * count is kept so that the drawable knows when it is no longer being 47 | * displayed. 48 | * 49 | * @param isDisplayed - Whether the drawable is being displayed or not 50 | */ 51 | public void setIsDisplayed(boolean isDisplayed) { 52 | //BEGIN_INCLUDE(set_is_displayed) 53 | synchronized (this) { 54 | if (isDisplayed) { 55 | mDisplayRefCount++; 56 | mHasBeenDisplayed = true; 57 | } else { 58 | mDisplayRefCount--; 59 | } 60 | } 61 | 62 | // Check to see if recycle() can be called 63 | checkState(); 64 | //END_INCLUDE(set_is_displayed) 65 | } 66 | 67 | /** 68 | * Notify the drawable that the cache state has changed. Internally a count 69 | * is kept so that the drawable knows when it is no longer being cached. 70 | * 71 | * @param isCached - Whether the drawable is being cached or not 72 | */ 73 | public void setIsCached(boolean isCached) { 74 | //BEGIN_INCLUDE(set_is_cached) 75 | synchronized (this) { 76 | if (isCached) { 77 | mCacheRefCount++; 78 | } else { 79 | mCacheRefCount--; 80 | } 81 | } 82 | 83 | // Check to see if recycle() can be called 84 | checkState(); 85 | //END_INCLUDE(set_is_cached) 86 | } 87 | 88 | private synchronized void checkState() { 89 | //BEGIN_INCLUDE(check_state) 90 | // If the drawable cache and display ref counts = 0, and this drawable 91 | // has been displayed, then recycle 92 | if (mCacheRefCount <= 0 && mDisplayRefCount <= 0 && mHasBeenDisplayed 93 | && hasValidBitmap()) { 94 | if (BuildConfig.DEBUG) { 95 | Log.d(TAG, "No longer being used or cached so recycling. " 96 | + toString()); 97 | } 98 | 99 | getBitmap().recycle(); 100 | } 101 | //END_INCLUDE(check_state) 102 | } 103 | 104 | private synchronized boolean hasValidBitmap() { 105 | Bitmap bitmap = getBitmap(); 106 | return bitmap != null && !bitmap.isRecycled(); 107 | } 108 | 109 | } 110 | -------------------------------------------------------------------------------- /Application/src/main/java/com/example/android/displayingbitmaps/util/Utils.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2012 The Android Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.example.android.displayingbitmaps.util; 18 | 19 | import android.annotation.TargetApi; 20 | import android.os.Build; 21 | import android.os.Build.VERSION_CODES; 22 | import android.os.StrictMode; 23 | 24 | import com.example.android.displayingbitmaps.ui.ImageDetailActivity; 25 | import com.example.android.displayingbitmaps.ui.ImageGridActivity; 26 | 27 | /** 28 | * Class containing some static utility methods. 29 | */ 30 | public class Utils { 31 | private Utils() {}; 32 | 33 | 34 | @TargetApi(VERSION_CODES.HONEYCOMB) 35 | public static void enableStrictMode() { 36 | if (Utils.hasGingerbread()) { 37 | StrictMode.ThreadPolicy.Builder threadPolicyBuilder = 38 | new StrictMode.ThreadPolicy.Builder() 39 | .detectAll() 40 | .penaltyLog(); 41 | StrictMode.VmPolicy.Builder vmPolicyBuilder = 42 | new StrictMode.VmPolicy.Builder() 43 | .detectAll() 44 | .penaltyLog(); 45 | 46 | if (Utils.hasHoneycomb()) { 47 | threadPolicyBuilder.penaltyFlashScreen(); 48 | vmPolicyBuilder 49 | .setClassInstanceLimit(ImageGridActivity.class, 1) 50 | .setClassInstanceLimit(ImageDetailActivity.class, 1); 51 | } 52 | StrictMode.setThreadPolicy(threadPolicyBuilder.build()); 53 | StrictMode.setVmPolicy(vmPolicyBuilder.build()); 54 | } 55 | } 56 | 57 | public static boolean hasFroyo() { 58 | // Can use static final constants like FROYO, declared in later versions 59 | // of the OS since they are inlined at compile time. This is guaranteed behavior. 60 | return Build.VERSION.SDK_INT >= VERSION_CODES.FROYO; 61 | } 62 | 63 | public static boolean hasGingerbread() { 64 | return Build.VERSION.SDK_INT >= VERSION_CODES.GINGERBREAD; 65 | } 66 | 67 | public static boolean hasHoneycomb() { 68 | return Build.VERSION.SDK_INT >= VERSION_CODES.HONEYCOMB; 69 | } 70 | 71 | public static boolean hasHoneycombMR1() { 72 | return Build.VERSION.SDK_INT >= VERSION_CODES.HONEYCOMB_MR1; 73 | } 74 | 75 | public static boolean hasJellyBean() { 76 | return Build.VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN; 77 | } 78 | 79 | public static boolean hasKitKat() { 80 | return Build.VERSION.SDK_INT >= VERSION_CODES.KITKAT; 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /Application/src/main/res/drawable-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/googlearchive/android-DisplayingBitmaps/9ab780818d4d7bc9d96bca289f601546eb01ae9b/Application/src/main/res/drawable-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /Application/src/main/res/drawable-hdpi/tile.9.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/googlearchive/android-DisplayingBitmaps/9ab780818d4d7bc9d96bca289f601546eb01ae9b/Application/src/main/res/drawable-hdpi/tile.9.png -------------------------------------------------------------------------------- /Application/src/main/res/drawable-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/googlearchive/android-DisplayingBitmaps/9ab780818d4d7bc9d96bca289f601546eb01ae9b/Application/src/main/res/drawable-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /Application/src/main/res/drawable-nodpi/empty_photo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/googlearchive/android-DisplayingBitmaps/9ab780818d4d7bc9d96bca289f601546eb01ae9b/Application/src/main/res/drawable-nodpi/empty_photo.png -------------------------------------------------------------------------------- /Application/src/main/res/drawable-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/googlearchive/android-DisplayingBitmaps/9ab780818d4d7bc9d96bca289f601546eb01ae9b/Application/src/main/res/drawable-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /Application/src/main/res/drawable-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/googlearchive/android-DisplayingBitmaps/9ab780818d4d7bc9d96bca289f601546eb01ae9b/Application/src/main/res/drawable-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /Application/src/main/res/drawable/photogrid_list_selector.xml: -------------------------------------------------------------------------------- 1 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | -------------------------------------------------------------------------------- /Application/src/main/res/layout/image_detail_fragment.xml: -------------------------------------------------------------------------------- 1 | 2 | 17 | 18 | 21 | 22 | 28 | 29 | 34 | 35 | -------------------------------------------------------------------------------- /Application/src/main/res/layout/image_detail_pager.xml: -------------------------------------------------------------------------------- 1 | 2 | 17 | 18 | 22 | 23 | -------------------------------------------------------------------------------- /Application/src/main/res/layout/image_grid_fragment.xml: -------------------------------------------------------------------------------- 1 | 2 | 17 | 18 | 28 | 29 | -------------------------------------------------------------------------------- /Application/src/main/res/menu/main_menu.xml: -------------------------------------------------------------------------------- 1 | 2 | 17 |

18 | 19 | 24 | 25 | -------------------------------------------------------------------------------- /Application/src/main/res/values-large/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 17 | 18 | 19 | 20 | 148dp 21 | 2dp 22 | 23 | -------------------------------------------------------------------------------- /Application/src/main/res/values-sw600dp/template-dimens.xml: -------------------------------------------------------------------------------- 1 | 16 | 17 | 18 | 19 | 20 | 21 | @dimen/margin_huge 22 | @dimen/margin_medium 23 | 24 | 25 | -------------------------------------------------------------------------------- /Application/src/main/res/values-sw600dp/template-styles.xml: -------------------------------------------------------------------------------- 1 | 16 | 17 | 18 | 19 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /Application/src/main/res/values-v11/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 17 | 18 | 19 | 20 | 25 | 26 | 31 | 32 | 33 | 34 | 35 | 36 | -------------------------------------------------------------------------------- /Application/src/main/res/values-v11/template-styles.xml: -------------------------------------------------------------------------------- 1 | 16 | 17 | 18 | 19 | 20 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /Application/src/main/res/values-xlarge/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 17 | 18 | 19 | 20 | 198dp 21 | 2dp 22 | 23 | -------------------------------------------------------------------------------- /Application/src/main/res/values/base-strings.xml: -------------------------------------------------------------------------------- 1 | 2 | 17 | 18 | 19 | DisplayingBitmaps 20 | 21 | 34 | 35 | 36 | -------------------------------------------------------------------------------- /Application/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 17 | 18 | 19 | 20 | #1Affffff 21 | #80000000 22 | 23 | 24 | -------------------------------------------------------------------------------- /Application/src/main/res/values/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 17 | 18 | 19 | 20 | 100dp 21 | 1dp 22 | 23 | -------------------------------------------------------------------------------- /Application/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | 17 | 18 | 19 | 20 | Clear Caches 21 | Caches have been cleared 22 | Image Thumbnail 23 | No network connection found 24 | 25 | -------------------------------------------------------------------------------- /Application/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 17 | 18 | 19 | 20 | 28 | 29 | -------------------------------------------------------------------------------- /Application/src/main/res/values/template-dimens.xml: -------------------------------------------------------------------------------- 1 | 16 | 17 | 18 | 19 | 20 | 21 | 4dp 22 | 8dp 23 | 16dp 24 | 32dp 25 | 64dp 26 | 27 | 28 | 29 | @dimen/margin_medium 30 | @dimen/margin_medium 31 | 32 | 33 | -------------------------------------------------------------------------------- /Application/src/main/res/values/template-styles.xml: -------------------------------------------------------------------------------- 1 | 16 | 17 | 18 | 19 | 20 | 21 | 34 | 35 | 41 | 42 | 43 | -------------------------------------------------------------------------------- /Application/tests/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 17 | 18 | 22 | 23 | 24 | 25 | 28 | 29 | 30 | 31 | 32 | 35 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /Application/tests/src/com/example/android/displayingbitmaps/tests/SampleTests.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2013 The Android Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.example.android.displayingbitmaps.tests; 17 | 18 | import com.example.android.displayingbitmaps.ui.ImageGridActivity; 19 | 20 | import android.test.ActivityInstrumentationTestCase2; 21 | 22 | /** 23 | * Tests for DisplayingBitmaps sample. 24 | */ 25 | public class SampleTests extends ActivityInstrumentationTestCase2 { 26 | 27 | private ImageGridActivity mTestActivity; 28 | 29 | public SampleTests() { 30 | super(ImageGridActivity.class); 31 | } 32 | 33 | @Override 34 | protected void setUp() throws Exception { 35 | super.setUp(); 36 | 37 | // Starts the activity under test using the default Intent with: 38 | // action = {@link Intent#ACTION_MAIN} 39 | // flags = {@link Intent#FLAG_ACTIVITY_NEW_TASK} 40 | // All other fields are null or empty. 41 | mTestActivity = getActivity(); 42 | } 43 | 44 | /** 45 | * Test if the test fixture has been set up correctly. 46 | */ 47 | public void testPreconditions() { 48 | //Try to add a message to add context to your assertions. These messages will be shown if 49 | //a tests fails and make it easy to understand why a test failed 50 | assertNotNull("mTestActivity is null", mTestActivity); 51 | } 52 | 53 | /** 54 | * Add more tests below. 55 | */ 56 | 57 | } 58 | -------------------------------------------------------------------------------- /CONTRIB.md: -------------------------------------------------------------------------------- 1 | # How to become a contributor and submit your own code 2 | 3 | ## Contributor License Agreements 4 | 5 | We'd love to accept your sample apps and patches! Before we can take them, we 6 | have to jump a couple of legal hurdles. 7 | 8 | Please fill out either the individual or corporate Contributor License Agreement (CLA). 9 | 10 | * If you are an individual writing original source code and you're sure you 11 | own the intellectual property, then you'll need to sign an [individual CLA] 12 | (https://developers.google.com/open-source/cla/individual). 13 | * If you work for a company that wants to allow you to contribute your work, 14 | then you'll need to sign a [corporate CLA] 15 | (https://developers.google.com/open-source/cla/corporate). 16 | 17 | Follow either of the two links above to access the appropriate CLA and 18 | instructions for how to sign and return it. Once we receive it, we'll be able to 19 | accept your pull requests. 20 | 21 | ## Contributing A Patch 22 | 23 | 1. Submit an issue describing your proposed change to the repo in question. 24 | 1. The repo owner will respond to your issue promptly. 25 | 1. If your proposed change is accepted, and you haven't already done so, sign a 26 | Contributor License Agreement (see details above). 27 | 1. Fork the desired repo, develop and test your code changes. 28 | 1. Ensure that your code adheres to the existing style in the sample to which 29 | you are contributing. Refer to the 30 | [Android Code Style Guide] 31 | (https://source.android.com/source/code-style.html) for the 32 | recommended coding standards for this organization. 33 | 1. Ensure that your code has an appropriate set of unit tests which all pass. 34 | 1. Submit a pull request. 35 | 36 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # How to become a contributor and submit your own code 2 | 3 | ## Contributor License Agreements 4 | 5 | We'd love to accept your sample apps and patches! Before we can take them, we 6 | have to jump a couple of legal hurdles. 7 | 8 | Please fill out either the individual or corporate Contributor License Agreement (CLA). 9 | 10 | * If you are an individual writing original source code and you're sure you 11 | own the intellectual property, then you'll need to sign an [individual CLA] 12 | (https://cla.developers.google.com). 13 | * If you work for a company that wants to allow you to contribute your work, 14 | then you'll need to sign a [corporate CLA] 15 | (https://cla.developers.google.com). 16 | 17 | Follow either of the two links above to access the appropriate CLA and 18 | instructions for how to sign and return it. Once we receive it, we'll be able to 19 | accept your pull requests. 20 | 21 | ## Contributing A Patch 22 | 23 | 1. Submit an issue describing your proposed change to the repo in question. 24 | 1. The repo owner will respond to your issue promptly. 25 | 1. If your proposed change is accepted, and you haven't already done so, sign a 26 | Contributor License Agreement (see details above). 27 | 1. Fork the desired repo, develop and test your code changes. 28 | 1. Ensure that your code adheres to the existing style in the sample to which 29 | you are contributing. Refer to the 30 | [Android Code Style Guide] 31 | (https://source.android.com/source/code-style.html) for the 32 | recommended coding standards for this organization. 33 | 1. Ensure that your code has an appropriate set of unit tests which all pass. 34 | 1. Submit a pull request. 35 | 36 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | -------------- 3 | 4 | Version 2.0, January 2004 5 | http://www.apache.org/licenses/ 6 | 7 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 8 | 9 | 1. Definitions. 10 | 11 | "License" shall mean the terms and conditions for use, reproduction, 12 | and distribution as defined by Sections 1 through 9 of this document. 13 | 14 | "Licensor" shall mean the copyright owner or entity authorized by 15 | the copyright owner that is granting the License. 16 | 17 | "Legal Entity" shall mean the union of the acting entity and all 18 | other entities that control, are controlled by, or are under common 19 | control with that entity. For the purposes of this definition, 20 | "control" means (i) the power, direct or indirect, to cause the 21 | direction or management of such entity, whether by contract or 22 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 23 | outstanding shares, or (iii) beneficial ownership of such entity. 24 | 25 | "You" (or "Your") shall mean an individual or Legal Entity 26 | exercising permissions granted by this License. 27 | 28 | "Source" form shall mean the preferred form for making modifications, 29 | including but not limited to software source code, documentation 30 | source, and configuration files. 31 | 32 | "Object" form shall mean any form resulting from mechanical 33 | transformation or translation of a Source form, including but 34 | not limited to compiled object code, generated documentation, 35 | and conversions to other media types. 36 | 37 | "Work" shall mean the work of authorship, whether in Source or 38 | Object form, made available under the License, as indicated by a 39 | copyright notice that is included in or attached to the work 40 | (an example is provided in the Appendix below). 41 | 42 | "Derivative Works" shall mean any work, whether in Source or Object 43 | form, that is based on (or derived from) the Work and for which the 44 | editorial revisions, annotations, elaborations, or other modifications 45 | represent, as a whole, an original work of authorship. For the purposes 46 | of this License, Derivative Works shall not include works that remain 47 | separable from, or merely link (or bind by name) to the interfaces of, 48 | the Work and Derivative Works thereof. 49 | 50 | "Contribution" shall mean any work of authorship, including 51 | the original version of the Work and any modifications or additions 52 | to that Work or Derivative Works thereof, that is intentionally 53 | submitted to Licensor for inclusion in the Work by the copyright owner 54 | or by an individual or Legal Entity authorized to submit on behalf of 55 | the copyright owner. For the purposes of this definition, "submitted" 56 | means any form of electronic, verbal, or written communication sent 57 | to the Licensor or its representatives, including but not limited to 58 | communication on electronic mailing lists, source code control systems, 59 | and issue tracking systems that are managed by, or on behalf of, the 60 | Licensor for the purpose of discussing and improving the Work, but 61 | excluding communication that is conspicuously marked or otherwise 62 | designated in writing by the copyright owner as "Not a Contribution." 63 | 64 | "Contributor" shall mean Licensor and any individual or Legal Entity 65 | on behalf of whom a Contribution has been received by Licensor and 66 | subsequently incorporated within the Work. 67 | 68 | 2. Grant of Copyright License. Subject to the terms and conditions of 69 | this License, each Contributor hereby grants to You a perpetual, 70 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 71 | copyright license to reproduce, prepare Derivative Works of, 72 | publicly display, publicly perform, sublicense, and distribute the 73 | Work and such Derivative Works in Source or Object form. 74 | 75 | 3. Grant of Patent License. Subject to the terms and conditions of 76 | this License, each Contributor hereby grants to You a perpetual, 77 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 78 | (except as stated in this section) patent license to make, have made, 79 | use, offer to sell, sell, import, and otherwise transfer the Work, 80 | where such license applies only to those patent claims licensable 81 | by such Contributor that are necessarily infringed by their 82 | Contribution(s) alone or by combination of their Contribution(s) 83 | with the Work to which such Contribution(s) was submitted. If You 84 | institute patent litigation against any entity (including a 85 | cross-claim or counterclaim in a lawsuit) alleging that the Work 86 | or a Contribution incorporated within the Work constitutes direct 87 | or contributory patent infringement, then any patent licenses 88 | granted to You under this License for that Work shall terminate 89 | as of the date such litigation is filed. 90 | 91 | 4. Redistribution. You may reproduce and distribute copies of the 92 | Work or Derivative Works thereof in any medium, with or without 93 | modifications, and in Source or Object form, provided that You 94 | meet the following conditions: 95 | 96 | (a) You must give any other recipients of the Work or 97 | Derivative Works a copy of this License; and 98 | 99 | (b) You must cause any modified files to carry prominent notices 100 | stating that You changed the files; and 101 | 102 | (c) You must retain, in the Source form of any Derivative Works 103 | that You distribute, all copyright, patent, trademark, and 104 | attribution notices from the Source form of the Work, 105 | excluding those notices that do not pertain to any part of 106 | the Derivative Works; and 107 | 108 | (d) If the Work includes a "NOTICE" text file as part of its 109 | distribution, then any Derivative Works that You distribute must 110 | include a readable copy of the attribution notices contained 111 | within such NOTICE file, excluding those notices that do not 112 | pertain to any part of the Derivative Works, in at least one 113 | of the following places: within a NOTICE text file distributed 114 | as part of the Derivative Works; within the Source form or 115 | documentation, if provided along with the Derivative Works; or, 116 | within a display generated by the Derivative Works, if and 117 | wherever such third-party notices normally appear. The contents 118 | of the NOTICE file are for informational purposes only and 119 | do not modify the License. You may add Your own attribution 120 | notices within Derivative Works that You distribute, alongside 121 | or as an addendum to the NOTICE text from the Work, provided 122 | that such additional attribution notices cannot be construed 123 | as modifying the License. 124 | 125 | You may add Your own copyright statement to Your modifications and 126 | may provide additional or different license terms and conditions 127 | for use, reproduction, or distribution of Your modifications, or 128 | for any such Derivative Works as a whole, provided Your use, 129 | reproduction, and distribution of the Work otherwise complies with 130 | the conditions stated in this License. 131 | 132 | 5. Submission of Contributions. Unless You explicitly state otherwise, 133 | any Contribution intentionally submitted for inclusion in the Work 134 | by You to the Licensor shall be under the terms and conditions of 135 | this License, without any additional terms or conditions. 136 | Notwithstanding the above, nothing herein shall supersede or modify 137 | the terms of any separate license agreement you may have executed 138 | with Licensor regarding such Contributions. 139 | 140 | 6. Trademarks. This License does not grant permission to use the trade 141 | names, trademarks, service marks, or product names of the Licensor, 142 | except as required for reasonable and customary use in describing the 143 | origin of the Work and reproducing the content of the NOTICE file. 144 | 145 | 7. Disclaimer of Warranty. Unless required by applicable law or 146 | agreed to in writing, Licensor provides the Work (and each 147 | Contributor provides its Contributions) on an "AS IS" BASIS, 148 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 149 | implied, including, without limitation, any warranties or conditions 150 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 151 | PARTICULAR PURPOSE. You are solely responsible for determining the 152 | appropriateness of using or redistributing the Work and assume any 153 | risks associated with Your exercise of permissions under this License. 154 | 155 | 8. Limitation of Liability. In no event and under no legal theory, 156 | whether in tort (including negligence), contract, or otherwise, 157 | unless required by applicable law (such as deliberate and grossly 158 | negligent acts) or agreed to in writing, shall any Contributor be 159 | liable to You for damages, including any direct, indirect, special, 160 | incidental, or consequential damages of any character arising as a 161 | result of this License or out of the use or inability to use the 162 | Work (including but not limited to damages for loss of goodwill, 163 | work stoppage, computer failure or malfunction, or any and all 164 | other commercial damages or losses), even if such Contributor 165 | has been advised of the possibility of such damages. 166 | 167 | 9. Accepting Warranty or Additional Liability. While redistributing 168 | the Work or Derivative Works thereof, You may choose to offer, 169 | and charge a fee for, acceptance of support, warranty, indemnity, 170 | or other liability obligations and/or rights consistent with this 171 | License. However, in accepting such obligations, You may act only 172 | on Your own behalf and on Your sole responsibility, not on behalf 173 | of any other Contributor, and only if You agree to indemnify, 174 | defend, and hold each Contributor harmless for any liability 175 | incurred by, or claims asserted against, such Contributor by reason 176 | of your accepting any such warranty or additional liability. 177 | 178 | END OF TERMS AND CONDITIONS 179 | 180 | APPENDIX: How to apply the Apache License to your work. 181 | 182 | To apply the Apache License to your work, attach the following 183 | boilerplate notice, with the fields enclosed by brackets "{}" 184 | replaced with your own identifying information. (Don't include 185 | the brackets!) The text should be enclosed in the appropriate 186 | comment syntax for the file format. We also recommend that a 187 | file or class name and description of purpose be included on the 188 | same "printed page" as the copyright notice for easier 189 | identification within third-party archives. 190 | 191 | Copyright {yyyy} {name of copyright owner} 192 | 193 | Licensed under the Apache License, Version 2.0 (the "License"); 194 | you may not use this file except in compliance with the License. 195 | You may obtain a copy of the License at 196 | 197 | http://www.apache.org/licenses/LICENSE-2.0 198 | 199 | Unless required by applicable law or agreed to in writing, software 200 | distributed under the License is distributed on an "AS IS" BASIS, 201 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 202 | See the License for the specific language governing permissions and 203 | limitations under the License. 204 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | Android DisplayingBitmaps Sample 3 | ================================ 4 | 5 | This repo has been migrated to [github.com/android/graphics][1]. Please check that repo for future updates. Thank you! 6 | 7 | [1]: https://github.com/android/graphics 8 | 9 | -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/googlearchive/android-DisplayingBitmaps/9ab780818d4d7bc9d96bca289f601546eb01ae9b/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Wed Apr 10 15:27:10 PDT 2013 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 10 | DEFAULT_JVM_OPTS="" 11 | 12 | APP_NAME="Gradle" 13 | APP_BASE_NAME=`basename "$0"` 14 | 15 | # Use the maximum available, or set MAX_FD != -1 to use that value. 16 | MAX_FD="maximum" 17 | 18 | warn ( ) { 19 | echo "$*" 20 | } 21 | 22 | die ( ) { 23 | echo 24 | echo "$*" 25 | echo 26 | exit 1 27 | } 28 | 29 | # OS specific support (must be 'true' or 'false'). 30 | cygwin=false 31 | msys=false 32 | darwin=false 33 | case "`uname`" in 34 | CYGWIN* ) 35 | cygwin=true 36 | ;; 37 | Darwin* ) 38 | darwin=true 39 | ;; 40 | MINGW* ) 41 | msys=true 42 | ;; 43 | esac 44 | 45 | # For Cygwin, ensure paths are in UNIX format before anything is touched. 46 | if $cygwin ; then 47 | [ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"` 48 | fi 49 | 50 | # Attempt to set APP_HOME 51 | # Resolve links: $0 may be a link 52 | PRG="$0" 53 | # Need this for relative symlinks. 54 | while [ -h "$PRG" ] ; do 55 | ls=`ls -ld "$PRG"` 56 | link=`expr "$ls" : '.*-> \(.*\)$'` 57 | if expr "$link" : '/.*' > /dev/null; then 58 | PRG="$link" 59 | else 60 | PRG=`dirname "$PRG"`"/$link" 61 | fi 62 | done 63 | SAVED="`pwd`" 64 | cd "`dirname \"$PRG\"`/" >&- 65 | APP_HOME="`pwd -P`" 66 | cd "$SAVED" >&- 67 | 68 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 69 | 70 | # Determine the Java command to use to start the JVM. 71 | if [ -n "$JAVA_HOME" ] ; then 72 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 73 | # IBM's JDK on AIX uses strange locations for the executables 74 | JAVACMD="$JAVA_HOME/jre/sh/java" 75 | else 76 | JAVACMD="$JAVA_HOME/bin/java" 77 | fi 78 | if [ ! -x "$JAVACMD" ] ; then 79 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 80 | 81 | Please set the JAVA_HOME variable in your environment to match the 82 | location of your Java installation." 83 | fi 84 | else 85 | JAVACMD="java" 86 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 87 | 88 | Please set the JAVA_HOME variable in your environment to match the 89 | location of your Java installation." 90 | fi 91 | 92 | # Increase the maximum file descriptors if we can. 93 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then 94 | MAX_FD_LIMIT=`ulimit -H -n` 95 | if [ $? -eq 0 ] ; then 96 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 97 | MAX_FD="$MAX_FD_LIMIT" 98 | fi 99 | ulimit -n $MAX_FD 100 | if [ $? -ne 0 ] ; then 101 | warn "Could not set maximum file descriptor limit: $MAX_FD" 102 | fi 103 | else 104 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 105 | fi 106 | fi 107 | 108 | # For Darwin, add options to specify how the application appears in the dock 109 | if $darwin; then 110 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 111 | fi 112 | 113 | # For Cygwin, switch paths to Windows format before running java 114 | if $cygwin ; then 115 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 116 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 117 | 118 | # We build the pattern for arguments to be converted via cygpath 119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 120 | SEP="" 121 | for dir in $ROOTDIRSRAW ; do 122 | ROOTDIRS="$ROOTDIRS$SEP$dir" 123 | SEP="|" 124 | done 125 | OURCYGPATTERN="(^($ROOTDIRS))" 126 | # Add a user-defined pattern to the cygpath arguments 127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 129 | fi 130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 131 | i=0 132 | for arg in "$@" ; do 133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 135 | 136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 138 | else 139 | eval `echo args$i`="\"$arg\"" 140 | fi 141 | i=$((i+1)) 142 | done 143 | case $i in 144 | (0) set -- ;; 145 | (1) set -- "$args0" ;; 146 | (2) set -- "$args0" "$args1" ;; 147 | (3) set -- "$args0" "$args1" "$args2" ;; 148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 154 | esac 155 | fi 156 | 157 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules 158 | function splitJvmOpts() { 159 | JVM_OPTS=("$@") 160 | } 161 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS 162 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" 163 | 164 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" 165 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 12 | set DEFAULT_JVM_OPTS= 13 | 14 | set DIRNAME=%~dp0 15 | if "%DIRNAME%" == "" set DIRNAME=. 16 | set APP_BASE_NAME=%~n0 17 | set APP_HOME=%DIRNAME% 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windowz variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | if "%@eval[2+2]" == "4" goto 4NT_args 53 | 54 | :win9xME_args 55 | @rem Slurp the command line arguments. 56 | set CMD_LINE_ARGS= 57 | set _SKIP=2 58 | 59 | :win9xME_args_slurp 60 | if "x%~1" == "x" goto execute 61 | 62 | set CMD_LINE_ARGS=%* 63 | goto execute 64 | 65 | :4NT_args 66 | @rem Get arguments from the 4NT Shell from JP Software 67 | set CMD_LINE_ARGS=%$ 68 | 69 | :execute 70 | @rem Setup the command line 71 | 72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if "%ERRORLEVEL%"=="0" goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 85 | exit /b 1 86 | 87 | :mainEnd 88 | if "%OS%"=="Windows_NT" endlocal 89 | 90 | :omega 91 | -------------------------------------------------------------------------------- /packaging.yaml: -------------------------------------------------------------------------------- 1 | # GOOGLE SAMPLE PACKAGING DATA 2 | # 3 | # This file is used by Google as part of our samples packaging process. 4 | # End users may safely ignore this file. It has no relevance to other systems. 5 | --- 6 | 7 | status: PUBLISHED 8 | technologies: [Android] 9 | categories: [UI] 10 | languages: [Java] 11 | solutions: [Mobile] 12 | github: googlesamples/android-DisplayingBitmaps 13 | level: BEGINNER 14 | icon: DisplayingBitmapsSample/src/main/res/drawable-xxhdpi/ic_launcher.png 15 | license: apache2-android 16 | -------------------------------------------------------------------------------- /screenshots/1-gridview.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/googlearchive/android-DisplayingBitmaps/9ab780818d4d7bc9d96bca289f601546eb01ae9b/screenshots/1-gridview.png -------------------------------------------------------------------------------- /screenshots/2-detail.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/googlearchive/android-DisplayingBitmaps/9ab780818d4d7bc9d96bca289f601546eb01ae9b/screenshots/2-detail.png -------------------------------------------------------------------------------- /screenshots/icon-web.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/googlearchive/android-DisplayingBitmaps/9ab780818d4d7bc9d96bca289f601546eb01ae9b/screenshots/icon-web.png -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include 'Application' 2 | --------------------------------------------------------------------------------