Example usage for android.graphics.drawable Drawable createFromStream

List of usage examples for android.graphics.drawable Drawable createFromStream

Introduction

In this page you can find the example usage for android.graphics.drawable Drawable createFromStream.

Prototype

public static Drawable createFromStream(InputStream is, String srcName) 

Source Link

Document

Create a drawable from an inputstream

Usage

From source file:org.mozilla.gecko.GeckoApp.java

@Override
public boolean onPrepareOptionsMenu(Menu aMenu) {
    Iterator<ExtraMenuItem> i = sExtraMenuItems.iterator();
    while (i.hasNext()) {
        final ExtraMenuItem item = i.next();
        if (aMenu.findItem(item.id) == null) {
            final MenuItem mi = aMenu.add(Menu.NONE, item.id, Menu.NONE, item.label);
            if (item.icon != null) {
                if (item.icon.startsWith("data")) {
                    byte[] raw = GeckoAppShell.decodeBase64(item.icon.substring(22),
                            GeckoAppShell.BASE64_DEFAULT);
                    Bitmap bitmap = BitmapFactory.decodeByteArray(raw, 0, raw.length);
                    BitmapDrawable drawable = new BitmapDrawable(bitmap);
                    mi.setIcon(drawable);
                } else if (item.icon.startsWith("jar:") || item.icon.startsWith("file://")) {
                    GeckoAppShell.getHandler().post(new Runnable() {
                        public void run() {
                            try {
                                URL url = new URL(item.icon);
                                InputStream is = (InputStream) url.getContent();
                                Drawable drawable = Drawable.createFromStream(is, "src");
                                mi.setIcon(drawable);
                            } catch (Exception e) {
                                Log.w(LOGTAG, "onPrepareOptionsMenu: Unable to set icon", e);
                            }//w  w w .  j  a v a2s.  c  o  m
                        }
                    });
                }
            }
            mi.setOnMenuItemClickListener(item);
        }
    }

    if (!sIsGeckoReady)
        aMenu.findItem(R.id.settings).setEnabled(false);

    Tab tab = Tabs.getInstance().getSelectedTab();
    MenuItem bookmark = aMenu.findItem(R.id.bookmark);
    MenuItem forward = aMenu.findItem(R.id.forward);
    MenuItem share = aMenu.findItem(R.id.share);
    MenuItem saveAsPDF = aMenu.findItem(R.id.save_as_pdf);
    MenuItem charEncoding = aMenu.findItem(R.id.char_encoding);

    if (tab == null) {
        bookmark.setEnabled(false);
        forward.setEnabled(false);
        share.setEnabled(false);
        saveAsPDF.setEnabled(false);
        return true;
    }

    bookmark.setEnabled(true);
    bookmark.setCheckable(true);

    if (tab.isBookmark()) {
        bookmark.setChecked(true);
        bookmark.setIcon(R.drawable.ic_menu_bookmark_remove);
    } else {
        bookmark.setChecked(false);
        bookmark.setIcon(R.drawable.ic_menu_bookmark_add);
    }

    forward.setEnabled(tab.canDoForward());

    // Disable share menuitem for about:, chrome: and file: URIs
    String scheme = Uri.parse(tab.getURL()).getScheme();
    boolean enabled = scheme != null
            && !(scheme.equals("about") || scheme.equals("chrome") || scheme.equals("file"));
    share.setEnabled(enabled);

    // Disable save as PDF for about:home and xul pages
    saveAsPDF.setEnabled(!(tab.getURL().equals("about:home")
            || tab.getContentType().equals("application/vnd.mozilla.xul+xml")));

    charEncoding.setVisible(GeckoPreferences.getCharEncodingState());

    return true;
}

From source file:com.adflake.AdFlakeManager.java

/**
 * Fetch image with the specified url./*from w  ww .ja  v a 2 s. c om*/
 * 
 * @param urlString
 *            the url string
 * @return the drawable
 */
private Drawable fetchImageWithURL(String urlString) {
    try {
        URL url = new URL(urlString);
        InputStream is = (InputStream) url.getContent();
        Drawable d = Drawable.createFromStream(is, "src");
        return d;
    } catch (Exception e) {
        Log.e(AdFlakeUtil.ADFLAKE, "Unable to fetchImage(): ", e);
        return null;
    }
}

From source file:android.support.v7.widget.SuggestionsAdapter.java

/**
 * Gets a drawable by URI, without using the cache.
 *
 * @return A drawable, or {@code null} if the drawable could not be loaded.
 *//*from   www.ja  va 2  s  .c  o  m*/
private Drawable getDrawable(Uri uri) {
    try {
        String scheme = uri.getScheme();
        if (ContentResolver.SCHEME_ANDROID_RESOURCE.equals(scheme)) {
            // Load drawables through Resources, to get the source density information
            try {
                return getDrawableFromResourceUri(uri);
            } catch (Resources.NotFoundException ex) {
                throw new FileNotFoundException("Resource does not exist: " + uri);
            }
        } else {
            // Let the ContentResolver handle content and file URIs.
            InputStream stream = mProviderContext.getContentResolver().openInputStream(uri);
            if (stream == null) {
                throw new FileNotFoundException("Failed to open " + uri);
            }
            try {
                return Drawable.createFromStream(stream, null);
            } finally {
                try {
                    stream.close();
                } catch (IOException ex) {
                    Log.e(LOG_TAG, "Error closing icon stream for " + uri, ex);
                }
            }
        }
    } catch (FileNotFoundException fnfe) {
        Log.w(LOG_TAG, "Icon not found: " + uri + ", " + fnfe.getMessage());
        return null;
    }
}

