You are on page 1of 10

package com.example.

home;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InterruptedIOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.Scanner;
import
import
import
import
import
import
import
import
import
import
import
import
import
import
import
import
import
import
import
import
import
import
import
import
import

android.app.Activity;
android.content.Context;
android.content.ContextWrapper;
android.content.Intent;
android.content.pm.PackageManager;
android.graphics.Bitmap;
android.graphics.BitmapFactory;
android.media.AudioManager;
android.media.MediaPlayer;
android.media.Ringtone;
android.media.RingtoneManager;
android.net.Uri;
android.os.AsyncTask;
android.os.Bundle;
android.os.Environment;
android.os.StrictMode;
android.provider.MediaStore;
android.util.Log;
android.view.View;
android.widget.Button;
android.widget.EditText;
android.widget.ImageView;
android.widget.Toast;
it.sauronsoftware.ftp4j.FTPClient;
it.sauronsoftware.ftp4j.FTPDataTransferListener;

public class MainActivity extends Activity {


public String mes;
public String resultF;
MediaPlayer mMediaPlayer;
static final String FTP_HOST= "192.168.175.1";

static final String FTP_USER = "mm";

static final String FTP_PASS ="mm";


Button btn,b1;
private static final int CAMERA_CAPTURE_IMAGE_REQUEST_CODE = 100;

public static final int MEDIA_TYPE_IMAGE = 1;


// directory name to store captured images and videos
private static final String IMAGE_DIRECTORY_NAME = "Hello Camera";
private Uri fileUri;
private ImageView imgPreview;
private Button btnCapturePicture;
EditText et;
public static File f;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
StrictMode.ThreadPolicy Tpolicy = new
StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(Tpolicy);
imgPreview = (ImageView) findViewById(R.id.imgPreview);
btnCapturePicture = (Button) findViewById(R.id.btnCapturePicture);
b1=(Button) findViewById(R.id.button1);
btn=(Button) findViewById(R.id.button2);
et=(EditText) findViewById(R.id.editText1);

/*
* Capture image
*/
///

btnCapturePicture.setOnClickListener(new View.OnClickListener() {

});

@Override
public void onClick(View v) {
// capture picture
captureImage();
Log.v("nnnnnn",fileUri.getPath());
}

if (!isDeviceSupportCamera()) {
Toast.makeText(getApplicationContext(),
"Sorry! Your device doesn't support camera",
Toast.LENGTH_LONG).show();

finish();
}

b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {

Toast.makeText(getBaseContext(),
fileUri.getPath(), Toast.LENGTH_LONG).show();
File f = new File(fileUri.getPath());
f= new File(fileUri.getPath());
uploadFile();
}
});
}
private boolean isDeviceSupportCamera() {
if (getApplicationContext().getPackageManager().hasSystemFeature(
PackageManager.FEATURE_CAMERA)) {
// this device has a camera
return true;
} else {
// no camera on this device
return false;
}
}
private void captureImage() {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);

startActivityForResult(intent,
CAMERA_CAPTURE_IMAGE_REQUEST_CODE);
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putParcelable("file_uri", fileUri);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {

super.onRestoreInstanceState(savedInstanceState);
fileUri = savedInstanceState.getParcelable("file_uri");
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent
data) {

// if the result is capturing Image


if (requestCode == CAMERA_CAPTURE_IMAGE_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
// successfully captured the image
// display it in image view
previewCapturedImage();
} else if (resultCode == RESULT_CANCELED) {
// user cancelled Image capture
Toast.makeText(getApplicationContext(),
"User cancelled image capture",
Toast.LENGTH_SHORT)
.show();
} else {
// failed to capture image
Toast.makeText(getApplicationContext(),
"Sorry! Failed to capture image",
Toast.LENGTH_SHORT)
.show();
}
}
}
private void previewCapturedImage() {
try {
imgPreview.setVisibility(View.VISIBLE);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 8;
final Bitmap bitmap =
BitmapFactory.decodeFile(fileUri.getPath(),
options);
imgPreview.setImageBitmap(bitmap);

/////////////////////
f=new File((saveToInternalSorage(bitmap)),"profile.jpg");
////////////////////////
} catch (NullPointerException e) {
e.printStackTrace();
}

public Uri getOutputMediaFileUri(int type) {


return Uri.fromFile(getOutputMediaFile(type));
}

//// Get result


public void res(View view)
{
// runTcpServer();

new Thread(new Runnable(){


public void run()
{runTcpServer();
}
} ).start();
}
private void runTcpServer() {
ServerSocket ss = null;
try {
int TCP_SERVER_PORT=8222;
ss = new ServerSocket(TCP_SERVER_PORT);
Log.d("TcpServer", ss.getInetAddress()+"");
Socket s = ss.accept();
Scanner dis1=new Scanner(s.getInputStream());
// Uri notification =
RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
// Uri notification1 =
RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
// Ringtone r = RingtoneManager.getRingtone(getApplicationContext(),
notification);
// Ringtone r1 = RingtoneManager.getRingtone(getApplicationContext(),
notification1);
mMediaPlayer = new MediaPlayer();

while(true){
if(dis1.hasNext())
{
mes=dis1.nextLine() ;
if(!mes.equals(null))
{
if(mes.contains("1"))
{resultF="1000"; mMediaPlayer =
MediaPlayer.create(this, R.raw.v1); }

runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(getBaseContext(),resultF,Toast.LENGTH_SHORT).show();
}
});

mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mMediaPlayer.setLooping(false);
mMediaPlayer.start();}
break;
}
}

