0

I wanted to upload file which is located in bluetooth folder of Internal Storage. I don't what is going wrong but my code is not working. Here I have not added NetworkAvailability class because class is just to send broadcast whenever user connects to Internet and it is not needed here. Rest of code is below , please check out and Help me find what going wrong here....

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.os.IBinder;
import android.widget.Toast; 
import java.io.File;  
import it.sauronsoftware.ftp4j.FTPClient;



public class uploadData extends Service {
private NetworkAvailability networkAvailability;
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    super.onStartCommand(intent, flags, startId);


    networkAvailability = NetworkAvailability.getInstance();
    networkAvailability.registerNetworkAvailability(this, receiver);

    return START_STICKY;
}

private BroadcastReceiver receiver = new BroadcastReceiver() {
    File file;

    @Override
    public void onReceive(final Context context, Intent intent) {
        if (intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false)) {
            Toast.makeText(getApplicationContext(), "network is not available", Toast.LENGTH_LONG).show();
        } else {
            Toast.makeText(getApplicationContext(), "network is available", Toast.LENGTH_LONG).show();


       try {
             String path = "/storage/emulated/0/bluetooth/f.txt";
             file = new File(path);
             if (!file.exists()) { Toast.makeText(context, "File not 
             found", Toast.LENGTH_LONG).show();
                }
                new uploadFTP().execute(file);

            } catch (Exception ex) {
                Toast.makeText(getApplicationContext(), ex.toString(),
            Toast.LENGTH_LONG).show();
            }


        }
    }

};


@Override
public void onDestroy() {
    networkAvailability.unregisterNetworkAvailability(this, receiver);
    super.onDestroy();
}

@Override
public IBinder onBind(Intent intent) {
    return null;
   }
}

Every time i connect to Internet,It Toast that Network is available but file didn't get uploaded . Here is FTP class:

import android.os.AsyncTask;
import it.sauronsoftware.ftp4j.FTPClient;
import java.io.File;

public class uploadFTP extends AsyncTask<File, Void, Void> {

    @Override
    protected Void doInBackground(File... uploadfile) {
        String FTP_HOST = "31.170.167.199";
        String FTP_USER = "u2821*****"; // Username Hidden
        String FTP_PASS = "**********"; //Password Hidden

        FTPClient client = new FTPClient();



            try {

                client.connect(FTP_HOST,21);
                client.login(FTP_USER, FTP_PASS);
                client.setType(FTPClient.TYPE_BINARY);


                client.upload(uploadfile[0]);

            } catch (Exception e) {
                e.printStackTrace();
                try {
                    client.disconnect(true);
                } catch (Exception e2) {
                    e2.printStackTrace();
                }
            }
           return null;
        }
}

I also wanted to ask that Is there any method to directly access Internal storage like there is one for external storage(Environment.getExternalStorageDirectory().getAbsolutePath()).

Novice
  • 49
  • 11

0 Answers0