加载大型位图有效的android [英] loading large bitmaps efficiently android
问题描述
我试图加载大位图。
我这样说的:
http://developer.android.com/training/displaying-bitmaps/load -bitmap.html
和我尝试从字符串同样的方法
公共静态位图德codeSampleBitmapFromStream(InputStream为,诠释reqWidth,诠释reqHeight){
//首先去code。与inJustDe codeBounds = true来检查尺寸
最后BitmapFactory.Options选项=新BitmapFactory.Options();
options.inJustDe codeBounds = TRUE;
BitmapFactory.de codeStream(是,空,期权); //计算inSampleSize
options.inSampleSize = calculateInSampleSize(选项,reqWidth,reqHeight); //德code位与inSampleSize集
options.inJustDe codeBounds = FALSE;
返回BitmapFactory.de codeStream(是,空,期权);
}
我敢肯定的InputStream是正确的,因为在previous版本我是直接加载位图
使用去codeSampleBitmapFromStream我一直有一个返回null
感谢
修改
我尝试InputStream的使用后添加此
{尝试
is.mark(1 <<;&下; 24);
//也尽量is.mark(0);
is.reset();
}赶上(IOException异常五){
// TODO自动生成catch块
e.printStackTrace();
}
我有这样的错误
9月六日至18日:55:31.831:W / System.err的(16460):java.io.IOException异常
九月六日至18日:55:31.831:W / System.err的(16460):在java.io.InputStream.reset(InputStream.java:221)
九月六日至18日:55:31.831:W / System.err的(16460):在ant.fileExplorer.FileExplorerAdapter.de codeSampleBitmapFromStream(FileExplorerAdapter.java:124)
九月六日至18日:55:31.831:W / System.err的(16460):在ant.fileExplorer.FileExplorerAdapter.getView(FileExplorerAdapter.java:56)
九月六日至18日:55:31.831:W / System.err的(16460):在android.widget.AbsListView.obtainView(AbsListView.java:1430)
九月六日至18日:55:31.831:W / System.err的(16460):在android.widget.ListView.makeAndAddView(ListView.java:1793)
九月六日至18日:55:31.831:W / System.err的(16460):在android.widget.ListView.fillDown(ListView.java:670)
九月六日至18日:55:31.831:W / System.err的(16460):在android.widget.ListView.fillFromTop(ListView.java:727)
九月六日至18日:55:31.831:W / System.err的(16460):在android.widget.ListView.layoutChildren(ListView.java:1646)
九月六日至18日:55:31.831:W / System.err的(16460):在android.widget.AbsListView.onLayout(AbsListView.java:1260)
九月六日至18日:55:31.841:W / System.err的(16460):在android.view.View.layout(View.java:7277)
九月六日至18日:55:31.841:W / System.err的(16460):在android.widget.LinearLayout.setChildFrame(LinearLayout.java:1254)
九月六日至18日:55:31.841:W / System.err的(16460):在android.widget.LinearLayout.layoutVertical(LinearLayout.java:1130)
九月六日至18日:55:31.841:W / System.err的(16460):在android.widget.LinearLayout.onLayout(LinearLayout.java:1047)
九月六日至18日:55:31.841:W / System.err的(16460):在android.view.View.layout(View.java:7277)
九月六日至18日:55:31.841:W / System.err的(16460):在android.widget.FrameLayout.onLayout(FrameLayout.java:338)
九月六日至18日:55:31.841:W / System.err的(16460):在android.view.View.layout(View.java:7277)
九月六日至18日:55:31.841:W / System.err的(16460):在android.widget.LinearLayout.setChildFrame(LinearLayout.java:1254)
九月六日至18日:55:31.841:W / System.err的(16460):在android.widget.LinearLayout.layoutVertical(LinearLayout.java:1130)
九月六日至18日:55:31.841:W / System.err的(16460):在android.widget.LinearLayout.onLayout(LinearLayout.java:1047)
九月六日至18日:55:31.841:W / System.err的(16460):在android.view.View.layout(View.java:7277)
九月六日至18日:55:31.841:W / System.err的(16460):在android.widget.FrameLayout.onLayout(FrameLayout.java:338)
九月六日至18日:55:31.841:W / System.err的(16460):在android.view.View.layout(View.java:7277)
九月六日至18日:55:31.841:W / System.err的(16460):在android.view.ViewRoot.performTraversals(ViewRoot.java:1203)
九月六日至18日:55:31.841:W / System.err的(16460):在android.view.ViewRoot.handleMessage(ViewRoot.java:1957)
九月六日至18日:55:31.841:W / System.err的(16460):在android.os.Handler.dispatchMessage(Handler.java:99)
九月六日至18日:55:31.841:W / System.err的(16460):在android.os.Looper.loop(Looper.java:150)
九月六日至18日:55:31.841:W / System.err的(16460):在android.app.ActivityThread.main(ActivityThread.java:4277)
九月六日至18日:55:31.841:W / System.err的(16460):在java.lang.reflect.Method.invokeNative(本机方法)
九月六日至18日:55:31.851:W / System.err的(16460):在java.lang.reflect.Method.invoke(Method.java:507)
九月六日至18日:55:31.851:W / System.err的(16460):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:839)
九月六日至18日:55:31.851:W / System.err的(16460):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
九月六日至18日:55:31.851:W / System.err的(16460):在dalvik.system.NativeStart.main(本机方法)
尝试添加 is.reset()
之前,你要加载的位图真正的
//首先确保你正在使用的BufferedInputStream
InputStream的双=新的BufferedInputStream(是)//德code位与inSampleSize集
options.inJustDe codeBounds = FALSE;
bis.reset();
我相信当你使用 inJustDe codeBounds = TRUE
有趣的事情发生在流。它会看过一些流该操作的。重设它为我工作。
编辑:你需要你的包装InputStream对象变成的BufferedInputStream
,它支持 .reset段()
I'm trying to load large bitmap.
I read this: http://developer.android.com/training/displaying-bitmaps/load-bitmap.html
and I try to create the same method from string
public static Bitmap decodeSampleBitmapFromStream(InputStream is, int reqWidth, int reqHeight){
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeStream(is,null,options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeStream(is,null,options);
}
I'm sure the inputStream is correct because in a previous version I was directly loading the bitmaps
using decodeSampleBitmapFromStream I always have a null return
Thanks
Edit
I try to add this after the inputStream use
try {
is.mark(1 << 24);
// also try is.mark(0);
is.reset();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
I have this error
06-18 09:55:31.831: W/System.err(16460): java.io.IOException
06-18 09:55:31.831: W/System.err(16460): at java.io.InputStream.reset(InputStream.java:221)
06-18 09:55:31.831: W/System.err(16460): at ant.fileExplorer.FileExplorerAdapter.decodeSampleBitmapFromStream(FileExplorerAdapter.java:124)
06-18 09:55:31.831: W/System.err(16460): at ant.fileExplorer.FileExplorerAdapter.getView(FileExplorerAdapter.java:56)
06-18 09:55:31.831: W/System.err(16460): at android.widget.AbsListView.obtainView(AbsListView.java:1430)
06-18 09:55:31.831: W/System.err(16460): at android.widget.ListView.makeAndAddView(ListView.java:1793)
06-18 09:55:31.831: W/System.err(16460): at android.widget.ListView.fillDown(ListView.java:670)
06-18 09:55:31.831: W/System.err(16460): at android.widget.ListView.fillFromTop(ListView.java:727)
06-18 09:55:31.831: W/System.err(16460): at android.widget.ListView.layoutChildren(ListView.java:1646)
06-18 09:55:31.831: W/System.err(16460): at android.widget.AbsListView.onLayout(AbsListView.java:1260)
06-18 09:55:31.841: W/System.err(16460): at android.view.View.layout(View.java:7277)
06-18 09:55:31.841: W/System.err(16460): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1254)
06-18 09:55:31.841: W/System.err(16460): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1130)
06-18 09:55:31.841: W/System.err(16460): at android.widget.LinearLayout.onLayout(LinearLayout.java:1047)
06-18 09:55:31.841: W/System.err(16460): at android.view.View.layout(View.java:7277)
06-18 09:55:31.841: W/System.err(16460): at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
06-18 09:55:31.841: W/System.err(16460): at android.view.View.layout(View.java:7277)
06-18 09:55:31.841: W/System.err(16460): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1254)
06-18 09:55:31.841: W/System.err(16460): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1130)
06-18 09:55:31.841: W/System.err(16460): at android.widget.LinearLayout.onLayout(LinearLayout.java:1047)
06-18 09:55:31.841: W/System.err(16460): at android.view.View.layout(View.java:7277)
06-18 09:55:31.841: W/System.err(16460): at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
06-18 09:55:31.841: W/System.err(16460): at android.view.View.layout(View.java:7277)
06-18 09:55:31.841: W/System.err(16460): at android.view.ViewRoot.performTraversals(ViewRoot.java:1203)
06-18 09:55:31.841: W/System.err(16460): at android.view.ViewRoot.handleMessage(ViewRoot.java:1957)
06-18 09:55:31.841: W/System.err(16460): at android.os.Handler.dispatchMessage(Handler.java:99)
06-18 09:55:31.841: W/System.err(16460): at android.os.Looper.loop(Looper.java:150)
06-18 09:55:31.841: W/System.err(16460): at android.app.ActivityThread.main(ActivityThread.java:4277)
06-18 09:55:31.841: W/System.err(16460): at java.lang.reflect.Method.invokeNative(Native Method)
06-18 09:55:31.851: W/System.err(16460): at java.lang.reflect.Method.invoke(Method.java:507)
06-18 09:55:31.851: W/System.err(16460): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
06-18 09:55:31.851: W/System.err(16460): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
06-18 09:55:31.851: W/System.err(16460): at dalvik.system.NativeStart.main(Native Method)
Try adding is.reset()
before you want to load the bitmap "for real"
// First make sure you are using a BufferedInputStream
InputStream bis = new BufferedInputStream(is)
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
bis.reset();
I believe funny things happen to the stream when you use inJustDecodeBounds = true
. It will have read some of the stream for that operation. Resetting it works for me.
Edit: you'll need to wrap your inputStream object into a BufferedInputStream
, which supports .reset()
.
这篇关于加载大型位图有效的android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!