1

サーバーに画像をアップロードしようとしているアンドロイドは初めてです。インターネットからサンプルコードを入手しました。しかし、それを処理できない行にいくつかのエラーが表示されています。この場合、誰でも私を助けることができますか?コードを取得するリンクは http://blog.sptechnolab.com/2011/03/09/android/android-upload-image-to-server/です

エラーが発生しています

「メソッド encodeBytes(byte[]) は Base64 型に対して定義されていません」

および対応する スクリーンショットは

プロジェクトに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 エンコードし、結果とともに新しく割り当てられた文字列を返します。

パラメーター

input : エンコードするデータ

offset : 入力配列内の開始位置

len : エンコードする入力のバイト数

flags : エンコードされた出力の特定の機能を制御します。

DEFAULT を渡すと、RFC 2045 に準拠した出力が得られます。

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

以降: API レベル 8

指定されたデータを Base64 エンコードし、結果とともに新しく割り当てられた文字列を返します。

パラメーター

input : エンコードするデータ

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();
        }
        }

読むだけです。1 週間で何かを変更したい場合、プログラムはより速く理解できます。

インデントするEclipseでは、ctrl + aコード全体を選択してctrl + iからインデントします。

これはあなたの質問に答えるものではありませんが、他の人が答えるのに役立ち、あなたのスキルを向上させるのに役立ちます.

于 2011-09-14T08:16:09.737 に答える
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 に答える
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();

これは、ファイルを 1 行ずつ読み取るための実装です。改行のない画像のエンコーディングが与えられた場合に機能するかどうかはわかりませんが、問題なくバイト単位でストリーミングするように再設計できるはずです。

于 2011-09-14T08:25:51.600 に答える