I cannot pass the URL of a downloadable pdf file without a static variable

Asked

Viewed 55 times

0

I am using the code below to try to download a pdf file through a URL. Activity "Downloadactivity" just download, and the URL link should be passed by parameter through another Activity.

The problem is that downloading the file only occurs when using a static variable to link the file to the class Downloadfilefromurl.

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

Is there another way to do this download procedure? How can I use a variable without being static 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 I am 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);
  • You could add how you are passing the parameter link and the filing cabinet, please? It will help a lot! Thank you

  • Most likely it is that Extra ("link") which you try to catch from the Intent is null.

  • I think of two errors, one was the wrong parameter, but you guarantee that it is coming right, the other is the url being wrong (the url being in trouble). When you take the value of the other Activity, does it just not enter the Asynctask, or does it open the connection??? it’s kind of hard to know, because as the friend said below, the logic is right!

  • @Márciooliveira, really you were right, the parameter was empty even. Now it’s working. Thanks for the help!

  • 1

    @Thiagoluizdomacoski, your line of reasoning was right too! Thanks for the help.

1 answer

2


You are passing the link in the Intent with the name "link_file" and recovering the Extra using another name ,"link", soon it will be null. The names have to be the same.

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

  • I corrected the statement, it was just an example, the code is right.

  • In theory there is nothing wrong with the code then. Already debugged and verified that at putExtra the variable link is with a valid value?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.