This post is very useful to novice users and users facing various problems on uploading picture from android to ftp server. This is very simple and straight forward post and I assumed you have good understanding of android basics. This time I won't start from scratch (i.e. from creating new android project), instead I assumed that you have already created android project with an activity and main.xml (UI). Please follow the simple steps as given below.
Step#1:
//declare the following in your activity before onCreate Method
private RelativeLayout photos_layout;
private static Bitmap scaledphoto=null;
Step#1:
//declare the following in your activity before onCreate Method
private RelativeLayout photos_layout;
private static Bitmap scaledphoto=null;
private String filePath=null;
private Uri u=null;
private Boolean picTaken=false;
Step#2:
In onCreate Method of your activity, write this code:
@Override
protected void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
photos_layout=(RelativeLayout)findViewById(R.id.photos);
setContentView(R.layout.main);
photos_layout=(RelativeLayout)findViewById(R.id.photos);
//button with
onClickListener to turn on camera for taking picture
((Button)photos_layout.findViewById(R.id.capture_photo))
.setOnClickListener(new
View.OnClickListener() {
@Override
public void onClick(View
arg0) {
String
pictureName="Test";//here you can get picture name from user. I
supposed Test name
Intent
intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
File
photo = new File(Environment.getExternalStorageDirectory(), pictureName+".jpg");//save picture
(.jpg) on SD Card
u=Uri.fromFile(photo);
intent.putExtra(MediaStore.EXTRA_OUTPUT,u);
filePath =
photo.getAbsolutePath();
startActivityForResult(intent,
REQUEST_CODE);
}
}
((Button)photos_layout.findViewById(R.id.upload_picture))
.setOnClickListener(new
View.OnClickListener() {
@Override
public void onClick(View
arg0) {
upLoadPicture();
}
}
}
Step#3:
Now in onActivityResult Method, copy/write this code:
protected void
onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode,
resultCode, data);
try {
getContentResolver().notifyChange(u, null);
ContentResolver cr = getContentResolver();
Bitmap bm =
android.provider.MediaStore.Images.Media.getBitmap(cr, u);
//ImageView to set the picture taken from camera.
photo_captured=(ImageView)photos_layout.findViewById(R.id.pic_captured);
photo_captured.setImageBitmap(bm);
picTaken=true; //to ensure picture is taken
}catch(Exception e) {
e.printStackTrace();
}
}
Step#4:
After picture is taken, than implement the upload method as under:
//Method for
uploading picture on FtpServer
public void upLoadPicture()
{
if(picTaken){
final ProgressDialog
pd = ProgressDialog.show(UserInfoActivity.this, "Please
wait", "Uploading Picture ...", true);
new Thread(){
@Override
public void run() {
File
file = new File(filePath);
try {
FTPClient
client = new FTPClient();
client.connect("ftp.********.net");
client.login(ftp_server_username,
ftp_server_password); //this is the login credentials of your ftpserver. Ensure to use valid username and password otherwise it throws exception
try {
client.changeDirectory("MyPictures"); //I want to upload picture in MyPictures directory/folder. you can use your own.
}
catch (Exception e) {
client.createDirectory("MyPictures");
client.changeDirectory("MyPictures");
}
client.upload(file); //this is actual file uploading on FtpServer in specified directory/folder
client.disconnect(true); //after file upload, don't forget to disconnect from FtpServer.
file.delete();
}
catch (Exception e) {
Toast.makeText(getApplicationContext(),
"Exception:
"+e.getMessage(), Toast.LENGTH_LONG).show();
}
}
pd.dismiss();
}
}.start();
}
else
{
Toast.makeText(getApplicationContext(),
"Please
Take Picture First than Upload.", Toast.LENGTH_LONG).show();
}
}
Picture uploading on Ftp Server is done. Similarly, you can upload any file on ftpserver as well.
Please don't forget to ask if you are facing difficulty in implementing this post. Your valuable suggestions are highly appreciated and please correct me if I am doing something wrong in this post.
Thank you very much for visiting my blog.
This comment has been removed by the author.
ReplyDeleteCongratulations !
ReplyDeleteHi Avram,
DeleteThanks for your feedback.
"Congratulations! " ??
Where's the complete source code?????.. please send me the link
ReplyDeleteThanks.
This comment has been removed by the author.
ReplyDeleteHi thanks for your code sharing very useful , I need server side code in java instead of PHP , also I need full source code for this android side.
ReplyDeletethanks.
Mail Id : erodepanneer90@gmail.com
Hi very usefull can you please share source code
ReplyDeleteThanks
borrasunil553@gmail.com
This comment has been removed by a blog administrator.
ReplyDeleteGood tutorial, can i get the complete source, please, thx.
ReplyDeleteThis Activity contains the complete code required for uploading picture.
DeleteThanks
hi, i need your help to build android app, i want upload text file on ftp server without click on any button. means background process, also need some help. contact me on
ReplyDelete"vaibhaoa@gmail.com"
where is xml code
ReplyDeleteHi,
ReplyDeleteWhere is FTP class? You have not shared the code of FTP class, please send it on prateekvijayvergiya.cse19@jecrc.ac.in
Hi
ReplyDeletethis code working perfect
ReplyDeletethank u
ReplyDeletecan u please give the code about how to download image from ftp
ReplyDeleteSorry for late reply, let me know how can I help you?
ReplyDeletethere is not working my source code pls guide me
ReplyDelete