0

now I am doing an Android application.I have to read a file from sdcard.I installed adobe reader in my emulater.And i used the following code to run the file from sdcard.

intent = new Intent(Intent.ACTION_VIEW);
    intent.setDataAndType(Uri.parse("/sdcard/samp.pdf"), "application/pdf");
    try{

        startActivity(intent);
    }
    catch (ActivityNotFoundException e) {
        System.out.println("5");
        // No application to view, ask to download one
        System.out.println("hai");
        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() {
                    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();
    }

I inserted a pdf named sample.pdf in sdcard.But when I am running my application I am getting "the file could not be opened". But i can open the pdf manually from the emulater.Please help me to find out a solution.

Hi friends I got the solution. I removed intent.setDataAndType(Uri.parse("/sdcard/samp.pdf"), "application/pdf"); and add

 File file = new File("/sdcard/sample.pdf");
    Uri uri = Uri.fromFile(file);
    intent.setDataAndType(uri, "application/pdf");

now its working. But now I put the sample.pdf folder in my assets folder and write the following code.

 File file = new File("android.resource://com.pdftest/assets/sample");
    Uri uri = Uri.fromFile(file);
    intent.setDataAndType(uri, "application/pdf"); 

and I getting file path not valid message...

sarath
  • 3,181
  • 7
  • 36
  • 58

4 Answers4

3

You have:

 File file = new File("android.resource://com.pdftest/assets/sample");

Shouldn't it be:

File file = new File("android.resource://com.pdftest/assets/sample.pdf");
Jack
  • 10,943
  • 13
  • 50
  • 65
2

The Uri must contain more than just the path. If you look at the content of your Uri, it is empty. To have a proper Uri, use Uri.fromFile()

file = new File(Environment.getExternalStorageDirectory()+"/samp.pdf");
if (file.exists())
{
  Uri uri = Uri.fromFile(file);
  intent.setDataAndType(uri, "application/pdf");
  startActivity(intent);
}
else
{
Log.e("File not Exist","Check pdf File");
}
user370305
  • 108,599
  • 23
  • 164
  • 151
  • I made some change aand now its working... I removeintent.setDataAndType(Uri.parse("/sdcard/samp.pdf"), "application/pdf"); and add File file = new File("/sdcard/sample.pdf"); Uri uri = Uri.fromFile(file); intent.setDataAndType(uri, "application/pdf"); – sarath Feb 29 '12 at 09:31
  • Hi..now its working for sdcard but not for the file i put in assets folder.How to read the same file from assets folder.I gave File file = new File("/sdcard/sample.pdf"); Uri uri = Uri.fromFile(file); intent.setDataAndType(uri, "application/pdf"); but got file path not found – sarath Feb 29 '12 at 09:39
  • ya..i put my file in to assets folder and write File file = new File("android.resource://com.pdftest/assets/sample");..but its giving path not found... – sarath Feb 29 '12 at 09:49
  • Look at this question http://stackoverflow.com/questions/6491210/how-to-open-a-pdf-stored-either-in-res-raw-or-assets-folder – user370305 Feb 29 '12 at 09:59
  • hi..I read the answer but i didnt understand how to make a code...can u give me sample code...? – sarath Feb 29 '12 at 10:09
0

Have you displayed the correct path? might be the path issue.

user1225750
  • 3
  • 1
  • 1
  • 2
0

Just looking you have : sdcard/samp.pdf is the filename, you are saying you have the file name as: sample.pdf ?

user1225750
  • 3
  • 1
  • 1
  • 2