s.close();
} catch (InterruptedIOException e) {
e.printStackTrace();
Log.e("TcpServer", ""+e);
} catch (IOException e) {
e.printStackTrace();
Log.e("TcpServer", ""+e);
} finally {
if (ss != null) {
try {
ss.close();
} catch (IOException e) {
e.printStackTrace();
Log.e("TcpServer", ""+e);
}
}
}

///Get result
private File getOutputMediaFile(int type) {
File mediaStorageDir = new File(
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
IMAGE_DIRECTORY_NAME);
// Create the storage directory if it does not exist
if (!mediaStorageDir.exists()) {
if (!mediaStorageDir.mkdirs()) {
Log.d(IMAGE_DIRECTORY_NAME, "Oops! Failed create "

+ IMAGE_DIRECTORY_NAME + " directory");


}

return null;

}
// Create a media file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
Locale.getDefault()).format(new Date());
File mediaFile;
if (type == MEDIA_TYPE_IMAGE) {
mediaFile = new File(mediaStorageDir.getPath() + File.separator
+ "IMG_" + timeStamp + ".jpg");
} else {
return null;
}
return mediaFile;
}
private String saveToInternalSorage(Bitmap bitmapImage){
ContextWrapper cw = new ContextWrapper(getApplicationContext());
// path to /data/data/yourapp/app_data/imageDir
File directory = cw.getDir("imageDir", Context.MODE_PRIVATE);
// Create imageDir
File mypath=new File(directory,"profile.jpg");
FileOutputStream fos = null;
try {
fos = new FileOutputStream(mypath);
// Use the compress method on the BitMap object to write image to the
OutputStream
bitmapImage.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
return directory.getAbsolutePath();
}

public void sendP(View v) {

Toast.makeText(getBaseContext(),et.getText().toString(),Toast.LENGTH_SHORT).show(
);
File f = new File(fileUri.getPath());
f = new File(fileUri.getPath());
//Toast.makeText(this,fi.getPath(), Toast.LENGTH_LONG).show();
Connection conn= new Connection()
;
conn.execute();
//uploadFile(f);

}
private class Connection extends AsyncTask {

@Override
protected Object doInBackground(Object... arg0) {
uploadFile();
return null;
}
}

public void uploadFile(){

FTPClient client = new FTPClient();


try {
Toast.makeText(this, "Start",
Toast.LENGTH_LONG).show();
Toast.makeText(this, FTP_HOST,
Toast.LENGTH_LONG).show();
client.connect(FTP_HOST,21);
Toast.makeText(this, "connect",
Toast.LENGTH_LONG).show();
client.login(FTP_USER, FTP_PASS);
Toast.makeText(this, "Authonticate",
Toast.LENGTH_LONG).show();
client.setType(FTPClient.TYPE_BINARY);
Toast.makeText(this,MainActivity.f.getPath(),
Toast.LENGTH_LONG).show();

client.upload(MainActivity.f, new MyTransferListener());


Toast.makeText(this, "Upload",
Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(this, e.toString(),
Toast.LENGTH_LONG).show();
try {
client.disconnect(true);
} catch (Exception e2) {
Toast.makeText(this, e2.toString(),
Toast.LENGTH_LONG).show();
}
}
}

public class MyTransferListener implements FTPDataTransferListener {


public void started() {
btn.setVisibility(View.GONE);
// Transfer started
Toast.makeText(getBaseContext(), " Upload Started ...",
Toast.LENGTH_SHORT).show();
//System.out.println(" Upload Started ...");
}
public void transferred(int length) {
// Yet other length bytes has been transferred since the last time

this

// method was called


Toast.makeText(getBaseContext(), " transferred ..." + length,
Toast.LENGTH_SHORT).show();
//System.out.println(" transferred ..." + length);
}
public void completed() {
btn.setVisibility(View.VISIBLE);
// Transfer completed
Toast.makeText(getBaseContext(), " completed ...",
Toast.LENGTH_SHORT).show();
//System.out.println(" completed ..." );
}
public void aborted() {
btn.setVisibility(View.VISIBLE);
// Transfer aborted
Toast.makeText(getBaseContext()," transfer aborted , please try
again...", Toast.LENGTH_SHORT).show();
//System.out.println(" aborted ..." );
}
public void failed() {
btn.setVisibility(View.VISIBLE);
// Transfer failed
System.out.println(" failed ..." );
}
}
}