Android:从MSSQL中检索图像

时间:2012-07-27 11:06:13

标签: android sql-server database bitmap bitmapfactory

我是Android的新手,并尝试从MSSQL获取图像并将其设置为背景。

                 CallableStatement proc = null;                 
                 String temp2 ="{ call AndroidDB.dbo.getStocks("+ temp +")}";
                 proc = MainActivity.conn.prepareCall(temp2);
                 ResultSet rs = proc.executeQuery();
                 Bitmap bitmap= null;
                 byte[] blob =null;
                 for (int i = 0,j=0; rs.next(); i++ ) {
                    if(i%3==0){
                        layout_stocks_h.add(new LinearLayout(this));
                        (layout_stocks_h.get(j)).setOrientation(LinearLayout.HORIZONTAL);
                        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT); // Verbose!
                        lp.weight = 1.0f;
                        (layout_stocks_h.get(j)).setLayoutParams(lp);
                        layout_stocks_v.addView((layout_stocks_h.get(j)));
                        (layout_stocks_h.get(j)).setId(200+(j));
                        j++;                    
                }
                imagebutton_stocks.add(new ImageButton(this));

                blob = rs.getBytes("stokresim");
                ByteArrayInputStream imageStream = new ByteArrayInputStream(blob);
                bitmap = BitmapFactory.decodeStream(imageStream);
                imagebutton_stocks.get(i).setImageBitmap(bitmap);

                imagebutton_stocks.get(i).setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
                layout_stocks_h.get(j-1).addView(imagebutton_stocks.get(i));
                imagebutton_stocks.get(i).setId(250+i);
                imagebutton_stocks.get(i).setOnClickListener(this);
                noOfStocks = i;

我也尝试过使用:

bitmap =  BitmapFactory.decodeByteArray(blob , 0, blob.length);

然而,没有任何作用,我的程序正常工作并创建正确数量的按钮,但没有图像。

我将图像文件存储在MSSQL中作为图像并尝试了varbinary(MAX)

先谢谢

0 个答案:

没有答案