You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Merge "Updates to "Displaying Bitmaps Efficiently" class. Changes: -Updated code sample (see http://ag/214812) -Updated code snippets to match updated sample -Fixed <> in code snippets -Updated disk cache section -Some other minor updates" into jb-dev
rely on images being available in this cache. Components like {@link android.widget.GridView} with
180
180
larger datasets can easily fill up a memory cache. Your application could be interrupted by another
181
181
task like a phone call, and while in the background it might be killed and the memory cache
182
-
destroyed. Once the user resumes, your application it has to process each image again.</p>
182
+
destroyed. Once the user resumes, your application has to process each image again.</p>
183
183
184
184
<p>A disk cache can be used in these cases to persist processed bitmaps and help decrease loading
185
185
times where images are no longer available in a memory cache. Of course, fetching images from disk
@@ -190,18 +190,14 @@ be unpredictable.</p>
190
190
appropriate place to store cached images if they are accessed more frequently, for example in an
191
191
image gallery application.</p>
192
192
193
-
<p>Included in the sample code of this class is a basic {@code DiskLruCache} implementation.
194
-
However, a more robust and recommended {@code DiskLruCache} solution is included in the Android 4.0
195
-
source code ({@code libcore/luni/src/main/java/libcore/io/DiskLruCache.java}). Back-porting this
196
-
class for use on previous Android releases should be fairly straightforward (a <a
197
-
href="http://www.google.com/search?q=disklrucache">quick search</a> shows others who have already
198
-
implemented this solution).</p>
199
-
200
-
<p>Here’s updated example code that uses the simple {@code DiskLruCache} included in the sample
201
-
application of this class:</p>
193
+
<p>The sample code of this class uses a {@code DiskLruCache} implementation that is pulled from the
194
+
<a href="https://android.googlesource.com/platform/libcore/+/master/luni/src/main/java/libcore/io/DiskLruCache.java">Android source</a>. Here’s updated example code that adds a disk cache in addition
195
+
to the existing memory cache:</p>
202
196
203
197
<pre>
204
-
private DiskLruCache mDiskCache;
198
+
private DiskLruCache mDiskLruCache;
199
+
private final Object mDiskCacheLock = new Object();
200
+
private boolean mDiskCacheStarting = true;
205
201
private static final int DISK_CACHE_SIZE = 1024 * 1024 * 10; // 10MB
206
202
private static final String DISK_CACHE_SUBDIR = "thumbnails";
Copy file name to clipboardExpand all lines: docs/html/training/displaying-bitmaps/display-bitmap.jd
+13-11Lines changed: 13 additions & 11 deletions
Original file line number
Diff line number
Diff line change
@@ -103,7 +103,8 @@ public class ImageDetailActivity extends FragmentActivity {
103
103
}
104
104
</pre>
105
105
106
-
<p>The details {@link android.app.Fragment} holds the {@link android.widget.ImageView} children:</p>
106
+
<p>Here is an implementation of the details {@link android.app.Fragment} which holds the {@link android.widget.ImageView} children. This might seem like a perfectly reasonable approach, but can
107
+
you see the drawbacks of this implementation? How could it be improved?</p>
107
108
108
109
<pre>
109
110
public class ImageDetailFragment extends Fragment {
@@ -146,11 +147,11 @@ public class ImageDetailFragment extends Fragment {
146
147
}
147
148
</pre>
148
149
149
-
<p>Hopefully you noticed the issue with this implementation; The images are being read from
150
-
resources on the UI thread which can lead to an application hanging and being force closed. Using an
151
-
{@link android.os.AsyncTask} as described in the <a href="process-bitmap.html">Processing Bitmaps Off
152
-
the UI Thread</a> lesson, it’s straightforward to move image loading and processing to a background
153
-
thread:</p>
150
+
<p>Hopefully you noticed the issue: the images are being read from resources on the UI thread,
151
+
which can lead to an application hanging and being force closed. Using an
152
+
{@link android.os.AsyncTask} as described in the <a href="process-bitmap.html">Processing Bitmaps
153
+
Off the UI Thread</a> lesson, it’s straightforward to move image loading and processing to a
154
+
background thread:</p>
154
155
155
156
<pre>
156
157
public class ImageDetailActivity extends FragmentActivity {
@@ -190,7 +191,7 @@ modifications for a memory cache:</p>
190
191
<pre>
191
192
public class ImageDetailActivity extends FragmentActivity {
0 commit comments