2012-02-29 10 views
3

私は完全に失われていますからHTTP POSTリクエストによってBase64でエンコードされた画像を送信し、私は、PHPのWebページにAndroidアプリを経由して写真を送信しようとしています。私はそれがうまく働いた単純な文字列ではなく重いファイルで試してみました、アンドロイド

私はポストデータを取得することができます...

Theoriticallyのすべてが右でなければなりませんが、先のデータが破損しているか、何かが私にはわかりませんデータが破損しているように見えます。

public class EncodingAndSending extends Thread{ 
    ShareOnMyWebSiteActivity mycontext; 
    ContentResolver cr; 
    Uri uri; 
    public Handler mainHandler,sHandler; 

    public EncodingAndSending(ShareOnMyWebSiteActivity myctxt,Handler mainone,Uri URI,ContentResolver crr){ 
     mycontext=myctxt; 
     cr=crr; 
     uri=URI; 
     mainHandler=mainone; 
     this.start(); 
    } 

     return buffer.toString().toUpperCase();  
     } 

    public void run(){ 

     InputStream is=null; 
     byte[] data=null; 
     try { 
      is = cr.openInputStream(uri); 
      // Get binary bytes for encode 
      data = getFileBytes(is); 
     } catch (Exception e1) { 
      // TODO Auto-generated catch block 
      e1.printStackTrace(); 
     } 

     String data_string=Base64.encodeToString(data,Base64.URL_SAFE); 


     if(data_string!=""){ 
      SendRequest(data_string); 
     } 
     else{ 

     } 

    } 

public byte[] getFileBytes(InputStream ios) throws IOException { 
      ByteArrayOutputStream ous = null; 
      //InputStream ios = null; 
      try { 
       byte[] buffer = new byte[4096]; 
       ous = new ByteArrayOutputStream(); 
       //ios = new FileInputStream(file); 
       int read = 0; 
       while ((read = ios.read(buffer)) != -1) 
        ous.write(buffer, 0, read); 
      } finally { 
       try { 
        if (ous != null) 
         ous.close(); 
       } catch (IOException e) { 
        // swallow, since not that important 
       } 
       try { 
        if (ios != null) 
         ios.close(); 
       } catch (IOException e) { 
        // swallow, since not that important 
       } 
      } 
      return ous.toByteArray(); 
     } 


    private void SendRequest(String data_string){ 

     HttpClient httpclient = new DefaultHttpClient(); 
     HttpPost httppost = new HttpPost("xxxxx.php"); 

     try { 

      List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); 
      nameValuePairs.add(new BasicNameValuePair("image", data_string)); 

      httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

      // Execute HTTP Post Request 
      HttpResponse response = httpclient.execute(httppost); 
      ResponseHandler<String> responseHandler=new BasicResponseHandler(); 
      String responseBody = httpclient.execute(httppost, responseHandler); 


     } catch (Exception e) { 
      // TODO Auto-generated catch block 
     }  
    } 

} 

EDIT:

これは動作します。画像をエンコードしてデコードしてプレビューできます。私はgetBytes()関数を使用しませんでしたが、そこから問題が発生するかどうかわかりません。

お知らせします。

super.onCreate(savedInstanceState); 
    setContentView(R.layout.test_image); 
    ImageView image = (ImageView) findViewById(R.id.imageView1); 
    FileInputStream in; 
    BufferedInputStream buf; 

    Intent intent = getIntent(); 
    Bundle extras = intent.getExtras(); 

    Uri uri = (Uri) extras.getParcelable(Intent.EXTRA_STREAM); 
    ContentResolver cr = getContentResolver(); 
    Bitmap bMap=null; 
    try { 
     InputStream is = cr.openInputStream(uri); 

     bMap = BitmapFactory.decodeStream(is); 

     if (is != null) { 
     is.close(); 
     } 
    } catch (Exception e) { 
     Log.e("Error reading file", e.toString()); 
    } 

    ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
    bMap.compress(Bitmap.CompressFormat.JPEG, 100, baos); 
    byte[] b = baos.toByteArray(); 
    String data_string=Base64.encodeToString(b,Base64.DEFAULT); 

    b=null;bMap=null; 
    b=Base64.decode(data_string, Base64.DEFAULT); 
    bMap=BitmapFactory.decodeByteArray(b, 0, b.length); 
    image.setImageBitmap(bMap); 
