2011-06-21 150 views

回答

2

您可以使用此代碼拍攝圖庫圖像對話框。

mUploadButton = (Button) findViewById(R.id.uploadButton); 

mUploadButton.setOnClickListener(new OnClickListener() {//this button is used for pickup gallery image. 
     public void onClick(View v) { 

      Intent rselect = new Intent(Intent.ACTION_GET_CONTENT, null); 
      rselect.setType("image/*"); 
      rselect.putExtra("return-data", true); 
      startActivityForResult(rselect, 1); 

     } 
    }); 

onActivityResult()被用於

@Override 
public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    switch (requestCode) { 
     case 1: 
      if(requestCode == 1 && data != null && data.getData() != null){ 
       // Bundle params = new Bundle(); 
       // params.putString("method", "photos.upload"); 
       // intent's getData() returns a Uri describing 
       // the data which the intent holds 
       Uri _uri = data.getData(); 

       if (_uri != null) { 
        //User had pick an image. 

        Cursor cursor = getContentResolver().query(_uri, new String[] { android.provider.MediaStore.Images.ImageColumns.DATA }, null, null, null); 
        cursor.moveToFirst(); 

        //Link to the image 
        final String imageFilePath = cursor.getString(0); 
        Log.v("imageFilePath", imageFilePath); 
        File photos= new File(imageFilePath); 
        long length = photos.length(); 

        byte[] imgData = new byte[(int) length]; 

        FileInputStream pdata = null; 
         try { 
          pdata = new FileInputStream(photos); 


         } catch (FileNotFoundException e1) { 
          // TODO Auto-generated catch block 
          e1.printStackTrace(); 
         } 
         try { 
          pdata.read(imgData);//imgdata is an array where you get byte data for selected image from gallery and ready to upload. 
         } catch (IOException e) { 
          // TODO Auto-generated catch block 
          e.printStackTrace(); 
         } 




        cursor.close(); 
       } 
      } 
      super.onActivityResult(requestCode, resultCode, data); 
     } 
    } 
+0

,我怎麼可以將圖像保存到服務器 – AB1209

0

「imgData」 是一個字節數組。這些數據現在可以在服務器上上傳了。你通過使用http post方法來做到這一點。

+1

謝謝,任何代碼都將有幫助 – AB1209

相關問題