1

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

错误是

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

和相应的 屏幕截图是

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

4

5 回答 5

1

APIencodeBytes中没有。使用.encodeToString

于 2011-09-14T07:34:49.443 回答
1

您可以改用这些方法

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

自:API 8 级

Base64 编码给定数据并返回一个新分配的字符串和结果。

参数

输入:要编码的数据

offset : 输入数组中开始的位置

len : 要编码的输入字节数

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

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

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

自:API 8 级

Base64 编码给定数据并返回一个新分配的字符串和结果。

参数

输入:要编码的数据

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

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

于 2011-09-14T08:28:19.423 回答
0

哇!

必须 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缩进。

这不会回答您的问题,但会帮助其他人回答并提高您的技能。

于 2011-09-14T08:16:09.737 回答
0

您可以简单地将文件作为字节流打开并将其作为流发送到您的 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();

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

于 2011-09-14T08:25:51.600 回答
0
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 <span id="IL_AD5" class="IL_AD">stream</span> = 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));

        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;
于 2012-09-03T16:02:29.277 回答