31

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

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 日志

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
4

5 回答 5

63

我遇到了同样的问题。而且我确保下载图像的 url 是正确的。通过调试代码,我发现第一次解码后inputstream中的位置变量设置为1024。所以我在第二次解码之前添加了inputstream.reset() 。这样可行。希望可以帮助别人。

于 2012-10-25T05:46:11.347 回答
12

环顾四周后,我得到了最好的解决方案。

正如#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);
于 2014-05-21T15:19:17.790 回答
12

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

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

于 2016-06-20T23:44:47.420 回答
1

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

于 2014-03-24T18:42:53.107 回答
1

与 Picasso 一样,确保您的 url 正确,只需将开发程序中的链接复制并粘贴到您的网络浏览器中

我输入:

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

代替

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

于 2017-02-25T08:46:17.823 回答