开发者

MonoDroid drawable.createfromstream

I'm reading an image fro开发者_如何学编程m my sqlite db into a MemoryStream. I seek to the beginning of my memory stream and then pass it to the Drawable.CreateFromStream and it crashes. I've checked the stream contains correct data by saving it to file and opening the file.

can't find any samples on how to load an image that is not in resources into an imageView.

any help?


private static InputStream fetch(String urlString)
        throws MalformedURLException, IOException {

    conn = (HttpURLConnection)(new URL(urlString)).openConnection();
    conn.setDoInput(true);
    conn.connect();
    DefaultHttpClient httpClient = new DefaultHttpClient();
    HttpGet request = new HttpGet(urlString);
    HttpResponse response = httpClient.execute(request);
    return conn.getInputStream();
}

Resources res;
// ***********New Function For Fetching
public static Drawable LoadImage(String URL, BitmapFactory.Options options) {
    Drawable drawable = null; Bitmap bm = null;
    try {
        if (fetch(URL) != null) {
            try {
                InputStream obj = (InputStream)fetch(URL);
                bm = BitmapFactory.decodeStream(obj,null,options);
                obj.close();
                conn.disconnect();
            } catch(OutOfMemoryError e){}
            catch(IndexOutOfBoundsException e){}
            drawable = new BitmapDrawable(bm);
            return drawable;
        } else {
            bitmap = BitmapFactory.decodeResource(
                        DefaultDisplay.mContext.getResources(),
                        R.drawable.profileplaceholder);
            drawable = new BitmapDrawable(bitmap);
            return drawable;
        }
    } catch (Exception e) {
        Log.e("GUIMethodClasas", "fetchDrawable failed", e);
        return null;
    }
}

This is a function for fetching a image from a URL. In this function I fetch data from a URL and put it into InputStream by the Fetch function then convert this stream into Drawable and Bitmap object.

In your case you should read an image from the SQLite db and put into the input stream then create image from stream just like I have done in the above function.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