-1

I've been looking for various tutorials but no one can solve my case. I hope here is something that can make me understand, about how to open PDF file automatically after finished download from database to android.

This is my download script.

@Override
public void onBindViewHolder(HolderData holder, int position) {
    final ModelData md = mItems.get(position);
    holder.txtname.setText(md.getName());
    holder.txtwaktu.setText(md.getWaktu());

    //Proses Downloading
    holder.relativeLayoutMateri.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            uri = String.valueOf(Uri.parse("http://192.168.43.144/MLearning/crud/"+md.getPath()));
            dm = (DownloadManager)context.getSystemService(Context.DOWNLOAD_SERVICE);
            DownloadManager.Request request = new DownloadManager.Request(Uri.parse(uri));
            request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
            longid = dm.enqueue(request);

            Toast.makeText(context, md.getName()+" Berhasil Di Download"+md.getPath(), Toast.LENGTH_SHORT).show();
        }
    });
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • *"saya baru belajar tentang android, jadi saya harap penjelasan yang bisa membuat saya mengerti. sebelumnya terimakasih"* This area of SO is conducted in English, so please use the best English you can manage. Comment deleted as it was extraneous & irrelevant anyway. (There's no need to mention you are 'new to X'.) – Andrew Thompson Jul 25 '18 at 00:24
  • I think this can help you https://stackoverflow.com/questions/5877753/android-how-to-use-download-manager-class – Crammeur Jul 25 '18 at 01:39

1 Answers1

0

Here is the solution please use ACTION_VIEW Intent to open all you need the path of downloaded pdf.

 private static String filepath = Environment.getExternalStorageDirectory().getPath()+"/myfile.pdf";   

    File file = new File(filepath);

    if (file.exists()) {
        Uri path = Uri.fromFile(file);
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(path, "application/pdf");
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

        try {
            startActivity(intent);
        } 
        catch (ActivityNotFoundException e) {
            Log.d(TAG,e.getMessage());
        }
    }
Umer Farooq
  • 168
  • 1
  • 12
  • when you save file in internal memory it will return a file path just add that path in file path variable . – Umer Farooq Jul 26 '18 at 11:51
  • sorry, the path variable comes from the database, which is the material / file.pdf what is the address that will be called? or location of the file after download. maap too much betanya. before I use the code above, it works open automatically, but the data does not appear. #by google translate. Sorry – Sulaiman Yahya Jul 27 '18 at 10:41