Comments (10)
Hi Patrick, can you post the code you use to create the cache?
from android-bitmapcache.
Also, can you post the code around the put_() and get_() calls?
from android-bitmapcache.
Creating of cache:
File cacheLocation = new File(Environment.getExternalStorageDirectory()
+ "/Android/data/jp.co.cyberagent.httpsimple.sample.simple");
mBitmapCache = new BitmapLruCache.Builder(this)
.setDiskCacheEnabled(true)
.setDiskCacheLocation(cacheLocation)
.setMemoryCacheEnabled(true)
.setMemoryCacheMaxSizeUsingHeapSize(0.5f)
.build();
Calling of get and put areas:
private long mLastTimeStamp;
private void logTime(String message) {
if (mLastTimeStamp != 0) {
Log.i("TEST", (System.currentTimeMillis() - mLastTimeStamp) + "ms " + message);
}
mLastTimeStamp = System.currentTimeMillis();
}
@Override
public void run() {
if (mCanceled) {
return;
}
HttpSimple httpSimple = mContract.getHttpSimple();
BitmapLruCache bitmapCache = mContract.getBitmapCache();
logTime("Start");
Drawable drawable = bitmapCache.get(mUrl);
if (drawable != null) {
show(drawable);
logTime("Drawable from Cache");
return;
}
try {
// download
mRequest = httpSimple.get(mUrl);
InputStream inputStream = mRequest
.receiveProgress(this)
.inputStream();
logTime("Connect to server");
if (inputStream == null) {
return;
}
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
if (bitmap == null) {
return;
}
logTime("Download/decode bitmap");
// resize
Bitmap resizedBitmap =
Bitmap.createScaledBitmap(bitmap, mImageSize, mImageSize, false);
logTime("Resize Bitmap");
resizedBitmap.compress(Bitmap.CompressFormat.JPEG, 100, new FileOutputStream(new File(Environment.getExternalStorageDirectory(), "test.png")));
logTime("Compress as JPEG/100 to sd card (manually)");
// cache
// TODO this is slow
drawable = bitmapCache.put(mUrl, resizedBitmap);
logTime("Put into bitmapCache");
// display
show(drawable);
} catch (Exception e) {
e.printStackTrace();
}
}
But I think I found the problem. compressing to PNG takes a lot longer than compressing to JPEG. In the above times I was compressing to JPEG. When I switched to compress to PNG (as you do), than I get about the same times.
Same applies by getting. I guess PNG is just not fast enough for big pictures (mine are around 1000x1000).
So would it be possible to switch to JPEG (maybe through some setting)? For my use case, that would speed up things a lot.
Times with PNG:
I/TEST (10482): 334ms Connect to server
I/TEST (10482): 954ms Download/decode bitmap
I/TEST (10482): 111ms Resize Bitmap
I/TEST (10482): 2199ms Compress as PNG/100 to sd card (manually)
I/TEST (10482): 1986ms Put into bitmapCache
JPEG
I/TEST (12746): 114ms Connect to server
I/TEST (12746): 311ms Download/decode bitmap
I/TEST (12746): 278ms Resize Bitmap
I/TEST (12746): 911ms Compress as JPEG/100 to sd card (manually)
I/TEST (12746): 3082ms Put into bitmapCache
from android-bitmapcache.
The commit above allows you to specify the compression format.
FYI, you can completely omit the encode and decode though by using the put(String, InputStream)
or put(String, InputStream, BitmapFactory.Options)
methods.
from android-bitmapcache.
Thank you very much!
Yes, using the InputStream would be the best method to use. But images I am getting are not the correct size (sadly can not use a resizing image API service).
from android-bitmapcache.
You could use inSampleSize
with the InputStream method ;)
from android-bitmapcache.
I could, but it does not do the job :). Problem of inSampleSize are 2:
- It only allows me to reduce the size by factor 2. But to make android render nicely I need the exact size. ImageView scaling runs on UI thread which makes ListView/ViewPager pretty slow...
- To know the inSampleSize I need to use I need to run it once with inJustDecodeBounds. With a stream from the internet, that would be bad. Okay, I could quickly store it as a file and do it with a FileInputStream that i then as well give to BitmapCache.
For those reasons, the method you created seems to be the best for the task. Or do you have another idea?
Thx a lot for a great library!
from android-bitmapcache.
Ah ok. I just want to make sure you were aware of this more efficient way.
Just to correct you a little bit, ImageView scaling works via draw matrices, which is actually efficient (especially on GPU accelerated canvases).
from android-bitmapcache.
Thank you for the correction! Will have to try that out again. Last time I tried I think ListView had small stutters because of image scaling. Or maybe it was something else.
from android-bitmapcache.
On 1 May 2013 12:27, Patrick Boos [email protected] wrote:
Thank you for the correction! Will have to try that out again. Last time I
tried I think ListView had small stutters because of image scaling. Or
maybe it was something else.Might be related to
https://plus.google.com/113058165720861374515/posts/iTk4PjgeAWX
from android-bitmapcache.
Related Issues (20)
- Incompatibility with latest DiskLruCache (2.0) HOT 1
- Images sometimes don't render in listview
- Recycled Bitmap Method Stack
- Running sample app in Android Studio
- Frequently getting the error of "java.lang.OutOfMemoryError" exception
- i test the simple app,IllegalArgumentException
- Need a way to purge the DiskCache. HOT 1
- TransitionDrawable and Recycled Bitmap Exception HOT 2
- After I updated to BitmapCache 2.3 version, OutOfMemoryError happens frequently
- NullpointerException. mRecyclePolicy is null HOT 2
- Removed bitmaps on LruCache is not checked if it is being used HOT 1
- RecyclePolicy not interpreted as expected
- NoClassDefFoundError HOT 1
- Facebook Login error
- Strange gradle issue? java.lang.NoSuchMethodError: No static method getPointerCount(Landroid/view/MotionEvent;)I in class Landroid/support/v4/view/MotionEventCompatEclair HOT 1
- CacheableBitmapDrawable cannot be previewed in Android Studio debugger
- Gradle build includes ancient support-lib-v4 HOT 3
- CacheableBitmapDrawable sets null in detach, should probably just set unused
- Current Repo not maintained - new suggested 'head' until chris returns HOT 1
- Encoding/encryption processing
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from android-bitmapcache.