0

我正在将我的 sqlite 数据库中的图像读取到 MemoryStream 中。我寻找我的内存流的开头,然后将它传递给 Drawable.CreateFromStream 并且它崩溃了。我通过将流保存到文件并打开文件来检查流是否包含正确的数据。

找不到任何关于如何将不在资源中的图像加载到 imageView 的示例。

有什么帮助吗?

4

1 回答 1

0
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;
    }
}

这是一个从 URL 获取图像的函数。在此函数中,我从 URL 获取数据并通过 Fetch 函数将其放入 InputStream,然后将此流转换为 Drawable 和 Bitmap 对象。

在您的情况下,您应该从 SQLite db 中读取图像并将其放入输入流中,然后像我在上述函数中所做的那样从流中创建图像。

于 2011-05-20T07:07:46.700 回答