6

Now I am working on an application. Through my app users can read pdf files and if pdf reader is not there then my app automatically will install it from the site. This is the code I used for reading pdf file.

File file = new File("/sdcard/sample.pdf");
PackageManager packageManager = getPackageManager();
Intent testIntent = new Intent(Intent.ACTION_VIEW);
testIntent.setType("application/pdf");
List list = packageManager.queryIntentActivities(testIntent, PackageManager.MATCH_DEFAULT_ONLY);
if (list.size() > 0 && file.isFile()) {
    Intent intent = new Intent();
    intent.setAction(Intent.ACTION_VIEW);
    Uri uri = Uri.fromFile(file);
    intent.setDataAndType(uri, "application/pdf");
    startActivity(intent);
}

My doubts are:

  1. How to check there is a adobe reader installed in phone or not?
  2. How to programatically install the adobe reader on a phone?
Yury
  • 20,618
  • 7
  • 58
  • 86
sarath
  • 3,181
  • 7
  • 36
  • 58

1 Answers1

20

From your code some complication..

Use this code,

Intent intent;
        intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(file, "application/pdf");
        try {
            startActivity(intent);
        } catch (ActivityNotFoundException e) {
            // No application to view, ask to download one
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("No Application Found");
            builder.setMessage("Download one from Android Market?");
            builder.setPositiveButton("Yes, Please",
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            Intent marketIntent = new Intent(Intent.ACTION_VIEW);
                            marketIntent
                                    .setData(Uri
                                            .parse("market://details?id=com.adobe.reader"));
                            startActivity(marketIntent);
                        }
                    });
            builder.setNegativeButton("No, Thanks", null);
            builder.create().show();
        }
    }
user370305
  • 108,599
  • 23
  • 164
  • 151
  • thank u very much.....i tested ur code in emulater...that alertdialog box came .and i press ok then it gives exception.. what u think...its because i tested in emulater..will it work in phone?? – sarath Feb 28 '12 at 10:50
  • @sarath the emulator doesn't come with the marketplace application, see http://stackoverflow.com/questions/3994923/no-marketplace-application-on-the-android-emulator – TerryProbert Feb 28 '12 at 10:53
  • Have you downloaded Adobe reader by programming using this code? – user370305 Feb 29 '12 at 06:51
  • when i worked in pc emulater its opening the file if installed the pdf reader otherwise app is showing a alertbox with options .But when I run it in phone app is showing file cant open. My phone is not having adobe reader so it must shows a alertdialog.But its just showing file cant open without any alertdialogbox – sarath Feb 29 '12 at 06:57
  • Its because your phone has a any pdf reader which can't able to open that file. Be sure the condition is only for check Intent type "application/pdf" not for application adobe reader if any application available on your device which handle this intent type then dialog doesn't appear. – user370305 Feb 29 '12 at 07:01
  • please help me...how can i test this app in my phone?? – sarath Feb 29 '12 at 07:07
  • Just make a simple demo activity and write code of catch block in onCreate of that activity and just run it in device. To check download dialog. – user370305 Feb 29 '12 at 07:21
  • I did this but instead of launching the market with a specific app i put "market://search?q=pdf&c=apps" so they could choose. – Travis Castillo May 14 '13 at 20:43