无法在外部应用程序中打开PDF文件

安德烈·维迪切夫(Andrey E.

当用户单击按钮时,我想打开一个PDF文件。目前,我正在使用此代码来实现此目的:

Uri path = Uri.fromFile(new File("file:///android_asset/regola11_1.pdf"));
            Intent pdfIntent = new Intent(Intent.ACTION_VIEW);
            pdfIntent.setDataAndType(path, "application/pdf");
            pdfIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(pdfIntent);

但这不起作用。

当我选择使用Adobe Acrobat时,我收到一条消息,显示为Toast,上面写着

"This file could not be accessed Check the location or the network and try again."

当我尝试使用Drive PDF Viewer时,我得到

"Cannot display PDF ( regola11_1.pdf cannot be opened)"

PDF文件存储在

app > build > intermediates > assets

哪里有问题?

编辑

现在我正在使用以下代码:

File file = new File("\"file:///android_asset/regola11_1.pdf");
            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 {
                context.startActivity(intent);
            }
            catch (ActivityNotFoundException e) {
                Toast.makeText(context, "No application available to view PDF", Toast.LENGTH_LONG).show();
            }

但是,当我尝试通过单击按钮打开PDF时,应用程序崩溃了。

这是我得到的日志:

05-31 10:05:25.132  24474-24474/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.andrey.andreyvedis.iamaref, PID: 24474
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.content.Context.startActivity(android.content.Intent)' on a null object reference
        at com.andrey.andreyvedis.iamaref.FragmentR11.onClick(FragmentR11.java:147)
        at android.view.View.performClick(View.java:4781)
        at android.view.View$PerformClick.run(View.java:19873)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5293)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)

这是我的课:

public class FragmentR11 extends Fragment implements  View.OnClickListener{
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
Context context;



// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;


/**
 * Use this factory method to create a new instance of
 * this fragment using the provided parameters.
 *
 * @param param1 Parameter 1.
 * @param param2 Parameter 2.
 * @return A new instance of fragment FragmentR11.
 */
// TODO: Rename and change types and number of parameters
public static FragmentR11 newInstance(String param1, String param2) {
    FragmentR11 fragment = new FragmentR11();
    Bundle args = new Bundle();
    args.putString(ARG_PARAM1, param1);
    args.putString(ARG_PARAM2, param2);
    fragment.setArguments(args);
    return fragment;
}

public FragmentR11() {
    // Required empty public constructor
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getArguments() != null) {
        mParam1 = getArguments().getString(ARG_PARAM1);
        mParam2 = getArguments().getString(ARG_PARAM2);
    }



}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_r11, container, false);


}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    getActivity().findViewById(R.id.bD1).setOnClickListener(this);
    getActivity().findViewById(R.id.bD2).setOnClickListener(this);
    getActivity().findViewById(R.id.bD3).setOnClickListener(this);
    getActivity().findViewById(R.id.bD4).setOnClickListener(this);
    getActivity().findViewById(R.id.bD5).setOnClickListener(this);
    getActivity().findViewById(R.id.bD6).setOnClickListener(this);
    getActivity().findViewById(R.id.bD7).setOnClickListener(this);
    getActivity().findViewById(R.id.bD8).setOnClickListener(this);
    getActivity().findViewById(R.id.bD9).setOnClickListener(this);
    getActivity().findViewById(R.id.bD10).setOnClickListener(this);
    getActivity().findViewById(R.id.bD11).setOnClickListener(this);
    getActivity().findViewById(R.id.bD12).setOnClickListener(this);
    getActivity().findViewById(R.id.bD13).setOnClickListener(this);
    getActivity().findViewById(R.id.bD14).setOnClickListener(this);
    getActivity().findViewById(R.id.bD15).setOnClickListener(this);
    getActivity().findViewById(R.id.bD16).setOnClickListener(this);
    getActivity().findViewById(R.id.bD17).setOnClickListener(this);


}

/**private void openPDF(final String pathToPDF) {
    File file = new File(pathToPDF);
    Uri path = Uri.fromFile(file);
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    intent.setDataAndType(path, "application/pdf");
    try {
        startActivity(intent);
    } catch (ActivityNotFoundException e) {
        Toast.makeText(getActivity(), "Devi installare un'app per aprire PDF, come Adobe Acrobat Reader ", Toast.LENGTH_SHORT).show();
    }
}*/


