从资产打开pdf文件

时间:2014-07-10 16:19:39

标签: android pdf manifest

早上好 我使用代码打开资产文件夹中存在的pdf文件,但每次使用此消息时应用程序都会崩溃 无法启动活动Pdfreader ActivityNoFoundExeption发现NoActivity处理意图 这个代码

package com.example.albir;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.AssetManager;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;

public class Pdfreader extends Activity {
     @Override
     protected void onCreate(Bundle savedInstanceState)
     {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.pdf);
         CopyReadAssets();

     }

     @SuppressLint("WorldReadableFiles")
    @SuppressWarnings("deprecation")
    private void CopyReadAssets()
     {
         AssetManager assetManager = getAssets();

         InputStream in = null;
         OutputStream out = null;
         File file = new File(getFilesDir(), "beralahsa001.pdf");
         try
         {
             in = assetManager.open("beralahsa001.pdf");
             out = openFileOutput(file.getName(), Context.MODE_WORLD_READABLE);

             copyFile(in, out);
             in.close();
             in = null;
             out.flush();
             out.close();
             out = null;
         } catch (Exception e)
         {
             Log.e("tag", e.getMessage());
         }

         Intent intent = new Intent(Intent.ACTION_VIEW);
         intent.setDataAndType(
                 Uri.parse("file://" + getFilesDir() + "/beralahsa001.pdf"),
                 "application/pdf");

         startActivity(intent);
     }

     private void copyFile(InputStream in, OutputStream out) throws IOException
     {
         byte[] buffer = new byte[1024];
         int read;
         while ((read = in.read(buffer)) != -1)
         {
             out.write(buffer, 0, read);
         }
     }

}

这是清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.albir"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />
     <uses-permission android:name="android.permission.INTERNET"/>  
     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.albir.Menu"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".ShowDetails"></activity>
        <activity android:name=".MainActivity"></activity>
        <activity android:name=".Pdfreader"></activity>

    </application>

</manifest>
你可以帮我吗

2 个答案:

答案 0 :(得分:1)

似乎意图解析器无法找到任何可以处理PDF的内容。尝试在您的设备上安装PDF查看器。

答案 1 :(得分:1)

试试..

AssetManager assetManager = getAssets();


InputStream in = null;
OutputStream out = null;
File file = new File(getFilesDir(), "pdfname.pdf");
try
{
in = assetManager.open("pdfname.pdf");
out = openFileOutput(file.getName(), Context.MODE_WORLD_READABLE);


copyFile(in, out);
in.close();
in = null;
out.flush();
out.close();
out = null;
} catch (Exception e)
{
Toast.makeText(getApplicationContext(), "no pdf viewer", 2000).show();
finish();
}
try{
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(
Uri.parse("file://" + getFilesDir() + "/pdfname.pdf"),
"application/pdf");


startActivity(intent);
finish();
}
catch (Exception e)
{
Toast.makeText(getApplicationContext(), "no pdf viewer", 2000).show();
finish();
}
}


private void copyFile(InputStream in, OutputStream out) throws IOException
{
byte[] buffer = new byte[1024];
int read;
while ((read = in.read(buffer)) != -1)
{
out.write(buffer, 0, read);
}
}