码迷,mamicode.com
首页 > 移动开发 > 详细

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

时间:2014-12-23 10:15:22      阅读:205      评论:0      收藏:0      [点我收藏+]

标签:

解决方法:

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

测试结果:

技术分享

 

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

标签:

原文地址:http://www.cnblogs.com/yshyee/p/4179464.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!