Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/212.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在android中将ImageView上传到webservice?_Android_Web Services - Fatal编程技术网

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

如何在android中将ImageView上传到webservice?,android,web-services,Android,Web Services,我有一个xml格式的图像视图,如下所示 <ImageView android:id="@+id/qrcode" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true"

我有一个xml格式的图像视图,如下所示

<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" />
我已经使用zxing库生成了二维码,并将该二维码图像分配给上面的imageview

所以里面的图像我有二维码。如何将此图像发送到webservice。

请尝试以下示例

 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
  }

它可能会对您有所帮助。

首先通过以下方式从imageview中获取位图:

Bitmap bitmap=image.getDrawingCache()
现在在AsyncTask中调用以下函数:


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

image.buildDrawingCache();
Bitmap bmp = imageView.getDrawingCache();
将其转换为字节数组

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

通过您的Web服务发送此byteArray

您搜索过吗?是的,我搜索过,但教程正在从sd卡访问图像,但我不想使用sd卡,因为我在imageview中已经有图像,并希望将其上载到服务器。然后将图像保存到sd卡并使用该代码…我的图像不在sd卡中。如何将图像放入文件路径?首先,您必须将图像保存到SD卡中的临时文件夹中,然后获取文件路径。开始上载。使用AsynkTask会更好地获得良好的性能。请在此处进行编码。如何将此bytearray发送到Web服务?您使用的是哪种Web服务?FileInputStream FileInputStream=newFileInputStream在此添加什么?;File someFile=新文件名;FileOutputStream fos=新的FileOutputStreamsomeFile;fos.writebyteArray//从imageview fos.flush创建的字节数组;fos.close;最后,FileInputStream FileInputStream=newfileinputstreamsomefile;
image.buildDrawingCache();
Bitmap bmp = imageView.getDrawingCache();
 ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();