Search in sources :

Example 6 with ParseError

use of com.android.volley.ParseError in project AndroidLife by CaMnter.

the class ImageRequest method doParse.

/**
 * The real guts of parseNetworkResponse. Broken out for readability.
 */
/*
     * 这里开始真正解析 网络请求结果( 响应 )NetworkResponse
     * NetworkResponse -> Response<Bitmap> 的转换
     */
private Response<Bitmap> doParse(NetworkResponse response) {
    // 拿到结果数据
    byte[] data = response.data;
    // 实例化一个 BitmapFactory.Options 用于解析数据成 Bitmap
    BitmapFactory.Options decodeOptions = new BitmapFactory.Options();
    Bitmap bitmap = null;
    // 如果缺少 最大宽度 和 最大高度
    if (mMaxWidth == 0 && mMaxHeight == 0) {
        // 设置 BitmapFactory.Options.Config
        decodeOptions.inPreferredConfig = mDecodeConfig;
        // 开始生成 Bitmap
        bitmap = BitmapFactory.decodeByteArray(data, 0, data.length, decodeOptions);
    } else {
        // If we have to resize this image, first get the natural bounds.
        /**
         * 如果存在 最大宽度 和 最大高度
         */
        /*
             * 由于一下四行操作只是想拿到这个 Bitmap 的自身的实际宽高,但又不想申请一个 Bitmap 内存
             * 可以设置 inJustDecodeBounds = true,只是读图片大小,不申请 Bitmap 内存
             * BitmapFactory.decodeByteArray(...) 的时候,就会 return null
             * 此时,再通过 BitmapFactory.Options 内被设置好的 outWidth 和 outHeight
             * 拿到该 Bitmap 的自身的实际宽高
             */
        decodeOptions.inJustDecodeBounds = true;
        /*
             * 这里正常是 Bitmap bitmap = BitmapFactory.decodeByteArray(...)
             * 但是由于上面设置了 inJustDecodeBounds = true
             * 这里一定返回 null
             * 但是这里为 BitmapFactory.Options 设置了 Bitmap 的数据参数
             * 所以下面能拿到 Bitmap 的实际宽高
             */
        BitmapFactory.decodeByteArray(data, 0, data.length, decodeOptions);
        // 记录该 Bitmap 的实际宽度
        int actualWidth = decodeOptions.outWidth;
        // 记录该 Bitmap 的实际高度
        int actualHeight = decodeOptions.outHeight;
        // Then compute the dimensions we would ideally like to decode to.
        // 根据 最宽高、Bitmap 实际宽高 以及 ImageView.ScaleType,计算出 需求宽度
        int desiredWidth = getResizedDimension(mMaxWidth, mMaxHeight, actualWidth, actualHeight, mScaleType);
        // 根据 最宽高、Bitmap 实际宽高 以及 ImageView.ScaleType,计算出 需求高度
        int desiredHeight = getResizedDimension(mMaxHeight, mMaxWidth, actualHeight, actualWidth, mScaleType);
        // Decode to the nearest power of two scaling factor.
        // 关闭 inJustDecodeBounds,因为以下要进行真实的 Bitmap 内存申请
        decodeOptions.inJustDecodeBounds = false;
        // TODO(ficus): Do we need this or is it okay since API 8 doesn't support it?
        // decodeOptions.inPreferQualityOverSpeed = PREFER_QUALITY_OVER_SPEED;
        /*
             * 计算缩放比例
             * 如果 BitmapFactory.Options.inSampleSize = 4,那么宽高为 原 Bitmap 的 1/4
             */
        decodeOptions.inSampleSize = findBestSampleSize(actualWidth, actualHeight, desiredWidth, desiredHeight);
        // 解析出 测试 Bitmap
        Bitmap tempBitmap = BitmapFactory.decodeByteArray(data, 0, data.length, decodeOptions);
        /*
             * 上面解析出的 测试 Bitmap
             * 如果 测试 Bitmap 的宽高 超过 需求宽高
             * 重新 根据 需求宽高 再拿 测试 Bitmap 解析一遍
             * 得到 最终 Bitmap 返回
             */
        if (tempBitmap != null && (tempBitmap.getWidth() > desiredWidth || tempBitmap.getHeight() > desiredHeight)) {
            bitmap = Bitmap.createScaledBitmap(tempBitmap, desiredWidth, desiredHeight, true);
            tempBitmap.recycle();
        } else {
            bitmap = tempBitmap;
        }
    }
    /*
         * 没有解析出的 Bitmap,调用错误回调,回调一个 ParseError
         * 有解析出的 Bitmap,调用 解析结果数据 的回调接口,回调 Bitmap
         */
    if (bitmap == null) {
        return Response.error(new ParseError(response));
    } else {
        return Response.success(bitmap, HttpHeaderParser.parseCacheHeaders(response));
    }
}
Also used : Bitmap(android.graphics.Bitmap) ParseError(com.android.volley.ParseError) BitmapFactory(android.graphics.BitmapFactory)

