Example usage for android.graphics BitmapFactory decodeStream

List of usage examples for android.graphics BitmapFactory decodeStream

Introduction

In this page you can find the example usage for android.graphics BitmapFactory decodeStream.

Prototype

public static Bitmap decodeStream(InputStream is) 

Source Link

Document

Decode an input stream into a bitmap.

Usage

From source file:com.v2soft.misto.Providers.MapnikProvider.java

@Override
public synchronized boolean prepareTileImage(TileInfo info) {
    try {/*from w  w w . ja  v a  2s. c o m*/
        String local_name = String.format("%d_%d_%d.png", info.getZoom(), info.getLongitude(),
                info.getLatitude());
        if (!mLocalCache.isFileInCache(local_name)) {
            String query = String.format("http://%s/%d/%d/%d%s", BASE_HOST, info.getZoom(), info.getLongitude(),
                    info.getLatitude(), IMAGE_EXT);
            Log.d("Uploading tile ", query);

            HttpParams httpParameters = new BasicHttpParams();
            // Set the timeout in milliseconds until a connection is established.
            int timeoutConnection = 3000;
            HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
            // Set the default socket timeout (SO_TIMEOUT) 
            // in milliseconds which is the timeout for waiting for data.
            int timeoutSocket = 5000;
            HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);

            DefaultHttpClient client = new DefaultHttpClient(httpParameters);

            HttpGet request = new HttpGet(query);

            HttpResponse response = client.execute(request);
            HttpEntity entity = response.getEntity();
            int code = response.getStatusLine().getStatusCode();
            if (code == 200) {
                Calendar cal = Calendar.getInstance();
                cal.add(Calendar.MONTH, 1);
                RandomAccessFile out = mLocalCache.addFile(local_name, cal.getTime());
                InputStream in = entity.getContent();
                byte[] buffer = new byte[4096];
                int readed = 0;
                while ((readed = in.read(buffer)) > 0) {
                    out.write(buffer, 0, readed);
                }
                out.close();
                in.close();
            }
        }
        InputStream in = mLocalCache.getFileInputStream(local_name);
        final Bitmap bitmap = BitmapFactory.decodeStream(in);
        BitmapManager.registerBitmap(bitmap, info.toString());
        info.setBitmap(bitmap);
        in.close();
        return true;
    } catch (Exception e) {
        Log.d("MapnikProvider::prepareTileImage", e.toString());
    }
    return false;
}

From source file:com.gh4a.utils.ImageDownloader.java

/**
 * Download bitmap2.//from   www  .  ja va  2s  . co  m
 * 
 * @param url the url
 * @return the bitmap
 */
static Bitmap downloadBitmap2(String url) {
    final AndroidHttpClient client = AndroidHttpClient.newInstance("Android");
    final HttpGet getRequest = new HttpGet(url);

    try {
        HttpResponse response = client.execute(getRequest);
        final int statusCode = response.getStatusLine().getStatusCode();
        if (statusCode != HttpStatus.SC_OK) {
            Log.w("ImageDownloader", "Error " + statusCode + " while retrieving bitmap from " + url);
            return null;
        }

        final HttpEntity entity = response.getEntity();
        if (entity != null) {
            InputStream inputStream = null;
            try {
                inputStream = entity.getContent();
                final Bitmap bitmap = BitmapFactory.decodeStream(new FlushedInputStream(inputStream));
                return bitmap;
            } finally {
                if (inputStream != null) {
                    inputStream.close();
                }
                entity.consumeContent();
            }
        }
    } catch (Exception e) {
        // Could provide a more explicit error message for IOException or
        // IllegalStateException
        getRequest.abort();
        Log.w("ImageDownloader", "Error while retrieving bitmap from " + url + e.toString());
    } finally {
        if (client != null) {
            client.close();
        }
    }
    return null;
}

From source file:com.android.datacloud.Entity.java

/**
  * Devuelve un BitmapDrawable del recurso en la aplicacin
  * //from  w  w  w. j a v  a  2  s.c o  m
  * @param name nombre del campo
  * @return valor (Tipo BitmapDrawable)
  */
public BitmapDrawable getBitmapDrawable(String name) {
    int id = DataCloud.getInstance().getContext().getResources().getIdentifier(
            DataCloud.getInstance().getPackage() + ":drawable/" + getValue(name).toString(), null, null);
    java.io.InputStream is = DataCloud.getInstance().getContext().getResources().openRawResource(id);
    BitmapDrawable bmd = new BitmapDrawable(BitmapFactory.decodeStream(is));
    return bmd;
}

