Android:SkImageDecoder:: 工厂返回 null

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12006785/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 09:26:18  来源:igfitidea点击:

Android: SkImageDecoder:: Factory returned null

android

提问by user1006896

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!

我正在使用我的本地主机来获取图像并在 ImageView 中查看。出于某种原因,我收到 Factory 返回的 null 错误。我已经看了很多次代码,但我看不出有什么问题。任何帮助,将不胜感激!

GalleryZoom.java

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

LogCat 日志

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

回答by George Jia

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.

我遇到了同样的问题。而且我还要确保下载图片的网址是正确的。通过调试代码,发现第一次解码后,inputstream中的position变量被设置为1024。所以我在第二次解码之前添加inputstream.reset()。那个有效。希望可以帮助别人。

回答by Xenione

Hi guys I after looking around I got the best solution. as #jia George point out you shold 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.

大家好,我环顾四周后得到了最好的解决方案。正如#jia George 指出的,您应该在第一次解码后重置输入流,问题是不支持某些时间重置,但您可以将输入流包装在 BufferedInputStream 中,这很好。

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);

回答by alexgophermix

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

这可能是一种罕见的情况,但对于那些使用Picasso 的人来说,如果您尝试从 URL 加载图像但 URL 未引用图像,则会看到此错误。

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

例如:
http: //www.google.ca
而不是:https:
//www.google.ca/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png

回答by Dom

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

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

instead of

代替

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

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

回答by emote_control

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.

从图库应用程序返回的 Intent 读取流时,我遇到了类似的问题。inputstream.reset() 抛出 IOException,正如 Shellum 上面提到的,所以我通过关闭流并再次重新打开来解决它。很简单,而且成功了。