六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 22|回复: 0

Android Post图片和数据

[复制链接]

升级  13.33%

18

主题

18

主题

18

主题

秀才

Rank: 2

积分
70
 楼主| 发表于 2013-1-28 18:42:17 | 显示全部楼层 |阅读模式
调用照相
    private void imageClient(){        //        // 隐藏title//        this.requestWindowFeature(Window.FEATURE_NO_TITLE);//    //        // 设置全屏//        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);        fileName = UUID.randomUUID().toString();        try{            Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);            imgFile = new File(Environment.getExternalStorageDirectory(), fileName + ".jpg");            Uri outputFileUri = Uri.fromFile(imgFile);            cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);            startActivityForResult(cameraIntent, 10);        } catch (Exception ex) {            ex.printStackTrace();            Log.e("EP", "" + ex.getMessage());        }    }        @Override    protected void onActivityResult(int requestCode, int resultCode, Intent data){        // 父类方法        super.onActivityResult(requestCode, resultCode, data);                switch (resultCode) {            case RESULT_OK:                LogUtil.info("on Activity Result");                Bundle extras = data.getExtras();                b = (Bitmap) extras.get("data");                                new Thread(new Runnable(){                    public void run(){                        if (b != null) {                            try {                                BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(android.os.Environment.getExternalStorageDirectory() + "/test.jpg"));                                // 压缩图片                                b.compress(CompressFormat.JPEG, 75, bos);                                bos.flush();                                bos.close();                            } catch (Exception e) {                                Log.e("Exception", "file or compress exception");                                e.printStackTrace();                            }                                    }                        imgFile = new File(android.os.Environment.getExternalStorageDirectory() + "/" +fileName + ".jpg");                                                // ImgManager.resize(file, file, 200, "jpg");                                                // 发送到服务器                        if(!HttpUtil.postImg("http://neil.finalist.hk/namex/namex/nclient/upload", "user", "client", imgFile)){                            // 发送失败则重发一次                            HttpUtil.postImg("http://neil.finalist.hk/namex/namex/nclient/upload", "user", "client", imgFile);                        }                    }            }).start();        }    }
发送
@SuppressWarnings("deprecation")    public static boolean postImg(String url, String u, String c, File file){        LogUtil.info("" + file.exists());                PostMethod postMethod = new PostMethod(url);                Part[] part = new Part[4];        part[0] = new StringPart("u", u);        part[1] = new StringPart("c", c);        try {            part[2] = new FilePart("Filedata", file);        } catch (FileNotFoundException ex) {            ex.printStackTrace();            Log.e("file exception", ex.getMessage());        }        part[3] = new StringPart("flag", "image");                MultipartRequestEntity mrp = new MultipartRequestEntity(part, postMethod.getParams());        postMethod.setRequestEntity(mrp);        org.apache.commons.httpclient.HttpClient client = new org.apache.commons.httpclient.HttpClient();        client.getParams().setContentCharset("utf-8");        try {            client.executeMethod(postMethod);            if("false".equals(postMethod.getRequestEntity().toString())){                return false;            }        } catch (HttpException e) {            e.printStackTrace();            return false;        } catch (IOException e) {            e.printStackTrace();            return false;        }finally {            if (client != null) {                client = null;            }            if (postMethod != null) {                postMethod = null;            }        }        return true;    }
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表