@Override
public void onClick(View v) {



    switch(v.getId()){

        case R.id.bD1: {

            /**Uri path = Uri.fromFile(new File("regola11_1.pdf"));
            Intent pdfIntent = new Intent(Intent.ACTION_VIEW);
            pdfIntent.setDataAndType(path, "application/pdf");
            pdfIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(pdfIntent);
            Toast.makeText(getActivity(), "Hai cliccato Regola 1 in Reg11 ", Toast.LENGTH_SHORT).show();*/

            File file = new File("\"file:///android_asset/regola11_1.pdf");
            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 {
                context.startActivity(intent);
            }
            catch (ActivityNotFoundException e) {
                Toast.makeText(context, "No application available to view PDF", Toast.LENGTH_LONG).show();
            }
            break;
        }

        case R.id.bD2:

        {
            Toast.makeText(getActivity(), "Hai cliccato Regola 2 in Reg11 ", Toast.LENGTH_SHORT).show();

            break;
        }


        case R.id.bD3:

        {
            Toast.makeText(getActivity(), "Hai cliccato Regola 3 in Reg11 ", Toast.LENGTH_SHORT).show();

            break;
        }

        case R.id.bD4:

        {
            Toast.makeText(getActivity(), "Hai cliccato Regola 4 in Reg11 ", Toast.LENGTH_SHORT).show();

            break;
        }

        case R.id.bD5:

        {
            Toast.makeText(getActivity(), "Hai cliccato Regola 5 in Reg11 ", Toast.LENGTH_SHORT).show();

            break;
        }

        case R.id.bD6:

        {
            Toast.makeText(getActivity(), "Hai cliccato Regola 6 in Reg11 ", Toast.LENGTH_SHORT).show();

            break;
        }

        case R.id.bD7:

        {
            Toast.makeText(getActivity(), "Hai cliccato Regola 7 in Reg11 ", Toast.LENGTH_SHORT).show();

            break;
        }

        case R.id.bD8:

        {
            Toast.makeText(getActivity(), "Hai cliccato Regola 8 in Reg11 ", Toast.LENGTH_SHORT).show();

            break;
        }

        case R.id.bD9:

        {
            Toast.makeText(getActivity(), "Hai cliccato Regola 9 in Reg11 ", Toast.LENGTH_SHORT).show();

            break;
        }

        case R.id.bD10:

        {
            Toast.makeText(getActivity(), "Hai cliccato Regola 10 in Reg11 ", Toast.LENGTH_SHORT).show();

            break;
        }

        case R.id.bD11:

        {
            Toast.makeText(getActivity(), "Hai cliccato Regola 11 in Reg11 ", Toast.LENGTH_SHORT).show();

            break;
        }

        case R.id.bD12:

        {
            Toast.makeText(getActivity(), "Hai cliccato Regola 12 in Reg11 ", Toast.LENGTH_SHORT).show();

            break;
        }

        case R.id.bD13:

        {
            Toast.makeText(getActivity(), "Hai cliccato Regola 13 in Reg11 ", Toast.LENGTH_SHORT).show();

            break;
        }

        case R.id.bD14:

        {
            Toast.makeText(getActivity(), "Hai cliccato Regola 14 in Reg11 ", Toast.LENGTH_SHORT).show();

            break;
        }

        case R.id.bD15:

        {
            Toast.makeText(getActivity(), "Hai cliccato Regola 15 in Reg11 ", Toast.LENGTH_SHORT).show();

            break;
        }

        case R.id.bD16:

        {
            Toast.makeText(getActivity(), "Hai cliccato Regola 16 in Reg11 ", Toast.LENGTH_SHORT).show();

            break;
        }

        case R.id.bD17:

        {
            Toast.makeText(getActivity(), "Hai cliccato Regola 17 in Reg11 ", Toast.LENGTH_SHORT).show();

            break;
        }


    }

}

}

有人能帮我吗?

编辑2

我找到了解决方案,请在下面的代码中查看我的答案。

谢谢大家的答案。

安德烈·维迪切夫(Andrey E.

好的,问题解决了!

这是我用来打开存储在外部存储器中的PDF文件的代码:

File pdfFile = new File(Environment.getExternalStorageDirectory(),"namePdfFile.pdf");//File path
            if (pdfFile.exists()) //Checking if the file exists or not
            {
                Uri path = Uri.fromFile(pdfFile);
                Intent objIntent = new Intent(Intent.ACTION_VIEW);
                objIntent.setDataAndType(path, "application/pdf");
                objIntent.setFlags(Intent. FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(objIntent);//Starting the pdf viewer
            } else {

                Toast.makeText(getActivity(), "The file not exists! ", Toast.LENGTH_SHORT).show();

            }

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

从netrw在外部应用程序中打开文件?

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

在Chrome上的外部应用程序中打开PDF链接

在外部浏览器中从FB应用程序打开PWA

单击按钮在默认 pdf 查看器应用程序中打开外部存储中的 pdf 文件

python错误无法在python应用程序中打开pdf

我想在我的应用程序中打开 .pdf 文件

如何使Google Chrome浏览器的“应用程序模式”在外部浏览器中打开外部链接?

在外部 Applescript 中获取 Automator 应用程序结果?

无法在Spring Web应用程序中打开属性文件

无法在 QuickLook 应用程序中打开 USDZ 文件

无法在我的应用程序中显示PDF文件

为什么使用FileProvider我无法使用外部应用程序从INTERNAL STORAGE打开文件?

应用程序无法在牛轧糖中打开

无需在外部JavaScript文件中对Web应用程序的上下文路径进行硬编码

无法通过“在<我的应用程序>”中打开“打开”文件从iOS文件应用程序打开文件:提供的文件URL上没有文件

无法在Phonegap应用中打开pdf文件

使用桌面的默认应用程序打开存储在jar文件中的PDF文件

使用Java上的外部应用程序打开文件

如何在自制的Mac OS应用程序中打开外部文件

在Windows RT应用程序中复制或打开外部文件

AndroidPDFViewer-无法在我的应用程序中打开pdf文档

Swagger 2 UI无法访问,Spring Boot应用程序部署在外部tomcat上

从Java应用程序即时打开PDF文件

如何在Xamarin Forms的默认应用程序中打开PDF或TXT文件

在第 3 方应用程序中打开 pdf 文件的权限

打开命令以在应用程序中打开文件

从以守护程序启动的应用程序中打开外部应用程序

在 ZK 中打开外部应用程序和网页