Example 7 with ParseError

use of com.android.volley.ParseError in project TaEmCasa by Dionen.

the class ImageRequest method doParse.

/**
 * The real guts of parseNetworkResponse. Broken out for readability.
 */
private Response<Bitmap> doParse(NetworkResponse response) {
    byte[] data = response.data;
    BitmapFactory.Options decodeOptions = new BitmapFactory.Options();
    Bitmap bitmap = null;
    if (mMaxWidth == 0 && mMaxHeight == 0) {
        decodeOptions.inPreferredConfig = mDecodeConfig;
        bitmap = BitmapFactory.decodeByteArray(data, 0, data.length, decodeOptions);
    } else {
        // If we have to resize this image, first get the natural bounds.
        decodeOptions.inJustDecodeBounds = true;
        BitmapFactory.decodeByteArray(data, 0, data.length, decodeOptions);
        int actualWidth = decodeOptions.outWidth;
        int actualHeight = decodeOptions.outHeight;
        // Then compute the dimensions we would ideally like to decode to.
        int desiredWidth = getResizedDimension(mMaxWidth, mMaxHeight, actualWidth, actualHeight, mScaleType);
        int desiredHeight = getResizedDimension(mMaxHeight, mMaxWidth, actualHeight, actualWidth, mScaleType);
        // Decode to the nearest power of two scaling factor.
        decodeOptions.inJustDecodeBounds = false;
        // TODO(ficus): Do we need this or is it okay since API 8 doesn't support it?
        // decodeOptions.inPreferQualityOverSpeed = PREFER_QUALITY_OVER_SPEED;
        decodeOptions.inSampleSize = findBestSampleSize(actualWidth, actualHeight, desiredWidth, desiredHeight);
        Bitmap tempBitmap = BitmapFactory.decodeByteArray(data, 0, data.length, decodeOptions);
        // If necessary, scale down to the maximal acceptable size.
        if (tempBitmap != null && (tempBitmap.getWidth() > desiredWidth || tempBitmap.getHeight() > desiredHeight)) {
            bitmap = Bitmap.createScaledBitmap(tempBitmap, desiredWidth, desiredHeight, true);
            tempBitmap.recycle();
        } else {
            bitmap = tempBitmap;
        }
    }
    if (bitmap == null) {
        return Response.error(new ParseError(response));
    } else {
        return Response.success(bitmap, HttpHeaderParser.parseCacheHeaders(response));
    }
}
Also used : Bitmap(android.graphics.Bitmap) ParseError(com.android.volley.ParseError) BitmapFactory(android.graphics.BitmapFactory)

Example 8 with ParseError

use of com.android.volley.ParseError in project iosched by google.

the class ImageRequest method doParse.

/**
 * The real guts of parseNetworkResponse. Broken out for readability.
 */
private Response<Bitmap> doParse(NetworkResponse response) {
    byte[] data = response.data;
    BitmapFactory.Options decodeOptions = new BitmapFactory.Options();
    Bitmap bitmap = null;
    if (mMaxWidth == 0 && mMaxHeight == 0) {
        decodeOptions.inPreferredConfig = mDecodeConfig;
        bitmap = BitmapFactory.decodeByteArray(data, 0, data.length, decodeOptions);
    } else {
        // If we have to resize this image, first get the natural bounds.
        decodeOptions.inJustDecodeBounds = true;
        BitmapFactory.decodeByteArray(data, 0, data.length, decodeOptions);
        int actualWidth = decodeOptions.outWidth;
        int actualHeight = decodeOptions.outHeight;
        // Then compute the dimensions we would ideally like to decode to.
        int desiredWidth = getResizedDimension(mMaxWidth, mMaxHeight, actualWidth, actualHeight);
        int desiredHeight = getResizedDimension(mMaxHeight, mMaxWidth, actualHeight, actualWidth);
        // Decode to the nearest power of two scaling factor.
        decodeOptions.inJustDecodeBounds = false;
        // TODO(ficus): Do we need this or is it okay since API 8 doesn't support it?
        // decodeOptions.inPreferQualityOverSpeed = PREFER_QUALITY_OVER_SPEED;
        decodeOptions.inSampleSize = findBestSampleSize(actualWidth, actualHeight, desiredWidth, desiredHeight);
        Bitmap tempBitmap = BitmapFactory.decodeByteArray(data, 0, data.length, decodeOptions);
        // If necessary, scale down to the maximal acceptable size.
        if (tempBitmap != null && (tempBitmap.getWidth() > desiredWidth || tempBitmap.getHeight() > desiredHeight)) {
            bitmap = Bitmap.createScaledBitmap(tempBitmap, desiredWidth, desiredHeight, true);
            tempBitmap.recycle();
        } else {
            bitmap = tempBitmap;
        }
    }
    if (bitmap == null) {
        return Response.error(new ParseError(response));
    } else {
        return Response.success(bitmap, HttpHeaderParser.parseCacheHeaders(response));
    }
}
Also used : Bitmap(android.graphics.Bitmap) ParseError(com.android.volley.ParseError) BitmapFactory(android.graphics.BitmapFactory)

