图片以BLOB存储在后台数据库中,Android客户端要进行读取显示

解决方法:

1:在后台以InputStream的方式将图片从数据库中读出:

public static InputStream getPicInputStream(){
        String id = "f304733361e243779b2340afe20e62bf";
        
         Connection conn = JdbcUtil.getConnection();
         
         PreparedStatement ps = null;
         
         ResultSet rs = null;
         
         InputStream is = null;
         
         String sql = "select zp from pic_test where id= ?";
         
        try {
            
            ps = conn.prepareStatement(sql);
            
            ps.setString(1, id);
            
            rs = ps.executeQuery();
            
            if(rs.next()){
                is = rs.getBlob("zp").getBinaryStream();
            }
            
        } catch (SQLException ex) {
            Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
        }finally{
            try {
                if(null!=ps){
                    ps.close();
                }
            } catch (SQLException ex) {
                Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
            }
            try {
                if(null!=rs){
                    rs.close();
                }
            } catch (SQLException ex) {
                Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
            }
            JdbcUtil.release(conn);
        }
         
        
        return is;
    }

2:在Servlet中直接生成图片:

response.setContentType("image/jpeg");
        
        InputStream is = Test.getPicInputStream();
        
        if(null!=is){
           
            OutputStream os = response.getOutputStream();
            
            int len;
            
            byte buf[] = new byte[1024];
            
            while((len=is.read(buf))!=-1){
                os.write(buf, 0, len);
            }
            
            is.close();
            os.close();
        }

3:Android客户端的处理

public class MainActivity extends Activity {

    ImageView imgView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        imgView = (ImageView)findViewById(R.id.imgView);
        
        new ImgViewAsyncTask().execute("");
    }

    class ImgViewAsyncTask extends AsyncTask<String,String,String>{
        Bitmap bitmap = null;

        @Override
        protected void onPreExecute() {

        }

        @Override
        protected String doInBackground(String... strings) {
            InputStream is = null;
            String result = "";

            try {
                URL picURL = new URL("http://192.168.0.165:8084/wisdompm/PicServlet");
                HttpURLConnection conn = (HttpURLConnection)picURL.openConnection();
                conn.setConnectTimeout(10000);
                conn.connect();

                is = conn.getInputStream();
                if(null!=is){
                    bitmap = BitmapFactory.decodeStream(is);
                }
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                try {
                    if(null!=is){
                        is.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            return result;
        }

        @Override
        protected void onPostExecute(String s) {
            if(null!=bitmap && null!=imgView){
                imgView.setImageBitmap(bitmap);
            }
        }
    }
}

测试结果:

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值