Mega Code Archive

 
Categories / Android / 2D Graphics
 

Bitmap downloading, processing

/*  * Copyright 2011 Dmitry S. Vorobiev * *   Licensed under the Apache License, Version 2.0 (the "License"); *   you may not use this file except in compliance with the License. *   You may obtain a copy of the License at * *       http://www.apache.org/licenses/LICENSE-2.0 * *   Unless required by applicable law or agreed to in writing, software *   distributed under the License is distributed on an "AS IS" BASIS, *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *   See the License for the specific language governing permissions and *   limitations under the License. */ //package com.litecoding.classkit.util; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.URL; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.util.Log; /**  * This class contains utils for Bitmap downloading, processing etc.  *   * @author Dmitry S. Vorobiev  *  */ public class BitmapUtils {   public static int IO_BUFFER_SIZE = 1024 * 1024;      /**    * Downloads image and creates a Bitmap object    * @param url URL of image to be downloaded     * @return downloaded bitmap or null if error occurred    */   public static Bitmap loadBitmapFromUrl(String url)   {       Bitmap bitmap = null;              try       {         bitmap = loadBitmapFromUrl(new URL(url));       }       catch(Exception e)       {       }              return bitmap;   }      /**    * Downloads image and creates a Bitmap object    * @param url URL of image to be downloaded     * @return downloaded bitmap or null if error occurred    */   public static Bitmap loadBitmapFromUrl(URL url)   {     Bitmap bitmap = null;       InputStream in = null;       BufferedOutputStream out = null;       try {           in = new BufferedInputStream(url.openStream(), IO_BUFFER_SIZE);           final ByteArrayOutputStream dataStream = new ByteArrayOutputStream();           out = new BufferedOutputStream(dataStream, IO_BUFFER_SIZE);                      byte[] buf = new byte[1024];           while(in.available() > 0)           {             int cntBytes = in.read(buf);             out.write(buf, 0, cntBytes);           }           out.flush();           final byte[] data = dataStream.toByteArray();           BitmapFactory.Options options = new BitmapFactory.Options();           //options.inSampleSize = 1;           bitmap = BitmapFactory.decodeByteArray(data, 0, data.length,options);       }       catch (IOException e)       {       }       finally       {         try         {           in.close();                   out.close();         }         catch(IOException e)         {         }       }       return bitmap;   } }