0

i am trying to open a pdf file in my app.. i have installed the android pdf viewer in my emulator.. am using the following code.. "https://github.com/jesperborgstrup/buzzingandroid/blob/master/src/com/buzzingandroid/tools/PDFTools.java"
now i have added the pdf file in my assets folder.. this is my code..

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button;
        final String url = "android.resource://com.buzzingandroid.tools/raw/ll.pdf";
        final PDFTools pdf = new PDFTools();
        button = (Button)this.findViewById(R.id.button1);
        button.setOnClickListener(new OnClickListener() {

            @SuppressWarnings("static-access")
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                pdf.showPDFUrl(Context,url );
            }
        });

the prg crashes in the "ispdfsupported" module. the module is given below..

public static boolean isPDFSupported( Context context ) {
        Intent i = new Intent( Intent.ACTION_VIEW );
        final File tempFile = new File( context.getExternalFilesDir( Environment.DIRECTORY_DOWNLOADS ), "test.pdf" );
        i.setDataAndType( Uri.fromFile( tempFile ), PDF_MIME_TYPE );
        return context.getPackageManager().queryIntentActivities( i, PackageManager.MATCH_DEFAULT_ONLY ).size() > 0;
    }

and my log cat..

02-24 01:08:59.912: E/AndroidRuntime(1172): FATAL EXCEPTION: main
02-24 01:08:59.912: E/AndroidRuntime(1172): Process: com.buzzingandroid.tools, PID: 1172
02-24 01:08:59.912: E/AndroidRuntime(1172): java.lang.NullPointerException
02-24 01:08:59.912: E/AndroidRuntime(1172):     at com.buzzingandroid.tools.PDFTools.isPDFSupported(PDFTools.java:142)
02-24 01:08:59.912: E/AndroidRuntime(1172):     at com.buzzingandroid.tools.PDFTools.showPDFUrl(PDFTools.java:38)
02-24 01:08:59.912: E/AndroidRuntime(1172):     at com.buzzingandroid.tools.MAIN$1.onClick(MAIN.java:30)
02-24 01:08:59.912: E/AndroidRuntime(1172):     at android.view.View.performClick(View.java:4438)
02-24 01:08:59.912: E/AndroidRuntime(1172):     at android.view.View$PerformClick.run(View.java:18422)
02-24 01:08:59.912: E/AndroidRuntime(1172):     at android.os.Handler.handleCallback(Handler.java:733)
02-24 01:08:59.912: E/AndroidRuntime(1172):     at android.os.Handler.dispatchMessage(Handler.java:95)
02-24 01:08:59.912: E/AndroidRuntime(1172):     at android.os.Looper.loop(Looper.java:136)
02-24 01:08:59.912: E/AndroidRuntime(1172):     at android.app.ActivityThread.main(ActivityThread.java:5017)
02-24 01:08:59.912: E/AndroidRuntime(1172):     at java.lang.reflect.Method.invokeNative(Native Method)
02-24 01:08:59.912: E/AndroidRuntime(1172):     at java.lang.reflect.Method.invoke(Method.java:515)
02-24 01:08:59.912: E/AndroidRuntime(1172):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
02-24 01:08:59.912: E/AndroidRuntime(1172):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
02-24 01:08:59.912: E/AndroidRuntime(1172):     at dalvik.system.NativeStart.main(Native Method)

please explain what i did wrong.. thanks in advance..

also i have given an url to access a pdf from server.. it crashed in the same place.. same error in log cat...

user3214173
  • 227
  • 3
  • 14

1 Answers1

0

Try the below code. I am using this code for open PDF. You can use it for other files also.

File file = new File(Environment.getExternalStorageDirectory(),
             "Report.pdf");
    Uri path = Uri.fromFile(file);
    Intent pdfOpenintent = new Intent(Intent.ACTION_VIEW);
    pdfOpenintent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    pdfOpenintent.setDataAndType(path, "application/pdf");
    try {
        startActivity(pdfOpenintent);
    } catch (ActivityNotFoundException e) {

    }

If you want to open files. You can change the setDataAndType(path, "application/pdf"). If you want to open different files with same intent you can use Intent.createChooser(intent, "Open in...");. For more information look below link

How to make an intent with multiple actions

Community
  • 1
  • 1
Ameer
  • 2,709
  • 1
  • 28
  • 44