我正在开发一个Android应用程序,我需要将我的图像发送到特定的FTP URL地址,任何人都可以帮助我,我该怎么办呢。
网址是: 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 :(得分:0)
尝试使用此简单示例从here上传网络服务器上的任何文件。
答案 1 :(得分:0)
您必须添加使用FTPClient类的依赖项。您可以在任何地方轻松找到它。它是“commons apache”,命名为依赖。