Open pdf by URL and Inputstream Pdfview

Asked

Viewed 296 times

0

I would like the user to enter the product code (at activity_main.xml) and click the button.

The screen changes (view_pdf.xml), would make a PDF download (AnsyTask) and would open on PDFView for pdfView.fromStream.
Using Android Studio and Spring Boot.

Mainactivity

public class MainActivity extends AppCompatActivity {
    private ProgressDialog load;
    private TextView codigoModel;
    private Button bt_busca;
    private PDFView pdfView;
    private ProgressDialog loadPdf;

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

        codigoModel = (TextView) findViewById(R.id.codigoModelo);
        bt_busca = (Button) findViewById(R.id.bt_buscar);
        pdfView = (PDFView) findViewById(R.id.pdfView);

        bt_busca.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setContentView(R.layout.view_pdf);
                chamarAsyncTask(codigoModel.getText().toString());
            }
        });
    }

    private void chamarAsyncTask(String codigoModel) {
        DownloadPdfView downloadPdfView = new DownloadPdfView();
        Log.i("AsyncTask", "AsyncTask senado chamado Thread: " + Thread.currentThread().getName());
        downloadPdfView.execute(codigoModel);
    }

    private class DownloadPdfView extends AsyncTask<String, Void, InputStream> {

        @Override
        protected void onPreExecute() {
            Log.i("AsyncTask", "Exibindo ProgressDialog na tela Thread: " + Thread.currentThread().getName());
            loadPdf = ProgressDialog.show(MainActivity.this, "Por Favor Aguarde ...",
                    "Baixando Desenho ...");

        }

        @Override
        protected InputStream doInBackground(String... params) {
        InputStream inputStream;
        AcessoWebService url = new AcessoWebService();
        String codigo = params[0];
        Log.i("AsyncTask", "Baixando o PDF Thread: " + Thread.currentThread().getName());

        AcessoWebService acessoWebService = new AcessoWebService();
        inputStream = acessoWebService.getApiInputStream("http://192.168.100.100:8080/produto/download/" + codigo);

        return inputStream;
        }

        @Override
        protected void onPostExecute(InputStream inputStream) {
            pdfView.fromStream(inputStream).load();
        }
    }
}  

Acessowebservice

    public class AcessoWebService {

    public InputStream getApiInputStream(String url) {

        int codigoResposta;
        HttpURLConnection conexao;
        InputStream inputStream = null;

        try {

            URL apiEnd = new URL(url);
            conexao = (HttpURLConnection) apiEnd.openConnection();
            conexao.setRequestMethod("GET");
            conexao.setReadTimeout(15000);
            conexao.setConnectTimeout(15000);
            conexao.connect();

            codigoResposta = conexao.getResponseCode();
            if (codigoResposta < HttpURLConnection.HTTP_BAD_REQUEST) {
                inputStream = new BufferedInputStream(conexao.getInputStream());
            } else {
                inputStream = conexao.getErrorStream();
            }

            inputStream.close();
            conexao.disconnect();

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return inputStream;
    }
}  

Dependencies

//Add Library
compile 'com.github.barteksc:android-pdf-viewer:2.8.2'
compile 'org.apache.commons:commons-collections4:4.1'  

Exception

E/AndroidRuntime: FATAL EXCEPTION: main
                                                                        Process: br.com.mobile, PID: 16834
                                                                        java.lang.NullPointerException: Attempt to invoke virtual method 'com.github.barteksc.pdfviewer.PDFView$Configurator com.github.barteksc.pdfviewer.PDFView.fromStream(java.io.InputStream)' on a null object reference  
    at br.com.mobile.MainActivity$DownloadPdfView.onPostExecute(MainActivity.java:100)
                                                                            at br.com.mobile.MainActivity$DownloadPdfView.onPostExecute(MainActivity.java:54)

1 answer

0


Instead of "change screen" with setContentView() should use another Activity.

From what I understand the PDF view(R.id.pdfView) should be in the layout R.layout.view_pdf and not in the R.layout.activity_main.

So you can only get a reference to it after using setContentView(R.layout.view_pdf).

Change the method onClick() for:

bt_busca.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        setContentView(R.layout.view_pdf);
        pdfView = (PDFView) findViewById(R.id.pdfView);
        chamarAsyncTask(codigoModel.getText().toString());
    }
});
  • Thanks ramaral. I tested your answer and worked correctly. I’m already modifying to call another Activity, as the tip above.

Browser other questions tagged

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