如何在Android中将ImageView上传到webservice?

时间:2013-12-20 05:29:29

标签: android web-services

我在xml中有一个Image视图,如下所示。

<ImageView
        android:id="@+id/qrcode"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="15dp"
        android:layout_marginTop="86dp" />

在这张imageview中,我正在创建一个qr代码。我想将它发送到werservice。

public class GenerateQrcode extends ActivityGroup{
    String id;
    ImageView image;
     Bitmap bm;
     Button qrusers,qrinvites,qrback;
     @Override
    public void onBackPressed() {
        // TODO Auto-generated method stub

    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.qrcode);

     image = (ImageView) findViewById(R.id.qrcode);

我使用zxing库生成了qr代码,并将qr代码图像分配给上面的imageview。

所以内部图像我有qr代码。如何将此图像发送到webservice。

3 个答案:

答案 0 :(得分:0)

尝试以下示例

 public void upload(String filepath) throws IOException
 {
 HttpClient httpclient = new DefaultHttpClient();
 httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
 HttpPost httppost = new HttpPost("url");
 File file = new File(filepath);
 MultipartEntity mpEntity = new MultipartEntity();
 ContentBody cbFile = new FileBody(file, "image/jpeg");
 mpEntity.addPart("userfile", cbFile); 
 httppost.setEntity(mpEntity);
 System.out.println("executing request " + httppost.getRequestLine());
 HttpResponse response = httpclient.execute(httppost);
 HttpEntity resEntity = response.getEntity();
         // check the response and do what is required
  }

它可能对你有所帮助。

答案 1 :(得分:0)

首先从imageview获取位图:

Bitmap bitmap=image.getDrawingCache()

现在在AsyncTask中调用以下函数:

 public void executeMultipartPost(Bitmap bm) throws Exception {
        try {
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            bm.compress(CompressFormat.JPEG, 75, bos);
            byte[] data = bos.toByteArray();
            HttpClient httpClient = new DefaultHttpClient();
            HttpPost postRequest = new HttpPost("<YourURL>");//<----------URL
            ByteArrayBody bab = new ByteArrayBody(data, "forest.jpg");
            // File file= new File("/mnt/sdcard/forest.png");
            // FileBody bin = new FileBody(file);
            MultipartEntity reqEntity = new MultipartEntity(
                    HttpMultipartMode.BROWSER_COMPATIBLE);
            reqEntity.addPart("uploaded", bab);
            reqEntity.addPart("photoCaption", new StringBody("sfsdfsdf"));
            postRequest.setEntity(reqEntity);
            HttpResponse response = httpClient.execute(postRequest);
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    response.getEntity().getContent(), "UTF-8"));
            String sResponse;
            StringBuilder s = new StringBuilder();

            while ((sResponse = reader.readLine()) != null) {
                s = s.append(sResponse);
            }
            System.out.println("Response: " + s);
        } catch (Exception e) {
            // handle exception here
            Log.e(e.getClass().getName(), e.getMessage());
        }
    }

答案 2 :(得分:0)

您可以将图片视图转换为位图文件,

image.buildDrawingCache();
Bitmap bmp = imageView.getDrawingCache();

将其转换为字节数组

 ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();

通过您的网络服务发送此byteArray