From source file:cm.aptoide.com.actionbarsherlock.widget.SuggestionsAdapter.java

/**
 * Gets a drawable by URI, without using the cache.
 *
 * @return A drawable, or {@code null} if the drawable could not be loaded.
 *//*from w  w w  .j  ava 2 s . c o m*/
private Drawable getDrawable(Uri uri) {
    try {
        String scheme = uri.getScheme();
        if (ContentResolver.SCHEME_ANDROID_RESOURCE.equals(scheme)) {
            // Load drawables through Resources, to get the source density information
            try {
                return getTheDrawable(uri);
            } catch (Resources.NotFoundException ex) {
                throw new FileNotFoundException("Resource does not exist: " + uri);
            }
        } else {
            // Let the ContentResolver handle content and file URIs.
            InputStream stream = mProviderContext.getContentResolver().openInputStream(uri);
            if (stream == null) {
                throw new FileNotFoundException("Failed to open " + uri);
            }
            try {
                return Drawable.createFromStream(stream, null);
            } finally {
                try {
                    stream.close();
                } catch (IOException ex) {
                    Log.e(LOG_TAG, "Error closing icon stream for " + uri, ex);
                }
            }
        }
    } catch (FileNotFoundException fnfe) {
        Log.w(LOG_TAG, "Icon not found: " + uri + ", " + fnfe.getMessage());
        return null;
    }
}

From source file:net.gaast.giggity.Schedule.java

private Drawable getIconDrawable(Fetcher f) {
    return Drawable.createFromStream(f.getStream(), getIcon());
}

From source file:com.tandong.sa.sherlock.widget.SuggestionsAdapter.java

/**
 * Gets a drawable by URI, without using the cache.
 * // w  w w.j a v a 2  s  .c o m
 * @return A drawable, or {@code null} if the drawable could not be loaded.
 */
private Drawable getDrawable(Uri uri) {
    try {
        String scheme = uri.getScheme();
        if (ContentResolver.SCHEME_ANDROID_RESOURCE.equals(scheme)) {
            // Load drawables through Resources, to get the source density
            // information
            try {
                return getTheDrawable(uri);
            } catch (Resources.NotFoundException ex) {
                throw new FileNotFoundException("Resource does not exist: " + uri);
            }
        } else {
            // Let the ContentResolver handle content and file URIs.
            InputStream stream = mProviderContext.getContentResolver().openInputStream(uri);
            if (stream == null) {
                throw new FileNotFoundException("Failed to open " + uri);
            }
            try {
                return Drawable.createFromStream(stream, null);
            } finally {
                try {
                    stream.close();
                } catch (IOException ex) {
                    Log.e(LOG_TAG, "Error closing icon stream for " + uri, ex);
                }
            }
        }
    } catch (FileNotFoundException fnfe) {
        Log.w(LOG_TAG, "Icon not found: " + uri + ", " + fnfe.getMessage());
        return null;
    }
}

From source file:org.cryptsecure.Utility.java

/**
 * Load drawable image from a BASE64 encoded String.
 * /*ww w  . ja  v  a  2  s.co m*/
 * @param encodedImage
 *            the encoded image
 * @return the drawable
 */
public static Drawable loadDrawableFromBASE64String(String encodedImage) {
    byte[] imageBytes = Base64.decode(encodedImage.getBytes(), Base64.DEFAULT);
    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(imageBytes);
    Drawable drawable = Drawable.createFromStream(byteArrayInputStream, "attachment");
    return drawable;
}

From source file:es.javocsoft.android.lib.toolbox.ToolBox.java

/**
 * Grabs an image direct from a URL into a Drawable without saving a cache
 * /*from   w w  w.ja  v  a 2s  .c o  m*/
 * @param urlImage
 * @param src_name
 * @return
 * @throws Exception
 */
public static Drawable media_getDrawableFromNet(String urlImage, String src_name) throws Exception {
    Drawable res = null;

    try {
        InputStream is = ((InputStream) new URL(urlImage).getContent());
        res = Drawable.createFromStream(is, src_name);
        is.close();
    } catch (MalformedURLException e) {
        if (LOG_ENABLE)
            Log.e(TAG, "media_generateBitmapFromFile() - MalformedURLException: " + e.getMessage(), e);

        throw new Exception(TAG + "[media_generateBitmapFromFile() - MalformedURLException]: " + e.getMessage(),
                e);
    } catch (IOException e) {
        if (LOG_ENABLE)
            Log.e(TAG, "media_generateBitmapFromFile() - IOException: " + e.getMessage(), e);

        throw new Exception(TAG + "[media_generateBitmapFromFile() - IOException]: " + e.getMessage(), e);
    } catch (Exception e) {
        if (LOG_ENABLE)
            Log.e(TAG, "media_generateBitmapFromFile(): " + e.getMessage(), e);

        throw new Exception(TAG + "[media_generateBitmapFromFile()]: " + e.getMessage(), e);
    }

    return res;
}