I can not pass the URL of a pdf file for download without being a static variable

0

I'm using the code below to try to download a pdf file through a URL. The activity " DownloadActivity " only downloads, and the URL link must be passed by parameter through another activity.

The problem is that the file download only occurs when I use a static variable to pass the file link to the DownloadFileFromURL class.

If I try to pass the link using the FILE_PDF variable, expressed in the code below, the download does not happen.

Is there another way to do this procedure for download? How can I use a non-static variable to pass the link?

public class DownloadActivity extends AppCompatActivity {

Button mDownload;
TextView mProgressTxt;
ProgressBar mProgress;
String FILE_PDF;
String arquivo;

private static String FILE = "http://www.pdf995.com/samples/pdf.pdf";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_download);

    Intent intent = getIntent();
    arquivo = intent.getStringExtra("nome_arquivo");
    //FILE_PDF = intent.getStringExtra("link_arquivo");

    mDownload = (Button) findViewById(R.id.btn_baixar);
    mProgressTxt = (TextView) findViewById(R.id.tv_progress);
    mProgress = (ProgressBar) findViewById(R.id.progress);

    mProgress.setMax(100);
    mProgress.setProgress(0);

    mDownload.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            new DownloadFileFromURL().execute(FILE);
        }
    });
}


private class DownloadFileFromURL extends AsyncTask<String, String, String> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();

        mProgressTxt.setText("Baixando arquivo...");
    }


    @Override
    protected String doInBackground(String... f_url) {
        int count;
        try {
            URL url = new URL(f_url[0]);
            URLConnection conection = url.openConnection();
            conection.connect();

            int lenghtOfFile = conection.getContentLength();

            InputStream input = new BufferedInputStream(url.openStream(), 8192);

            FileOutputStream output = openFileOutput(arquivo, Context.MODE_PRIVATE);

            byte data[] = new byte[1024];

            long total = 0;

            while ((count = input.read(data)) != -1) {
                total += count;
                publishProgress(""+(int)((total*100)/lenghtOfFile));
                output.write(data, 0, count);
            }
            output.flush();
            output.close();
            input.close();

        } catch (Exception e) {
            Log.e("Error: ", e.getMessage());
        }
        return null;
    }

    protected void onProgressUpdate(String... progress) {
        String plural;

        mProgress.setProgress(Integer.parseInt(progress[0]));

        if (Integer.parseInt(progress[0]) > 1){
            plural = "Transferidos ";
        }else{
            plural = "Transferido ";
        }
        mProgressTxt.setText(plural+progress[0]+"%");
    }

    @Override
    protected void onPostExecute(String file_url) {
        mProgress.setProgress(0);
        mProgressTxt.setText("O arquivo foi transferido com sucesso!");
    }
}

How am I passing the parameter to DownloadActivity.class

Intent it = new Intent(getActivity(), DownloadActivity.class);
it.putExtra("nome_arquivo", arquivo);
it.putExtra("link_arquivo", link);
startActivity(it);
    
asked by anonymous 21.08.2017 / 23:00

1 answer

2

You are passing the link in the attempt with the name "link_file" and retrieving the Extra using another name, "link", will soon be null.

EDIT: The parameter passed in the intent ("link") may be empty as well.

    
22.08.2017 / 02:24