简体   繁体   English

无法在应用程序中打开.pdf文件

[英]Unable to open .pdf file in the application

I am try to display a .pdf document in my application. 我尝试在我的应用程序中显示一个.pdf文档。 I am unable to display it. 我无法显示它。 I am getting invalid document path error with adobe reader and pdf viewer display file cannot be opened. 我在Adobe Reader中收到无效的文档路径错误,并且无法打开pdf查看器显示文件。 Please any one of you let me know what mistake am I doing here. 请你们中的任何一个让我知道我在这里做错了什么。 If there is a better way in achieving this please teach me. 如果有更好的方法可以做到这一点,请教我。 I have posted the code which I am using: 我已经发布了我正在使用的代码:

public class HelpScreen extends ActionBarActivity {

    Toolbar toolbar;

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

        // Initializing, setting text and color of tool bar
        toolbar = (Toolbar) findViewById(R.id.appBar);
        setSupportActionBar(toolbar);
        toolbar.setTitleTextColor(Color.WHITE);

        CopyReadAssets();
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_help_screen, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }


    private void CopyReadAssets()
    {
        AssetManager assetManager = getAssets();
        Log.d("Pana", "The value of assests is " +assetManager);

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

            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() + "/" + "/help_document_task_management_system_document_4.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);
        }
    }
}

EDITED CODE: 编辑代码:

public class HelpScreen extends ActionBarActivity {

    Toolbar toolbar;

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

        // Initializing, setting text and color of tool bar
        toolbar = (Toolbar) findViewById(R.id.appBar);
        setSupportActionBar(toolbar);
        toolbar.setTitleTextColor(Color.WHITE);

        CopyReadAssets();
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_help_screen, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }


    private void CopyReadAssets() {
        AssetManager assetManager = getAssets();
        Log.d("Pana", "The value of assests is " + assetManager);

        InputStream in = null;
        OutputStream out = null;
        File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "help_document_task_management_system_document_4.pdf");
        // File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "help_document_task_management_system_document_4.pdf");
        try {
            in = assetManager.open("help_document_task_management_system_document_4.pdf");
            out = openFileOutput(file.getName(), Context.MODE_PRIVATE);

            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.fromFile(file), "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);
        }
    }
}

Please let me know my mistake and help me come out of this. 请让我知道我的错误,并帮助我解决这个问题。 Thanks in advance. 提前致谢。

Third party apps have no rights to access that file. 第三方应用无权访问该文件。 Use FileProvider to serve it, or copy the file to external storage instead of internal storage . 使用FileProvider为其提供服务,或将文件复制到外部存储而不是内部存储

意图创建中,在getFilesDir()之后的文件路径中似乎有两个/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM