Ajouve
Ajouve

Reputation: 10089

loading large bitmaps efficiently android

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)

Upvotes: 0

Views: 1512

Answers (1)

Ken Wolf
Ken Wolf

Reputation: 23269

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().

Upvotes: 1

Related Questions