Home » Android » Android Class to create pdf file and save it to ftp-Exceptionshub

Android Class to create pdf file and save it to ftp-Exceptionshub

Posted by: admin February 26, 2020 Leave a comment

Questions:

In Activity i have a button to open a ScanningActivity for scan document and in ActivityResult i create a pdf file and save it to an fpt server. For now it is ok but i’d like to make the same in others activities. How can i put downloadTask in separate file and how use it from another activities?

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);        

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

                startScan(111);
            }

protected void startScan(int preference) {
    Intent intent = new Intent(this, ScanActivity.class);
    intent.putExtra(ScanConstants.OPEN_INTENT_PREFERENCE, preference);
    startActivityForResult(intent, REQUEST_CODE);
}  

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

            new CreatingPdf().execute();            
    }
}


public void download(){
        String address="11.11.11.123",u="user",p="parola",serverPath=comandanr+".pdf",destination=(Environment.getExternalStoragePublicDirectory(
            DIRECTORY_DOWNLOADS).toString())+"/"+comandanr+".pdf";

   downloadTask async=new downloadTask();
    async.execute(address,u,p,serverPath,destination);
}

class downloadTask extends AsyncTask<String, Void, String> {

    private ProgressDialog prg;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        prg = new ProgressDialog(DetaliiActivity.this);
        prg.setMessage("Download comanda " + comandanr);
        prg.show();
    }

    @Override
    protected String doInBackground(String... params) {
        try {
            easyFTP ftp = new easyFTP();               
            ftp.connect(params[0], params[1], params[2]);
            ftp.downloadFile(params[3], params[4]);
            return new String("Download Successful");
        } catch (Exception e) {
            String t = "Failure : " + e.getLocalizedMessage();
            return t;
        }
    }

    @Override
    protected void onPostExecute(String str) {
        prg.dismiss();

        openpdf();
    }
    private void openpdf(){

     }
How to&Answers: