且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

安卓:SkImageDe codeR ::厂返回null

更新时间:2022-10-29 13:40:18

这也许可以帮助:

  

inJustDe codeBounds:如果设置为true,去codeR将返回null(没有位图),但出...领域仍将设置,允许调用者查询的位图,而不必分配的内存的像素。

问候,

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

Maybe this can help:

inJustDecodeBounds: If set to true, the decoder will return null (no bitmap), but the out... fields will still be set, allowing the caller to query the bitmap without having to allocate the memory for its pixels.

Regards,