I need open a pdf in my android application. I only have a success
using the google docs
via the WebView of the Android SDK.

I readed a lot of examples using a secondary activities and Intents
but all when runs
throws a NullPointerException inside the SDK class.

My Manyfest is:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android";
      android:versionCode="1"
      android:versionName="1.0"
package="com.oesia.movilidad.android.appdocumental.activities">
    <uses-sdk android:minSdkVersion="8" />
        <uses-permission android:name="android.permission.INTERNET" />
    <application android:icon="@drawable/icon" android:label="@string/
app_name">
        <activity android:name=".PDFViewActivity"
                android:label="@string/app_name"
            android:configChanges="orientation|keyboardHidden">
            <intent-filter>
                <action android:name="android.intent.action.VIEW"/>
                <category
android:name="android.intent.category.DEFAULT"/>
                <category
android:name="android.intent.category.BROWSABLE"/>
                <data android:mimeType="application/pdf"/>
                <data android:mimeType="application/vnd.ms-excel"/>
                <data android:mimeType="application/msword"/>
                <data android:mimeType="application/vnd.ms-powerpoint"/
>
            </intent-filter>
                        <provider android:name=".FileContentProvider"
android:authorities="com.oesia.movilidad.android.utilities" />
        </activity>
        <activity android:name=".PrincipalActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category
android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>


And my java code for open de pdf:

package com.oesia.movilidad.android.appdocumental.activities;

import java.io.File;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.widget.Toast;

public class PDFViewActivity extends Activity  {
    public void abrirPDF() {
        /** First
        Intent intent = new Intent();
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setAction(android.content.Intent.ACTION_VIEW);
        String category = new
String("android.intent.category.DEFAULT");
        intent.setDataAndType(Uri.fromFile(new File("/mnt/sdcard/
Hibernate-1x2.pdf")), "application/pdf");
        startActivity(intent);
        */
        /** Second
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(Uri.fromFile(new File("/mnt/sdcard/
Hibernate-1x2.pdf")), "application/pdf");
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

        try {
            startActivity(intent);
        } catch (ActivityNotFoundException e) {
            Toast.makeText(this,
                "No Application Available to View PDF",
                Toast.LENGTH_SHORT).show();
        } catch (Exception e) {
            Toast.makeText(this,
                    "Exception" + e.toString(),
                    Toast.LENGTH_SHORT).show();
        }
        */
        /** Third
        File pdf = new File("assets/spring-in-action.pdf");
        Intent intent = new Intent(Intent.ACTION_VIEW,
Uri.parse("pdf:location://com.oesia.movilidad.android.utilities/" +
pdf));
        intent.setType("application/pdf");
        startActivity(intent);
        */
        openFile(new File("/mnt/sdcard/Hibernate-1x2.pdf"), "application/
pdf");
    }

    private void openFile(File file, String mimeType) {
        Intent viewIntent = new Intent();
        viewIntent.setAction(Intent.ACTION_VIEW);
        viewIntent.setDataAndType(Uri.fromFile(file), mimeType);
        // using the packagemanager to query is faster than trying
startActivity
        // and catching the activity not found exception, which causes
a stack unwind.
        List<ResolveInfo> resolved =
getPackageManager().queryIntentActivities(viewIntent, 0);
        if (resolved != null && resolved.size() > 0) {
            startActivity(viewIntent);
        } else {
            // notify the user they can't open it.
            Toast.makeText(this,
                    "No Application Available to View PDF",
                    Toast.LENGTH_SHORT).show();
        }
    }
}

Is possible open a pdf via a third application inside a android app?.

Thanks in advance,
José-Alberto

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to