簡體   English   中英

Android:SkImageDecoder:: 工廠返回 null

[英]Android: SkImageDecoder:: Factory returned null

我正在使用我的本地主機來獲取圖像並在 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

我遇到了同樣的問題。 而且我還要確保下載圖片的網址是正確的。 通過調試代碼,發現第一次解碼后,inputstream中的position變量被設置為1024。 所以我在第二次解碼之前添加inputstream.reset() 這樣可行。 希望可以幫助別人。

環顧四周后,我得到了最好的解決方案。

正如#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);

這可能是一種罕見的情況,但對於那些使用Picasso 的人來說,如果您嘗試從 URL 加載圖像但 URL 未引用圖像,則會看到此錯誤。

例如:
http://www.google.ca
代替:
https://www.google.ca/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png

從圖庫應用程序返回的 Intent 讀取流時,我遇到了類似的問題。 inputstream.reset() 拋出一個 IOException,正如 Shellum 上面提到的,所以我通過關閉流並再次重新打開來解決它。 很簡單,而且成功了。

與畢加索一樣,請確保您的網址正確,只需將開發程序中的鏈接復制並粘貼到您的網絡瀏覽器中

我輸入:

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

代替

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM