2012-02-14 32 views
1
public class upload extends Activity { 
private static final int CAMERA_REQUEST = 1888; 
private ImageView imageView; 
String selectedPath = ""; 
TextView textTargetUri; 
ImageView targetImage; 
InputStream is; 
@Override 
public void onCreate(Bundle icicle) { 
    super.onCreate(icicle); 
    setContentView(R.layout.main); 

    this.imageView = (ImageView)this.findViewById(R.id.targetimage); 
    Button photoButton = (Button) this.findViewById(R.id.takeimage); 
    photoButton.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
      startActivityForResult(cameraIntent, CAMERA_REQUEST); 
     } 
    }); 

    Button buttonLoadImage = (Button)findViewById(R.id.loadimage); 
    textTargetUri = (TextView)findViewById(R.id.targeturi); 
    targetImage = (ImageView)findViewById(R.id.targetimage); // result gambar ditampilkan 

    buttonLoadImage.setOnClickListener(new Button.OnClickListener(){ 
     @Override 
     public void onClick(View arg0) { 
      Intent intent = new Intent(Intent.ACTION_PICK, 
      android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
      startActivityForResult(intent, 0); 
     }}); 
    } 

    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     super.onActivityResult(requestCode, resultCode, data); 

     if (requestCode == CAMERA_REQUEST) { 
       Bitmap photo = (Bitmap) data.getExtras().get("data"); 
       imageView.setImageBitmap(photo); 
      } 

     if (resultCode == RESULT_OK){ 
      Uri targetUri = data.getData(); 
      textTargetUri.setText(targetUri.toString()); 
      Bitmap bitmap; 
      try { 
       bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(targetUri)); 
       targetImage.setImageBitmap(bitmap); 
      } catch (FileNotFoundException e) { 
       e.printStackTrace(); 
      } 
     } 
    } 
} 

我已經設法從相機顯示圖片和文件
瀏覽,但我不能整合上傳功能
我看過其他的答案,但它只是讓更多的錯誤..
我混淆了POST PHP的東西也
任何人都可以幫忙嗎?由於之前
上傳的ImageView到服務器

這些都是XML

<Button 
    android:id="@+id/loadimage" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Open Picture Gallery" 
/> 
<Button 
    android:id="@+id/takeimage" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Take Picture" 
/> 
<TextView 
    android:id="@+id/targeturi" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
/> 

<ImageView 
    android:id="@+id/targetimage" 
    android:layout_width="fill_parent" 
    android:layout_height="323sp" /> 

<Button 
    android:id="@+id/uploadimage" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Upload Picture" /> 
+0

也許這可以幫助你: http://stackoverflow.com/questions/7163311/android-file-upload-using-http-put – Andreas 2012-02-14 06:24:12

回答

2

@VenkataKrishna似乎你的代碼讓我感到很困擾XD。我繼續搜索,發現了一個新的解決方案

response = httpclient.execute(postRequest); 

response=httpclient.execute(new HttpPost("my url here")); 

取代它在try-catch前

HttpParams p=new BasicHttpParams(); 
p.setParameter("parameter", p); 
HttpClient httpclient = new DefaultHttpClient(p); 

反正添加此,感謝幫助我:d

0
HttpResponse response=null; 

      //uploading image... 

      HttpClient httpclient = new DefaultHttpClient();   
      HttpPost postRequest = new HttpPost("your url..."); 



      try { 
       MultipartEntity multipartContent = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);  
       multipartContent.addPart("__VIEWSTATE",new StringBody("")); 
       multipartContent.addPart("__EVENTVALIDATION",new StringBody("")); 


      FileBody fiebody = new FileBody(new File(your_image_path...));  
        multipartContent.addPart("fupimage", fiebody); 

       postRequest.setEntity(multipartContent); 


       response = httpclient.execute(postRequest); 
       BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8")); 
       String sResponse;    
       StringBuilder s = new StringBuilder(); 
       while ((sResponse = reader.readLine()) != null) { 
        s = s.append(sResponse);      
        System.out.println(sResponse); 
       } 

       reader.close();    

       System.out.println("Status code:" +response.getStatusLine().getStatusCode());   
       System.out.println("Status code:" +response.getStatusLine().getReasonPhrase()); 

      } catch (UnsupportedEncodingException e) { 
       e.printStackTrace(); 
      } catch (IllegalStateException e) { 
       e.printStackTrace(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 

如果你response.getStatusLine()。getStatusCode()(狀態代碼)200,這意味着你的圖像上傳到服務器成功。

+0

文件圖像路徑存儲在我的textTargetUri,我已經通過使用 將數據類型更改爲字符串directoryFile; directoryFile = textTargetUri.getText()。toString(); 仍然沒有什麼工作有:( – Setsurei 2012-02-16 02:16:52

+0

它趕上IOException異常,趕上(UnsupportedEncodingException五){ \t \t e.printStackTrace(); \t \t Toast.makeText(upload.this, 「AAAA」,Toast.LENGTH_SHORT)。顯示(); \t \t}趕上(IllegalStateException異常E){ \t \t e.printStackTrace(); \t \t Toast.makeText(upload.this, 「BBBB」,Toast.LENGTH_SHORT).show(); \t \t } catch(IOException e){ \t \t e.printStackTrace(); \t \t Toast.makeText(upload.this,「cccc」,Toast.LENGTH_SHORT).show(); \t \t} 它顯示「cccc」 – Setsurei 2012-02-16 09:14:22