1

我是 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)

提前致谢

4

0 回答 0