2014-03-29 20 views
0

我正在開發一個android應用程序,因爲我需要將圖像發送到特定的FTP URL地址,任何人都可以幫助我,請問我該怎麼做。如何發送圖像到FTP客戶端在Android?

網址是: http://demo1.idevtechnolabs.com/RChatAPI/usrPhotos/

而且我的代碼是:

public class ImageGallery extends Activity 
{ 

    private static final int PICK_IMAGE = 1; 
    private static final int PICK_Camera_IMAGE = 2; 
    private ImageView imgView; 
    private Button upload,cancel; 
    private Bitmap bitmap; 
    private ProgressDialog dialog; 
    Uri imageUri; 
    Uri selectedImageUri = null; 
    String filePath = null; 
    MediaPlayer mp=new MediaPlayer(); 
    Handler progressHandler = new Handler(); 

    @Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_image_gallery); 

     imgView = (ImageView) findViewById(R.id.ImageView); 
     upload = (Button) findViewById(R.id.imguploadbtn); 
     cancel = (Button) findViewById(R.id.imgcancelbtn); 

     upload.setOnClickListener(new View.OnClickListener() 
     { 

      public void onClick(View v) 
      { 
       if (bitmap == null) 
       { 
        Toast.makeText(getApplicationContext(),"Please select image", Toast.LENGTH_SHORT).show(); 
       } 
       else 
       { 
        dialog = ProgressDialog.show(ImageGallery.this, "Uploading","Please wait...", true); 
        new ImageGalleryTask().execute(); 
       } 
      } 
     }); 

     cancel.setOnClickListener(new OnClickListener() 
     { 
      public void onClick(View v) 
      { 
       // TODO Auto-generated method stub 
       ImageGallery.this.finish(); 
      } 
     }); 

    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) 
    { 
       MenuInflater inflater = getMenuInflater(); 
       inflater.inflate(R.menu.activity_image_gallery, menu); 
       return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) 
    { 
     switch (item.getItemId()) 
     { 
      case R.id.camera: 
       String fileName = "new-photo-name.jpg"; 

       ContentValues values = new ContentValues(); 
       values.put(MediaStore.Images.Media.TITLE, fileName); 
       values.put(MediaStore.Images.Media.DESCRIPTION,"Image captured by camera"); 

       imageUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values); 

       Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
       intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); 
       intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1); 
       startActivityForResult(intent, PICK_Camera_IMAGE); 
       return true; 

      case R.id.gallery: 
       try 
       { 
         Intent gintent = new Intent(); 
         gintent.setType("image/*"); 
         gintent.setAction(Intent.ACTION_GET_CONTENT); 
         startActivityForResult(Intent.createChooser(gintent, "Select Picture"),PICK_IMAGE); 
       } 
       catch (Exception e) 
       { 
         Toast.makeText(getApplicationContext(), 
         e.getMessage(), 
         Toast.LENGTH_LONG).show(); 
         Log.e(e.getClass().getName(), e.getMessage(), e); 
       } 
       return true; 
     } 
     return false; 
    } 

    protected void onActivityResult(int requestCode, int resultCode, Intent data) 
    { 
     switch (requestCode) 
     { 
       case PICK_IMAGE: 
        if (resultCode == Activity.RESULT_OK) 
        { 
         selectedImageUri = data.getData(); 
        } 
        break; 
       case PICK_Camera_IMAGE: 
        if (resultCode == RESULT_OK) 
        { 
         //use imageUri here to access the image 
         selectedImageUri = imageUri; 
         /*Bitmap mPic = (Bitmap) data.getExtras().get("data"); 
         selectedImageUri = Uri.parse(MediaStore.Images.Media.insertImage(getContentResolver(), mPic, getResources().getString(R.string.app_name), Long.toString(System.currentTimeMillis())));*/ 
        } 
        else if (resultCode == RESULT_CANCELED) 
        { 
         Toast.makeText(this, "Picture was not taken", Toast.LENGTH_SHORT).show(); 
        } 
        else 
        { 
         Toast.makeText(this, "Picture was not taken", Toast.LENGTH_SHORT).show(); 
        } 
        break; 
      } 

      if(selectedImageUri != null) 
      { 
        try 
        { 
         // OI FILE Manager 
         String filemanagerstring = selectedImageUri.getPath(); 

         // MEDIA GALLERY 
         String selectedImagePath = getPath(selectedImageUri); 

         if (selectedImagePath != null) 
         { 
          filePath = selectedImagePath; 
         } 
         else if (filemanagerstring != null) 
         { 
          filePath = filemanagerstring; 
         } 
         else 
         { 
          Toast.makeText(getApplicationContext(), "Unknown path", 
            Toast.LENGTH_LONG).show(); 
          Log.e("Bitmap", "Unknown path"); 
         } 

         if (filePath != null) 
         { 
          decodeFile(filePath); 
         } 
         else 
         { 
          bitmap = null; 
         } 
        } 
        catch (Exception e) 
        { 
         Toast.makeText(getApplicationContext(), "Internal error", 
           Toast.LENGTH_LONG).show(); 
         Log.e(e.getClass().getName(), e.getMessage(), e); 
        } 
      } 

    } 

    class ImageGalleryTask extends AsyncTask<Void, Void, String> 
    { 
     @Override 
     protected String doInBackground(Void... unsued) 
     { 
      FTPClient ftpClient = new FTPClient(); 
      try 
      { 
       ftpClient.connect(InetAddress.getByName("My URL Address")); 
       ftpClient.login("My Username", "My Password"); 
       ftpClient.changeWorkingDirectory("My Path of URL"); 

       if (ftpClient.getReplyString().contains("250")) 
       { 
        ftpClient.setFileType(FTP.BINARY_FILE_TYPE); 
        BufferedInputStream buffIn = null; 
        String path=selectedImageUri.toString(); 
        Log.v("Image Path:",filePath); 

        buffIn = new BufferedInputStream(new FileInputStream(filePath)); 
        ftpClient.enterLocalPassiveMode(); 

        ProgressInputStream progressInput = new ProgressInputStream(buffIn, progressHandler); 
        boolean result=ftpClient.storeUniqueFile(progressInput); 
        boolean result = ftpClient.storeFile(localAsset.getFileName(), progressInput); 
        buffIn.close(); 
        ftpClient.logout(); 
        ftpClient.disconnect(); 
       } 

      } 
      catch (Exception e) 
      { 
       Log.d("Error in ftp:",e.toString()); 
      } 
      return "Success"; 

     } 

     @Override 
     protected void onProgressUpdate(Void... unsued) 
     { 
     } 

     @Override 
     protected void onPostExecute(String sResponse) 
     { 
      try 
      { 
       if (dialog.isShowing()) 
        dialog.dismiss(); 
      } 
      catch (Exception e) 
      { 
       Toast.makeText(getApplicationContext(),e.getMessage(),Toast.LENGTH_LONG).show(); 
       Log.e(e.getClass().getName(), e.getMessage(), e); 
      } 
     } 

    } 

    public String getPath(Uri uri) 
    { 
     String[] projection = { MediaStore.Images.Media.DATA }; 
     Cursor cursor = managedQuery(uri, projection, null, null, null); 
     if (cursor != null) 
     { 
      int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
      cursor.moveToFirst(); 
      return cursor.getString(column_index); 
     } 
     else 
      return null; 
    } 

    public void decodeFile(String filePath) 
    { 
     // Decode image size 
     BitmapFactory.Options o = new BitmapFactory.Options(); 
     o.inJustDecodeBounds = true; 
     BitmapFactory.decodeFile(filePath, o); 

     // The new size we want to scale to 
     final int REQUIRED_SIZE = 1024; 

     // Find the correct scale value. It should be the power of 2. 
     int width_tmp = o.outWidth, height_tmp = o.outHeight; 
     int scale = 1; 
     while (true) 
     { 
      if (width_tmp < REQUIRED_SIZE && height_tmp < REQUIRED_SIZE) 
       break; 
      width_tmp /= 2; 
      height_tmp /= 2; 
      scale *= 2; 
     } 

     // Decode with inSampleSize 
     BitmapFactory.Options o2 = new BitmapFactory.Options(); 
     o2.inSampleSize = scale; 
     bitmap = BitmapFactory.decodeFile(filePath, o2); 

     imgView.setImageBitmap(bitmap); 
    } 
} 

當我使用此代碼,然後點擊上傳按鈕 我得到了以下錯誤:

Could not find class 'org.apache.commons.net.ftp.FTPClient', referenced from method com.androidmyway.demo.imagedemo.ImageGallery$ImageGalleryTask.doInBackground 

回答

0

嘗試使用這個簡單的例子來從here通過網絡服務器上傳任何文件。

+0

我曾試過這個,但我需要上傳一個圖像到FTP客戶端不在任何URL。此示例適用於將圖像發送到URL地址 –

+0

時的檢查:http://androidexample.com/FTP_File_Upload_From_Sdcard_to_server/index.php?view=article_discription&aid=98&aaid=120 FTP在端口號21上工作,因此請嘗試提供21,同時連接像'ftpClient.connect(InetAddress.getByName(「我的URL地址」),21);' –

+0

21是固定在任何端口mam? –

相關問題