Example 9 with ParseError

use of com.android.volley.ParseError in project DesignLibrary by StylingAndroid.

the class RssRequest method parseNetworkResponse.

@Override
protected Response<Articles> parseNetworkResponse(NetworkResponse response) {
    InputStream inputStream = new ByteArrayInputStream(response.data);
    SaRssParser parser = SaRssParser.newInstance(inputStream);
    Articles articles;
    try {
        articles = parser.parse();
    } catch (Exception e) {
        return Response.error(new ParseError(e));
    }
    return Response.success(articles, HttpHeaderParser.parseCacheHeaders(response));
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Articles(com.stylingandroid.designlibrary.rss.model.Articles) ParseError(com.android.volley.ParseError)

Example 10 with ParseError

use of com.android.volley.ParseError in project saga-android by AnandChowdhary.

the class ImageRequest method doParse.

/**
 * The real guts of parseNetworkResponse. Broken out for readability.
 */
private Response<Bitmap> doParse(NetworkResponse response) {
    byte[] data = response.data;
    BitmapFactory.Options decodeOptions = new BitmapFactory.Options();
    Bitmap bitmap = null;
    if (mMaxWidth == 0 && mMaxHeight == 0) {
        decodeOptions.inPreferredConfig = mDecodeConfig;
        bitmap = BitmapFactory.decodeByteArray(data, 0, data.length, decodeOptions);
    } else {
        // If we have to resize this image, first get the natural bounds.
        decodeOptions.inJustDecodeBounds = true;
        BitmapFactory.decodeByteArray(data, 0, data.length, decodeOptions);
        int actualWidth = decodeOptions.outWidth;
        int actualHeight = decodeOptions.outHeight;
        // Then compute the dimensions we would ideally like to decode to.
        int desiredWidth = getResizedDimension(mMaxWidth, mMaxHeight, actualWidth, actualHeight, mScaleType);
        int desiredHeight = getResizedDimension(mMaxHeight, mMaxWidth, actualHeight, actualWidth, mScaleType);
        // Decode to the nearest power of two scaling factor.
        decodeOptions.inJustDecodeBounds = false;
        // TODO(ficus): Do we need this or is it okay since API 8 doesn't support it?
        // decodeOptions.inPreferQualityOverSpeed = PREFER_QUALITY_OVER_SPEED;
        decodeOptions.inSampleSize = findBestSampleSize(actualWidth, actualHeight, desiredWidth, desiredHeight);
        Bitmap tempBitmap = BitmapFactory.decodeByteArray(data, 0, data.length, decodeOptions);
        // If necessary, scale down to the maximal acceptable size.
        if (tempBitmap != null && (tempBitmap.getWidth() > desiredWidth || tempBitmap.getHeight() > desiredHeight)) {
            bitmap = Bitmap.createScaledBitmap(tempBitmap, desiredWidth, desiredHeight, true);
            tempBitmap.recycle();
        } else {
            bitmap = tempBitmap;
        }
    }
    if (bitmap == null) {
        return Response.error(new ParseError(response));
    } else {
        return Response.success(bitmap, HttpHeaderParser.parseCacheHeaders(response));
    }
}
Also used : Bitmap(android.graphics.Bitmap) ParseError(com.android.volley.ParseError) BitmapFactory(android.graphics.BitmapFactory)

Aggregations

ParseError (com.android.volley.ParseError)11 Bitmap (android.graphics.Bitmap)7 BitmapFactory (android.graphics.BitmapFactory)7 UnsupportedEncodingException (java.io.UnsupportedEncodingException)3 JSONException (org.json.JSONException)2 JSONObject (org.json.JSONObject)2 LinearLayoutManager (android.support.v7.widget.LinearLayoutManager)1 NetworkResponse (com.android.volley.NetworkResponse)1 Response (com.android.volley.Response)1 VolleyError (com.android.volley.VolleyError)1 JsonObjectRequest (com.android.volley.toolbox.JsonObjectRequest)1 Gson (com.google.gson.Gson)1 JsonParseException (com.google.gson.JsonParseException)1 PeopleInSpaceAdapter (com.restart.spacestationtracker.adapter.PeopleInSpaceAdapter)1 Astronaut (com.restart.spacestationtracker.data.Astronaut)1 Articles (com.stylingandroid.designlibrary.rss.model.Articles)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 ArrayList (java.util.ArrayList)1 JSONArray (org.json.JSONArray)1