31

I'm using my localhost to fetch images and to view in an ImageView. For some reason I'm getting Factory returned null error. I've looked through the code many times and I don't see what's wrong. Any help would be appreciated!

GalleryZoom.java

public class Zoom extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.gallery_zoom);

        String selection = getIntent().getExtras().getString("image");
        Toast.makeText(this, selection, Toast.LENGTH_LONG).show();

        new backgroundLoader().execute();       
    }


    private class backgroundLoader extends AsyncTask<Void, Void, Void> {
        Bitmap bmp;

        @Override
        protected Void doInBackground(Void... params) {

            bmp = DecodeBitmapSampleSize(getIntent().getExtras().getString("image"), 48, 64);
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);

            ImageView image = (ImageView) findViewById(R.id.imageZoom);
            image.setImageBitmap(bmp);
        }

    }

    public Bitmap DecodeBitmapSampleSize (String strURL, int reqWidth, int reqHeight) {
        InputStream in = null;
        Bitmap bmp = null;

        in = OpenHttpConnection(strURL);
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(in, null, options);

        options.inSampleSize = calculateSampleSize(options, reqWidth, reqHeight);

        options.inJustDecodeBounds = false;
        bmp = BitmapFactory.decodeStream(in, null, options);
                return bmp;
    }

    private InputStream OpenHttpConnection(String strURL) {

        try {
            URL url = new URL(strURL);

            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            InputStream in = new BufferedInputStream(connection.getInputStream());
            return in;
        } catch (Exception exception) {
            exception.printStackTrace();
            return null;
        }
    }

    public static int calculateSampleSize(BitmapFactory.Options options,
            int reqWidth, int reqHeight) {

        final int width = options.outWidth;
        final int height = options.outHeight;
        int inSampleSize = 1;

        if (width > reqWidth || height > reqHeight) {
            if (width > height) {
                inSampleSize = Math.round((float) height / (float) reqHeight);
            } else {
                inSampleSize = Math.round((float) width / (float) reqWidth);
            }
        }
        return inSampleSize;
    }

}

LogCat Log

08-13 21:55:19.578: I/MemoryCache(3197): MemoryCache maximum limit is 6MB
08-13 21:55:19.658: I/MemoryCache(3197): cache size = 24600, length = 1
08-13 21:55:19.688: I/MemoryCache(3197): cache size = 24600, length = 1
08-13 21:55:19.708: I/MemoryCache(3197): cache size = 24600, length = 1
08-13 21:55:19.708: I/MemoryCache(3197): cache size = 24600, length = 1
08-13 21:55:20.628: I/MemoryCache(3197): cache size = 71600, length = 2
08-13 21:55:20.678: I/MemoryCache(3197): cache size = 101408, length = 3
08-13 21:55:26.228: I/MemoryCache(3197): MemoryCache maximum limit is 6MB
08-13 21:55:26.228: I/MemoryCache(3197): MemoryCache maximum limit is 6MB
08-13 21:55:26.998: D/skia(3197): --- SkImageDecoder::Factory returned null
user1006896
  • 315
  • 1
  • 5
  • 8
  • refer this post it worked for me : [http://stackoverflow.com/questions/23559736/android-skimagedecoderfactory-returned-null-error][1] [1]: http://stackoverflow.com/questions/23559736/android-skimagedecoderfactory-returned-null-error – mithil1501 Jan 22 '15 at 12:30

5 Answers5

63

I have encountered the same problem. And also I make sure the url is correct to download the image. By debugging the code, I found the variable of position in inputstream was set to 1024 after the first decode. So I add inputstream.reset() before the second decode. That works. Hope can help others.

George Jia
  • 659
  • 6
  • 7
  • 6
    This worked for me, but make sure you call ```boolean supported = inputStream.markSupported()``` first. Marking must be supported or reset() will throw an ```IOException```. If it's not supported, maybe open up a second input stream. – Shellum Jun 19 '13 at 16:01
  • how did you solve this? i got the same problem and i've posted about it here: http://stackoverflow.com/questions/17774442/how-to-get-bitmap-information-and-then-decode-bitmap-from-internet-inputstream . for some reason, it doesn't work on some very specific websites and files. – android developer Jul 21 '13 at 20:56
12

After looking around I got the best solution.

As #jia George point out, you should reset the inputstream after first decoding, the issue is that some time reset is not supported but you can wrap inputstream inside a BufferedInputStream and this one is fine.

final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true; 
BufferedInputStream buffer=new BufferedInputStream(is);
BitmapFactory.decodeStream(buffer,null,options);
buffer.reset();

    // Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth,reqHeight);

    // Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false; 
BitmapFactory.decodeStream(buffer,null,options);
peterh
  • 11,875
  • 18
  • 85
  • 108
Xenione
  • 2,174
  • 1
  • 23
  • 30
12

This might be a rare case but for those of you using Picasso you'll see this error if you try to load an image from URL but the URL doesn't refer to an image.

For example:
http://www.google.ca
instead of:
https://www.google.ca/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png

alexgophermix
  • 4,189
  • 5
  • 32
  • 59
  • Also, some URL's may end with a `.jpg`, `.png`, `.tif`, etc., but do not actually refer to an image. For example: http://www.guoguiyan.com/data/out/96/69698096-high-resolution-wallpapers.jpg. – shoe Oct 02 '17 at 20:33
1

I had a similar problem when reading a stream from an Intent returned from the gallery app. inputstream.reset() throws an IOException, as Shellum mentions above, so I solved it by just closing the stream and reopening it again. Simple, and did the trick.

emote_control
  • 745
  • 6
  • 21
1

As above with Picasso make sure that your url is correct, just copy and paste the link from your development program into your web browser

I typed:

http://lorepixel.com/600/400/city

instead of

http://lorempixel.com/600/400/city

Dom
  • 11
  • 2