+0

あなたはより良いあなたがそのような 'byteArrayToHexString'として、あなたの例では使用していないコードを残すことができ、それだけで読者を混乱させる。 –

+0

それは関連性があるかどうかわかりませんでした。 – flo360

+0

メソッド 'getFileBytes'のコードを含めることができますか?多分それが原因だろうか? –

答えて

2

携帯電話のカメラで撮影した画像をデータベースに送ることができる小さなアプリを書いた。ここでは、HTTPWorkerがHTTPMETHODを構築するだけでasynctaskある

public void writeCommentForRestaurant(int id, String comment, String author, 
     Bitmap image) { 

    if (image != null) { 
     /* Get the image as string */ 
     // Normal 
     ByteArrayOutputStream full_stream = new ByteArrayOutputStream(); 
     image.compress(Bitmap.CompressFormat.PNG, 100, full_stream); 
     byte[] full_bytes = full_stream.toByteArray(); 
     String img_full = Base64.encodeToString(full_bytes, Base64.DEFAULT); 
     // Thumbnail 
     ByteArrayOutputStream thumb_stream = new ByteArrayOutputStream(); 
        // The getScaledBitmap method only minimizes the Bitmap to a small icon! 
     getScaledBitmap(image, 72).compress(Bitmap.CompressFormat.JPEG, 75, 
       thumb_stream); 
     byte[] thumb_bytes = thumb_stream.toByteArray(); 
     String img_thumbnail = Base64.encodeToString(thumb_bytes, Base64.DEFAULT); 

     // new HTTPWorker(ctx, mHandler, HTTPWorker.WRITE_COMMENT, true).execute(
     // Integer.toString(id), comment, author, img_thumbnail, img_full); 
    } else { 
     // new HTTPWorker(ctx, mHandler, HTTPWorker.WRITE_COMMENT, true).execute(
     // Integer.toString(id), comment, author, null, null); 
    } 
} 

...私は事を解決する方法です。

... 
/* Add arguments */ 
arguments.add(new BasicNameValuePair("idrestaurant", params[0])); 
arguments.add(new BasicNameValuePair("comment", params[1])); 
arguments.add(new BasicNameValuePair("author", params[2])); 
if (params.length > 3) { 
    arguments.add(new BasicNameValuePair("image", params[3])); 
    arguments.add(new BasicNameValuePair("bigimage", params[4])); 
} 
... 

次に、このようにサーバーに送信しました。

/** 
* Executes a httppost to a server instance with the given POST arguments 
* and returns a String response from the server. 
*/ 
private String httppost(String url, ArrayList<NameValuePair> args) { 
    /* Create the channel for communicaton */ 
    InputStream is = null; 

    /* Send request to server */ 
    try { 
    /* Create the POST */ 
    HttpClient httpclient = new DefaultHttpClient(); 
    HttpPost httppost = new HttpPost(url); 

    /* Add the login information "POST" variables in the php */ 
    httppost.setEntity(new UrlEncodedFormEntity(args)); 

    /* Execute the http POST and get the response */ 
    HttpResponse response = httpclient.execute(httppost); 
    HttpEntity entity = response.getEntity(); 
    is = entity.getContent(); 
    } catch (Exception e) { 
    Log.e(TAG, "Error in http connection " + e.toString()); 
    return null; 
    } 

    /* Read response from server */ 
    try { 
    /* Read the response stream */ 
    BufferedReader reader = new BufferedReader(new InputStreamReader(
    is, "iso-8859-1"), 8); 

    /* Copy the response to StringBuilder */ 
    StringBuilder sb = new StringBuilder(); 
    String line = null; 
    while ((line = reader.readLine()) != null) { 
     sb.append(line + "\n"); 
    } 
    is.close(); 

    /* Return the response as string */ 
    return sb.toString(); 
    } catch (Exception e) { 
    Log.e(TAG, "Error converting result " + e.toString()); 
    return null; 
    } 
} 
0

ありがとう。実際には、問題はサーバーからのものでした。 base64は、長い文字列型(mysql)のフィールドに文字列を格納するphpスクリプトに送られます。問題は、データを破損し、私はそれをblobに変更し、うまくいきました。典型的ではないですか?

ありがとうございました。

私の最初の話題と素晴らしいコミュニティがここにあります。

後で教えてください。