有效加载大型位图android

时间:2013-06-18 07:27:42

标签: android bitmap

我正在尝试加载大位图。

我读到了这个: http://developer.android.com/training/displaying-bitmaps/load-bitmap.html

我尝试从字符串

创建相同的方法
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);
}

我确定inputStream是正确的,因为在之前的版本中我直接加载了位图

使用decodeSampleBitmapFromStream我总是有一个空返回

由于

修改

我尝试在inputStream使用后添加它

try {
    is.mark(1 << 24);
    // also try is.mark(0);
    is.reset();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

我有这个错误

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)

1 个答案:

答案 0 :(得分:1)

在您想要加载位图“for real”

之前,请尝试添加is.reset()
// First make sure you are using a BufferedInputStream
InputStream bis = new BufferedInputStream(is)

// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
bis.reset();

我相信当您使用inJustDecodeBounds = true时,流会发生有趣的事情。它将读取该操作的一些流。重置它对我有用。

编辑:你需要将inputStream对象包装成BufferedInputStream,它支持.reset()