将图像从Android手机上传到服务器

我是android的新手,试图将图像上传到服务器。 从互联网上获得了一些示例代码。 但是它显示出一些无法处理它的错误。 在这种情况下,任何人都可以帮助我。 获取代码的链接是http://blog.sptechnolab.com/2011/03/09/android/android-upload-image-to-server/

而错误就是

“对于Base64类型,方法encodeBytes(byte [])是未定义的”

和相应的 屏幕截图是

我甚至在项目中下载了base64.java文件

API中没有encodeBytes 。 使用encodeToString

您可以改用这些方法

 public static String encodeToString (byte[] input, int offset, int len, int flags) 

自:API等级8

Base64对给定数据进行编码,并返回一个新分配的String和结果。

参数

输入:要编码的数据

offset:输入数组中要启动的位置

len:要编码的输入字节数

flags:控制编码输出的某些function。

传递DEFAULT会导致输出符合RFC 2045。

 public static String encodeToString (byte[] input, int flags) 

自:API等级8

Base64对给定数据进行编码,并返回一个新分配的String和结果。

参数

输入:要编码的数据

flags:控制编码输出的某些function。

传递DEFAULT会导致输出符合RFC 2045。

Whaaaa!

必须 indent你的代码!

对于每一个{你打开你应该向右空间,这样你就会更好地看到你的代码在做什么:

这很好:

  try { something(); } catch (Exception e) { weMessedUp(); if (e == i) { lol(); } } 

这是不好的:

  try { something(); } catch (Exception e) { weMessedUp(); if (e == i) { lol(); } } 

这只是为了阅读,如果在一周内你想要修改某些东西,你的程序会更快理解。

在eclipse中缩进,按ctrl + a选择你的整个代码,然后按ctrl + i缩进。

这不会回答你的问题,但会帮助别人回答你并提高你的技能。

您可以简单地将文件作为字节流打开并将其作为流发送到您的httpconnection?

像这样打开文件如下:

  File inFile = new File(fileName); BufferedReader br = new BufferedReader( new InputStreamReader( new FileInputStream(inFile) ) ); URL url = new URL("http://www.google.com"); URLConnection connection = url.openConnection(); connection.setDoOutput(true); OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream()); while ((decodedString = br.readLine()) != null) { out.write(decodedString); } out.close(); 

这是逐行读取文件的实现。 不确定它是否可以在没有换行符的情况下对图像进行编码,但是您应该能够重新编程以逐字节地流式传输而不会有太多麻烦。

 public class UploadImage extends Activity { InputStream inputStream; @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.main); Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.icon); 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 nameValuePairs = new ArrayList(); nameValuePairs.add(new BasicNameValuePair("image",image_str)); try{ HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost("http://10.0.2.2/Upload_image_ANDROID/upload_image.php"); httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); HttpResponse response = httpclient.execute(httppost); String the_string_response = convertResponseToString(response); Toast.makeText(UploadImage.this, "Response " + the_string_response, Toast.LENGTH_LONG).show(); }catch(Exception e){ Toast.makeText(UploadImage.this, "ERROR " + e.getMessage(), Toast.LENGTH_LONG).show(); System.out.println("Error in http connection "+e.toString()); } } public String convertResponseToString(HttpResponse response) throws IllegalStateException, IOException{ String res = ""; StringBuffer buffer = new StringBuffer(); inputStream = response.getEntity().getContent(); int contentLength = (int) response.getEntity().getContentLength(); //getting content length….. Toast.makeText(UploadImage.this, "contentLength : " + contentLength, Toast.LENGTH_LONG).show(); if (contentLength < 0){ } else{ byte[] data = new byte[512]; int len = 0; try { while (-1 != (len = inputStream.read(data)) ) { buffer.append(new String(data, 0, len)); //converting to string and appending to stringbuffer….. } } catch (IOException e) { e.printStackTrace(); } try { inputStream.close(); // closing the stream….. } catch (IOException e) { e.printStackTrace(); } res = buffer.toString(); // converting stringbuffer to string….. Toast.makeText(UploadImage.this, "Result : " + res, Toast.LENGTH_LONG).show(); //System.out.println("Response => " + EntityUtils.toString(response.getEntity())); } return res;