2012-02-14 8 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のものと混同しています
誰でも助けることができますか?あなたはresponse.getStatusLine()。getStatusCode()(ステータスコード)を取得する場合
アップロード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

:私は、とにかくのtry-catch前に私を助けてくれてありがとうを

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

これを追加

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

に置き換える検索おくと

response = httpclient.execute(postRequest); 

新しい解決策を見つけましたおそらくこれがあなたを助けることができます: http://stackoverflow.com/questions/7163311/android-file-upload-using-http-put – Andreas

答えて

2

@VenkataKrishnaはあなたのコードがたくさんのXDを困惑させているようです。 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(); 
      } 

ある前に、おかげで200あなたのイメージが正常にサーバーにアップロードされることを意味します。

+0

ファイルイメージのパスがtextTargetUriに保存されていて、すでにデータ型をStringに変更しています。 String directoryFile; directoryFile = textTargetUri.getText()。toString(); は、まだ何も仕事がそこにある:( – Setsurei

+0

それはIOExceptionがキャッチ、キャッチ(にUnsupportedEncodingException電子){ \t \t e.printStackTrace(); \t \t Toast.makeText(upload.this、 "AAAA"、Toast.LENGTH_SHORT)。ショー(); \t \t}キャッチ(IllegalStateExceptionが電子){ \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