2012-11-14 192 views
0

我google了很多,但它不工作。我發現了很多網站的信息,但我的應用程序崩潰的所有網站。我想打開的圖片是:「lastfile.png」。它存儲在內部存儲器中,所以我用openFileInput(「lastfile.png」)打開它;上傳圖片到服務器

我在AsyncTask中完成它。

這是到目前爲止我的代碼:

class PostTask extends AsyncTask<String, String, String>{ 
     @Override 
     protected String doInBackground(String... uri) { 
      if(picture == null) { 
       HttpClient httpclient = new DefaultHttpClient(); 
       HttpResponse response; 
       String responseString = null; 
       try { 
        response = httpclient.execute(new HttpGet(uri[0])); 
        StatusLine statusLine = response.getStatusLine(); 
        if(statusLine.getStatusCode() == HttpStatus.SC_OK){ 
         ByteArrayOutputStream out = new ByteArrayOutputStream(); 
         response.getEntity().writeTo(out); 
         out.close(); 
         responseString = out.toString(); 
        } else{ 
         response.getEntity().getContent().close(); 
         throw new IOException(statusLine.getReasonPhrase()); 
        } 
       } catch (ClientProtocolException e) { 
        Toast.makeText(AddStoryActivity.this, getResources().getString(R.string.error), Toast.LENGTH_LONG).show(); 
        e.printStackTrace(); 
       } catch (IOException e) { 
        Toast.makeText(AddStoryActivity.this, getResources().getString(R.string.error), Toast.LENGTH_LONG).show(); 
        e.printStackTrace(); 
       } 
       return responseString; 
      } else { 
       /* IMAGE UPLOAD */ 
      } 
      return ""; 
     } 

     @Override 
     protected void onPostExecute(String result) { 
      super.onPostExecute(result); 
      progress.cancel(); 
      Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show();  
     } 


    } 
+0

哪裏圖像數據上傳?這似乎更像是從服務器上卸載某些東西的方式。 對於圖片上傳,您應該創建一個HttpPost請求,並在您調用'httpClient.execute'之前,將要上傳的文件附加到它。 –

回答

0

的方式我這樣做是在壓縮IMG爲字符串類型,然後把它作爲名稱值對,然後使用PHP解碼在服務器端串。

位圖bitmapOrg = BitmapFactory.decodeResource(「您的設備上的圖像路徑」);

 ByteArrayOutputStream bao = new ByteArrayOutputStream(); 
     bitmapOrg.compress(Bitmap.CompressFormat.JPEG, 90, bao); 
     byte [] ba = bao.toByteArray(); 
     String ba1= Base64.encodeToString(ba, 0); 
     ArrayList<NameValuePair> nameValuePairs = new 
ArrayList<NameValuePair>(); 
      nameValuePairs.add(new BasicNameValuePair("image",ba1)); 

try{ 

       HttpClient httpclient = new DefaultHttpClient(); 
       HttpPost httppost = new HttpPost("http://your_url/sink.php"); 
       httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
       HttpResponse response = httpclient.execute(httppost); 
       HttpEntity entity = response.getEntity(); 
       is = entity.getContent(); 
      }catch(Exception e){ 

       Log.e("log_tag", "Error in http connection "+e.toString()); 
      } 

SINK.PHP

<?php 

$base=$_REQUEST['image']; 
$name=$_REQUEST['name']; 
echo $base; 
// base64 encoded utf-8 string 
$binary=base64_decode($base); 
// binary, utf-8 bytes 
header('Content-Type: bitmap; charset=utf-8'); 


$file = fopen(name, 'wb'); 
fwrite($file, $binary); 
fclose($file); 
echo '<img src='+name+'>'; 

?>