2012-03-28 121 views
1

在我的应用程序中,我正在将图像上载到服务器上。在下面的代码中,我从可绘制文件夹上传图像。 但是我怎样才能从布局xml中的图像视图上传图像? 同样喜欢findviewbyid.imgid从android应用程序上传图片

我的布局名称是main.xml中和图像ID imgid

好心帮我...

Bitmap bitmap = 
BitmapFactory.decodeResource(getResources(),R.drawable.avatar);  
ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
bitmap.compress(Bitmap.CompressFormat.PNG, 90, stream); //compress to which format you want. 
byte [] byte_arr = stream.toByteArray(); 
String image_str = Base64.encodeBytes(byte_arr); 
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 

nameValuePairs.add(new BasicNameValuePair("image",image_str)); 

回答

2

我们可以通过上传图片Base64字符串和多部分实体

对于base64字符串

ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
     btMap.compress(Bitmap.CompressFormat.JPEG, 100, baos); // bm is the 
     byte[] b = baos.toByteArray(); 
     base64String = Base64.encodeBytes(b); 

和用于多

HttpPost httppost = new HttpPost("http://localhost:8080/upload.php"); 
File file = new File(yourimagepath); 

MultipartEntity mpEntity = new MultipartEntity(); 
ContentBody cbFile = new FileBody(file, "image/jpeg"); 
mpEntity.addPart("userfile", cbFile); 
httppost.setEntity(mpEntity); 
HttpResponse response = httpclient.execute(httppost); 
HttpEntity resEntity = response.getEntity(); 
+1

感谢名单krutik。但我想知道如何在图像视图中获取图像...? – CVD 2012-03-29 09:45:32

相关问题