From source file:org.mumod.util.ImageManager.java

public Bitmap fetchImage(String url) throws IOException {

    //   Log.d(TAG, "Fetching image: " + url);

    HttpGet get = new HttpGet(url);
    HttpConnectionParams.setConnectionTimeout(get.getParams(), CONNECTION_TIMEOUT_MS);
    HttpConnectionParams.setSoTimeout(get.getParams(), SOCKET_TIMEOUT_MS);

    HttpResponse response;//from ww  w .  j av  a 2s. co m

    try {
        response = mClient.execute(get);
    } catch (ClientProtocolException e) {
        if (MustardApplication.DEBUG)
            Log.e(TAG, e.getMessage(), e);
        throw new IOException("Invalid client protocol.");
    }

    if (response.getStatusLine().getStatusCode() != 200) {
        throw new IOException("Non OK response: " + response.getStatusLine().getStatusCode());
    }

    HttpEntity entity = response.getEntity();
    BufferedInputStream bis = new BufferedInputStream(entity.getContent(), 8 * 1024);
    Bitmap bitmap = BitmapFactory.decodeStream(bis);
    bis.close();

    return bitmap;
}

From source file:com.cloverstudio.spika.utils.BitmapManager.java

private Bitmap downloadBitmap(String url) {
    try {/*from  www  .j a  v a2  s  .c  om*/
        Bitmap bitmap = BitmapFactory.decodeStream(
                (InputStream) ConnectionHandler.httpGetRequest(url, UsersManagement.getLoginUser().getId()));
        imgRatio = (float) bitmap.getWidth() / (float) bitmap.getHeight();
        smallImg = false;

        //         int REQUIRED_SIZE = 100;
        //
        //         int width = 0;
        //         int height = 0;
        //
        //         if (bitmap.getHeight() < REQUIRED_SIZE
        //               && bitmap.getWidth() < REQUIRED_SIZE) {
        //            width = bitmap.getWidth();
        //            height = bitmap.getHeight();
        //            smallImg = true;
        //         } else if (bitmap.getHeight() < height) {
        //            height = bitmap.getHeight();
        //         } else if (bitmap.getWidth() < width) {
        //            width = bitmap.getWidth();
        //         }
        //
        //
        //         bitmap = Bitmap.createScaledBitmap(bitmap,
        //               (int) (height * imgRatio), height, true);
        cache.put(url, new SoftReference<Bitmap>(bitmap));
        return bitmap;
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (SpikaException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IllegalStateException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (SpikaForbiddenException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return null;
}

From source file:zjut.soft.finalwork.fragment.LeftFragment.java

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.left, null);

    iv1 = (ImageView) view.findViewById(R.id.left_iv1);
    iv2 = (ImageView) view.findViewById(R.id.left_iv2);
    iv3 = (ImageView) view.findViewById(R.id.left_iv3);
    iv4 = (ImageView) view.findViewById(R.id.left_iv4);
    iv5 = (ImageView) view.findViewById(R.id.left_iv5);
    headTV = (ImageView) view.findViewById(R.id.sliding_activity_head_view);
    headTV.setOnClickListener(new View.OnClickListener() {

        @Override//from ww  w  .  j av  a2s.  c  o  m
        public void onClick(View v) {
            Intent i = new Intent(getActivity(), BasicInfoUI.class);
            i.putExtra("myportrait", portraitBitmap);
            getActivity().startActivity(i);
            getActivity().overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
        }
    });
    TextView tv = (TextView) view.findViewById(R.id.sliding_activity_name_tv);
    YCApplication app = (YCApplication) this.getActivity().getApplication();
    tv.setText(app.get("name").toString()); // 
    //      portrait = (ImageView) view.findViewById(R.id.sliding_activity_head_view);

    final String url = ((YCApplication) getActivity().getApplication()).get("selectedIp")
            + Constant.portraitContext;

    new Thread(new Runnable() {

        @Override
        public void run() {
            HttpGet get = new HttpGet(url);

            try {
                DefaultHttpClient client = ((YCApplication) getActivity().getApplicationContext()).getClient();
                synchronized (client) {
                    HttpResponse response = client.execute(get);
                    HttpEntity entity = response.getEntity();
                    InputStream is = entity.getContent();

                    portraitBitmap = BitmapFactory.decodeStream(is); // 
                    is.close();
                    Message msg = mHandler.obtainMessage();
                    msg.obj = portraitBitmap;
                    mHandler.sendMessage(msg);
                }

            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }).start();

    TextView username = (TextView) view.findViewById(R.id.sliding_activity_username_textview);
    username.setText(app.get("username").toString()); // 
    userConfTV = (TextView) view.findViewById(R.id.sliding_activity_user_manage); // 
    payQueryTV = (TextView) view.findViewById(R.id.sliding_activity_pay_query); // 
    querySystemTV = (TextView) view.findViewById(R.id.sliding_activity_query_system); // 
    aboutUsTV = (TextView) view.findViewById(R.id.sliding_activity_about_us); // 
    unRegisterTV = (TextView) view.findViewById(R.id.sliding_activity_unregister_user); // 
    classNameTV = (TextView) view.findViewById(R.id.sliding_activity_class_tv); // 

    registTV = (TextView) view.findViewById(R.id.sliding_activity_regist_system); // 
    pickCourseTV = (TextView) view.findViewById(R.id.sliding_activity_pick_course_system); // 
    RatingTV = (TextView) view.findViewById(R.id.sliding_activity_student_rating); // 
    return view;
}

From source file:it.openyoureyes.test.panoramio.Panoramio.java

private Bitmap downloadFile(String url) {
    Bitmap bmImg = null;/*from ww  w. ja va 2 s . c  om*/
    URL myFileUrl = null;
    if (Util.isEmpty(url))
        return null;
    try {
        myFileUrl = new URL(url);
    } catch (MalformedURLException e) {

        e.printStackTrace();
    }
    try {
        HttpURLConnection conn = (HttpURLConnection) myFileUrl.openConnection();
        conn.setDoInput(true);
        conn.connect();
        InputStream is = conn.getInputStream();
        // BitmapFactory.Options op=new BitmapFactory.Options();
        // op.inSampleSize = 3;
        bmImg = BitmapFactory.decodeStream(is);// ,null,op);
        conn.disconnect();

    } catch (IOException e) {

        e.printStackTrace();
    }
    return bmImg;
}

From source file:net.dian1.player.download.DownloadTask.java

private static void downloadCover(DownloadJob job) {

    PlaylistEntry mPlaylistEntry = job.getPlaylistEntry();
    String mDestination = job.getDestination();
    String path = DownloadHelper.getAbsolutePath(mPlaylistEntry, mDestination);
    File file = new File(path + "/" + "cover.jpg");
    // check if cover already exists
    if (file.exists()) {
        Log.v(Dian1Application.TAG, "File exists - nothing to do");
        return;/*from w ww . j a  v  a  2  s  .c  o m*/
    }

    String albumUrl = mPlaylistEntry.getAlbum().getImage();
    if (albumUrl == null) {
        Log.w(Dian1Application.TAG, "album Url = null. This should not happened");
        return;
    }
    albumUrl = albumUrl.replace("1.100", "1.500");

    InputStream stream = null;
    URL imageUrl;
    Bitmap bmp = null;

    // download cover
    try {
        imageUrl = new URL(albumUrl);

        try {
            stream = imageUrl.openStream();
            bmp = BitmapFactory.decodeStream(stream);

        } catch (IOException e) {
            // TODO Auto-generated catch block
            Log.v(Dian1Application.TAG, "download Cover IOException");
            e.printStackTrace();
        }
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        Log.v(Dian1Application.TAG, "download CoverMalformedURLException");
        e.printStackTrace();
    }

    // save cover to album directory
    if (bmp != null) {

        try {
            file.createNewFile();
            OutputStream outStream = new FileOutputStream(file);
            bmp.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
            outStream.flush();
            outStream.close();

            Log.v(Dian1Application.TAG, "Album cover saved to sd");

        } catch (FileNotFoundException e) {
            Log.w(Dian1Application.TAG, "FileNotFoundException");

        } catch (IOException e) {
            Log.w(Dian1Application.TAG, "IOException");
        }

    }
}

From source file:com.gfan.sbbs.utils.images.ImageManager.java

/**
 * Downloads a file//from   w w w. ja  v a 2 s .c o  m
 * 
 * @param url
 * @return
 * @throws HttpException
 * @throws IOException 
 * @throws ResponseException 
 * @throws ClientProtocolException 
 */
public Bitmap downloadImage(String url) throws HttpException {
    Log.d(TAG, "Fetching image: " + url);
    InputStream inStream = BBSOperator.getInstance().get(url).asStream();

    return BitmapFactory.decodeStream(inStream);
}