Search in sources :

Example 11 with ParseError

use of com.android.volley.ParseError in project FastDev4Android by jiangqqlmj.

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