Skip to content Skip to sidebar Skip to footer

Trying To Open Pdf File From Assets Folder Using Fileprovider But It Gives Filenotfoundexception: No Such File Or Directory

I am trying to display 'NEFT.pdf' file stored in the asset folder of my android app. The following code works absolutely fine till API 25 private void CopyReadAssets(String filena

Solution 1:

Cause you wanna display the PDF file in a separate application (such as Adobe Reader), I would prefer doing the following:-

privatevoidCopyReadAssets()
        {
            AssetManagerassetManager= getActivity().getAssets();

            InputStreamin=null;
            OutputStreamout=null;
            Stringstate= Environment.getExternalStorageState();
            if (!Environment.MEDIA_MOUNTED.equals(state)) {
                Toast.makeText(getActivity(), "External Storage is not Available", Toast.LENGTH_SHORT).show();
            }
            FilepdfDir=newFile(Environment.getExternalStorageDirectory().getAbsolutePath() + "/PDFs");
            if (!pdfDir.exists()) {
                pdfDir.mkdir();
            }
            Filefile=newFile(pdfDir + "/abc.pdf");

            try
            {
                in = assetManager.open("abc.pdf");
                out = newBufferedOutputStream(newFileOutputStream(file));
                copyFile(in, out);
                in.close();
                in = null;
                out.flush();
                out.close();
                out = null;
            } catch (Exception e)
            {
                Log.e("tag", e.getMessage());
            }
            if (file.exists()) //Checking for the file is exist or not
            {
                Uripath= Uri.fromFile(file);
                IntentobjIntent=newIntent(Intent.ACTION_VIEW);
                objIntent.setDataAndType(path, "application/pdf");
                objIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                Intentintent1= Intent.createChooser(objIntent, "Open PDF with..");
                try {
                    startActivity(intent1);
                } catch (ActivityNotFoundException e) {
                    Toast.makeText(getActivity(), "Activity Not Found Exception ", Toast.LENGTH_SHORT).show();
                }
            } else {
                Toast.makeText(getActivity(), "The file not exists! ", Toast.LENGTH_SHORT).show();
            }
        }

For copy file into device memory:-

privatevoidcopyFile(InputStream in, OutputStream out) throws IOException
    {
        byte[] buffer = newbyte[1024];
        int read;
        while ((read = in.read(buffer)) != -1)
        {
            out.write(buffer, 0, read);
        }
    }

Use the below Permission

<uses-permissionandroid:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Post a Comment for "Trying To Open Pdf File From Assets Folder Using Fileprovider But It Gives Filenotfoundexception: No Such File Or Directory"