0

I can assure the in my phone I have a pdf file named "adobe.pdf" at /storage/emulated/0/Documents/adobe.pdf

I downloaded the pdf through a URL link and the stored in Documents. Here is how I downloaded it and stored it. This is another activity within the same app.


    public  void DownloadBooks(String url,String title){

        DownloadManager.Request request=new DownloadManager.Request(Uri.parse(url));
        String tempTitle=title.trim();
        request.setTitle(tempTitle);
        if(Build.VERSION.SDK_INT>= Build.VERSION_CODES.HONEYCOMB){
            request.allowScanningByMediaScanner();
            request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
        }
        request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOCUMENTS,tempTitle+".pdf");
        DownloadManager downloadManager=(DownloadManager)getSystemService(Context.DOWNLOAD_SERVICE);
        request.setMimeType("application/pdf");
        request.allowScanningByMediaScanner();
        request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_MOBILE | DownloadManager.Request.NETWORK_WIFI);
        downloadManager.enqueue(request);
    }
DownloadBooks("a link"," adobe");

After the download has completed the pdf adobe.pdf is stored at /storage/emulated/0/Documents/ and I want to view this downloaded PDF.

I am using the library to display the PDF but it is not visible. Here is my code

package com.shivansh.firebasedemo;



public class DownloadedPDFViewerActivity extends AppCompatActivity implements ViewPager.OnPageChangeListener, OnLoadCompleteListener {
    PDFView pdfView;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_downloaded_p_d_f);

        
        pdfView =findViewById(R.id.pdfView);
        File root = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS);
        File file = new File(root,"adobe.pdf");
        Log.i("5656",file.toString());
        pdfView.fromFile(file).
        enableSwipe(true)
        .defaultPage(0)
        .swipeHorizontal(false)

        .onLoad(this)
        .load();

    }
    
    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {

    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    @Override
    public void loadComplete(int nbPages) {
    }
}

The above log statements prints

2021-05-06 03:13:31.734 9470-9470/com.shivansh.firebasedemo I/5656: /storage/emulated/0/Documents/adobe.pdf

Which suggests that I have located the correct location of the file on the device.

ScreenShot of the PDF path from my phone

Now , how do I make this pdf to render in my activity ?

Randy Orton
  • 33
  • 1
  • 7
  • Android version of used device? What did you do to obtain read permissions? – blackapps May 05 '21 at 22:14
  • Also use file.exists() and file.canRead(). – blackapps May 05 '21 at 22:15
  • Hello there, I am on Android 11. I used `` to obtain read permissions – Randy Orton May 05 '21 at 23:04
  • the output for `file.exists()` is `true` and for ` file.canRead()` is `false`, then how can I read this file it is the same pdf file that I downloaded @blackapps you know the deal :) – Randy Orton May 05 '21 at 23:09
  • Who downloaded that file to begin with? And how does your app know that file path? – blackapps May 06 '21 at 07:30
  • On Android 11 the owner of the file is important. Apps can mostly not see the files of other apps. Unless they obtained special file manager rights. – blackapps May 06 '21 at 07:32
  • I have updated my post, briefly I downloaded the file from this app only then saved to Documents folder the now I am interested in viewing this downloaded pdf file with this library – Randy Orton May 06 '21 at 08:21
  • ???? I asked you to not only tell who downloadedand how but also to show your code for it. We still know nothing yet. – blackapps May 06 '21 at 08:26
  • I have already posted the code for the same in my updated post kindly check it. – Randy Orton May 06 '21 at 08:39
  • Aparently the file belongs to the DownloadManager then and not to your app. But.. You should register a broadcast receiver for ACTION_DOWNLOAD_COMPLETE. In onReceive() you then can get an uri for the downloaded file. Use that uri to access it. – blackapps May 06 '21 at 08:42
  • Let me do that and then get right back at you :) – Randy Orton May 06 '21 at 08:47
  • Meanwhile i checked what i said about the owner of the file. But my app has full access to that file in Download which was downloaded by DownloadManager. Are you shure your path is correct? – blackapps May 06 '21 at 08:48
  • Sir, I have provided the output of the log statement and also the screenshot of my phone just to confirm that that path is actually correct, please do check out the screenschot it clearly shows the path of the pdf and according to the code also that is the exact path , hence I can say that the path of the pdf is correct – Randy Orton May 06 '21 at 08:55
  • The pdf gets store in the Documents directory and I have used `File root = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS);` and then `File file = new File(root,"adobe.pdf");` to fetch the file so everything seems civil to me . And also do I need to check for broadcast receiver for ACTION_DOWNLOAD_COMPLETE thing now ? cuz you say that the app can read those files. – Randy Orton May 06 '21 at 09:00
  • Well if it does not work for you now then try what i suggested i would say. Dont understand your hesitation. – blackapps May 06 '21 at 09:26

0 Answers0