0

I have to open pdf file stored in asset folder. When I am trying to open the file in Android 6, it shows error. But, no problem with other android versions. I think its the problem with permission. Please help to rectify this error.

Here is my code ....

dialBtn.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {



            File fileBrochure = new File(Environment.getExternalStorageDirectory() + "/" + email+".pdf");
            if (!fileBrochure.exists())
            {
                CopyAssetsbrochure();
            }

            /** PDF reader code */
            File file = new File(Environment.getExternalStorageDirectory() + "/" + email+".pdf");

            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setDataAndType(Uri.fromFile(file),"application/pdf");
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            try
            {
                getApplicationContext().startActivity(intent);
            }
            catch (ActivityNotFoundException e)
            {
                Toast.makeText(getApplicationContext(), "Please install any pdf reader App.",
                        Toast.LENGTH_LONG).show();
                startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=com.adobe.reader")));
            }
        }
    });


}

private void CopyAssetsbrochure() {
    AssetManager assetManager = getAssets();
    String[] files = null;
    try
    {
        files = assetManager.list("");
    }
    catch (IOException e)
    {
        Log.e("tag", e.getMessage());
    }
    for(int i=0; i<files.length; i++)
    {
        String fStr = files[i];
        if(fStr.equalsIgnoreCase(email+".pdf"))
        {
            InputStream in = null;
            OutputStream out = null;
            try
            {
                in = assetManager.open(files[i]);
                out = new FileOutputStream(Environment.getExternalStorageDirectory() + "/" + files[i]);
                copyFile(in, out);
                in.close();
                in = null;
                out.flush();
                out.close();
                out = null;
                break;
            }
            catch(Exception e)
            {
                Log.e("tag", e.getMessage());
            }
        }
    }
}

private void copyFile(InputStream in, OutputStream out) throws IOException {
    byte[] buffer = new byte[1024];
    int read;
    while((read = in.read(buffer)) != -1){
        out.write(buffer, 0, read);
    }
}
Shemil
  • 387
  • 1
  • 7
  • 12
  • `void CopyAssetsbrochure()`. Make that `boolean CopyAssetsbrochure()` and check return value before you continue with your code. – greenapps Mar 03 '17 at 11:34

1 Answers1

1

Did you included: <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> and: <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> in the manifest?

Also I have found this code:

public class SampleActivity extends Activity {

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

    }

    private void CopyReadAssets()
    {
        AssetManager assetManager = getAssets();

        InputStream in = null;
        OutputStream out = null;
        File file = new File(getFilesDir(), "abc.pdf");
        try
        {
            in = assetManager.open("abc.pdf");
            out = openFileOutput(file.getName(), Context.MODE_WORLD_READABLE);

            copyFile(in, out);
            in.close();
            in = null;
            out.flush();
            out.close();
            out = null;
        } catch (Exception e)
        {
            Log.e("tag", e.getMessage());
        }

        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(
                Uri.parse("file://" + getFilesDir() + "/abc.pdf"),
                "application/pdf");

        startActivity(intent);
    }

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

}

Here is the link of another answer: Read a pdf file from assets folder

Community
  • 1
  • 1
Angelo Parente
  • 796
  • 2
  • 6
  • 15
  • its problem with File. – Shemil Mar 04 '17 at 08:49
  • This is a link of one of my project on GitHub. In my case I had to read a .JSON file. And my code works fine. This is the link to the class: https://github.com/angeloparenteapp/Italian-Recipes/blob/master/app/src/main/java/com/angeloparenteapp/italianrecipes/FetchRecipesJson.java In your case is a .PDF file but the code should be similar – Angelo Parente Mar 04 '17 at 13:44
  • finally i resolved the problem. Its due to ask permission. – Shemil Mar 07 '17 at 08:37