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:helloopengles.example.com.MyGLRenderer.java

public static Bitmap loadImage(String fileName) {
    try {/*from   w w  w  .  jav  a 2 s.c o m*/
        Bitmap tmp = BitmapFactory.decodeStream(OpenGLES20Activity.context.getAssets().open(fileName));
        android.graphics.Matrix matrix = new android.graphics.Matrix();
        matrix.preScale(1.0f, -1.0f);
        Bitmap image = Bitmap.createBitmap(tmp, 0, 0, tmp.getWidth(), tmp.getHeight(), matrix, true);
        tmp.recycle();
        return image;
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:com.lge.friendsCamera.CustomListAdapter.java

public CustomListAdapter(Activity context, ArrayList<HashMap<String, String>> itemInfo,
        ArrayList<Integer> thumbnailsId) {
    super(context, R.layout.customlistadapter_layout, itemInfo);
    mContext = context;//from w w w .  j  av a2s  . c o m
    mItemInfo = itemInfo;
    mBitmapId = thumbnailsId;

    try {
        //Set dummy image for thumbnail
        InputStream istr = mContext.getAssets().open("dummy.jpg");
        mDummyBitmap = VrUtils.scaleBitmap(BitmapFactory.decodeStream(istr), 300, 150);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:de.skubware.opentraining.db.rest.ExerciseImageGSONSerializer.java

@Override
public JsonElement serialize(ExerciseImage ex, Type typeOfSrc, JsonSerializationContext context) {

    JsonObject mainObject = new JsonObject();

    FileInputStream fis = null;/*from w w  w  .  j  a  v a  2 s.co  m*/
    String imgString = null;

    try {
        fis = new FileInputStream(ex.getRealImagePath());
    } catch (FileNotFoundException e) {
        Log.i(TAG, "File not found: " + ex.getRealImagePath());
        e.printStackTrace();
    }

    Bitmap bm = BitmapFactory.decodeStream(fis);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bm.compress(CompressFormat.JPEG, 100, baos);
    byte[] imgByte = baos.toByteArray();
    imgString = Base64.encodeToString(imgByte, Base64.DEFAULT);

    Log.i("Minion", "imgString -> JSONObject SUCCESS");
    mainObject.addProperty("image", imgString);

    mainObject.addProperty("license", 3);
    mainObject.addProperty("exercise", 260);

    return mainObject;

}

From source file:com.frostwire.android.util.ImageCache.java

private Bitmap diskGet(String key) {
    Bitmap bmp = null;/*from www  . j a  v a 2 s. c  o m*/

    if (disk != null) {
        try {
            Entry e = disk.get(key);
            if (e != null) {
                try {
                    bmp = BitmapFactory.decodeStream(e.getInputStream());
                } finally {
                    IOUtils.closeQuietly(e);
                }

                if (bmp == null) { // some error decoding
                    disk.remove(key);
                }
            }
        } catch (Throwable e) {
            // ignore
        }
    }

    return bmp;
}

From source file:com.android.dialer.lookup.yellowpages.YellowPagesReverseLookup.java

/**
 * Lookup image//from   w ww .  j  a  va  2  s . c o m
 *
 * @param context The application context
 * @param uri The image URI
 */
public Bitmap lookupImage(Context context, Uri uri) {
    if (uri == null) {
        throw new NullPointerException("URI is null");
    }

    Log.e(TAG, "Fetching " + uri);

    String scheme = uri.getScheme();

    if (scheme.startsWith("http")) {
        HttpClient client = new DefaultHttpClient();
        HttpGet request = new HttpGet(uri.toString());

        try {
            HttpResponse response = client.execute(request);

            int responseCode = response.getStatusLine().getStatusCode();

            ByteArrayOutputStream out = new ByteArrayOutputStream();
            response.getEntity().writeTo(out);
            byte[] responseBytes = out.toByteArray();

            if (responseCode == HttpStatus.SC_OK) {
                Bitmap bmp = BitmapFactory.decodeByteArray(responseBytes, 0, responseBytes.length);
                return bmp;
            }
        } catch (IOException e) {
            Log.e(TAG, "Failed to retrieve image", e);
        }
    } else if (scheme.equals(ContentResolver.SCHEME_CONTENT)) {
        try {
            ContentResolver cr = context.getContentResolver();
            Bitmap bmp = BitmapFactory.decodeStream(cr.openInputStream(uri));
            return bmp;
        } catch (FileNotFoundException e) {
            Log.e(TAG, "Failed to retrieve image", e);
        }
    }

    return null;
}

From source file:com.serloman.imagecachedownloader.task.DownloadImageAsyncTask.java

private Bitmap decodeImage(HttpEntity entity) throws IOException {
    InputStream inputStream = entity.getContent();

    Bitmap image = BitmapFactory.decodeStream(inputStream);

    if (inputStream != null)
        inputStream.close();/*from  w w  w  .j  a v a  2 s  .com*/

    entity.consumeContent();

    return image;
}

From source file:libraryjava.parseJSON.java

/**
 * Bitmap/*from  w  w w . j  a v  a  2 s  . c om*/
 * @param urll
 * @return Bitmap
 */
public Bitmap doInBackground(String urll) {
    StrictMode();

    try {

        URL url = new URL(urll);

        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoInput(true);
        connection.connect();

        InputStream input = connection.getInputStream();
        Bitmap myBitmap = BitmapFactory.decodeStream(input);

        return myBitmap;

    } catch (Exception ex) {
        return null;
    }
}

From source file:com.mousebird.maply.MaplyStarModel.java

public MaplyStarModel(String fileName, String imageName, Activity activity) throws IOException {
    AssetManager assetMgr = activity.getAssets();
    InputStream inputStream = null;
    String[] paths = assetMgr.list("maplystarmodel");
    for (String path : paths) {
        if (path.equals(imageName)) {
            //image
            BufferedInputStream bufferedInputStream = null;
            try {
                inputStream = assetMgr.open("maplystarmodel/" + path);
                bufferedInputStream = new BufferedInputStream(inputStream);
                image = BitmapFactory.decodeStream(bufferedInputStream);
            } finally {
                if (bufferedInputStream != null) {
                    try {
                        bufferedInputStream.close();
                    } catch (IOException e) {
                    }//from  ww w.  jav a2s. c om
                }
            }
        }
        if (path.equals(fileName)) {
            //data
            Matcher m;
            try {
                inputStream = assetMgr.open("maplystarmodel/" + path);
                String stars = IOUtils.toString(inputStream, Charset.defaultCharset());
                Pattern p = Pattern.compile("[-]?[0-9]*\\.?[0-9]+");
                m = p.matcher(stars);
            } finally {
                if (inputStream != null) {
                    try {
                        inputStream.close();
                    } catch (IOException e) {
                    }
                }
            }

            this.stars = new ArrayList<SingleStar>();
            if (m.groupCount() % 3 == 0) {
                int i = 0;
                SingleStar s = null;
                while (m.find()) {
                    switch (i) {
                    case 0:
                        s = new SingleStar();
                        s.ra = Float.valueOf(m.group());
                        i++;
                        break;
                    case 1:
                        s.dec = Float.valueOf(m.group());
                        i++;
                        break;
                    case 2:
                        s.mag = Float.valueOf(m.group());
                        this.stars.add(s);
                        i = 0;
                        break;
                    default:
                        break;
                    }
                }
            }
        }
    }
}

From source file:com.coodesoft.notee.MyImageGetter.java

@Override
public Drawable getDrawable(String source) {
    Drawable d = null;/*from  www .ja  va2  s .  com*/

    String strSrcLeft5 = source.substring(0, 5);

    // data
    if (strSrcLeft5.equalsIgnoreCase("data:")) {
        InputStream is = new ByteArrayInputStream(source.getBytes());

        //d = Drawable.createFromStream(is, null);
        //d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());

        //Bitmap dBitmap = BitmapFactory.decodeByteArray(data, 0, length);
        int nPosComma = source.indexOf(',');
        if (nPosComma > 0) {
            byte[] arrBuffer = Base64.decode(source.substring(nPosComma + 1), Base64.DEFAULT);
            //byte[] arrBuffer = Base64Coder.decode(source.substring(nPosComma + 1));
            Bitmap dBitmap = BitmapFactory.decodeByteArray(arrBuffer, 0, arrBuffer.length);
            d = new BitmapDrawable(dBitmap);
            d.setBounds(0, 0, dBitmap.getWidth(), dBitmap.getHeight());
        }

    } else {
        // url

        try {

            InputStream is = (InputStream) new URL(source).getContent();
            Bitmap dBitmap = BitmapFactory.decodeStream(is);
            if (dBitmap == null) {
                d = Resources.getSystem().getDrawable(android.R.drawable.picture_frame);
            } else {
                d = new BitmapDrawable(dBitmap);
                d.setBounds(0, 0, dBitmap.getWidth(), dBitmap.getHeight());
            }

            d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());

        } catch (MalformedURLException e) {

            e.printStackTrace();

        } catch (IOException e) {

            e.printStackTrace();

        }

        /*
         URLDrawable urlDrawable = new URLDrawable();
                
         // get the actual source
         ImageGetterAsyncTask asyncTask = 
        new ImageGetterAsyncTask( urlDrawable);
                
         asyncTask.execute(source);
                
         // return reference to URLDrawable where I will change with actual image from
         // the src tag
         return urlDrawable;
         */

    }

    return d;
}

From source file:Main.java

private static Bitmap loadBitmapFromAssets(Context context, String texturePath) {
    Bitmap loadedBitmap = null;/*from w w w  . ja v a2  s. c om*/
    try {
        InputStream ims = context.getAssets().open(texturePath);
        loadedBitmap = BitmapFactory.decodeStream(ims);
    } catch (IOException ex) {
        ex.printStackTrace();
    }
    return loadedBitmap;
}