2013-07-03 164 views
22

我正在开发一个Android应用程序,我必须打开一些文件。Android打开pdf文件

这是用我的代码的意图:

public class FacturaActivity extends Activity { 

    (...) 

    public void downloadInvoice(View view) { 
     File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() +"/"+ filename); 
     Intent intent = new Intent(Intent.ACTION_VIEW); 
     intent.setDataAndType(Uri.fromFile(file),"application/pdf"); 
     intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY); 
     startActivity(intent); 
    } 
} 

文件是在SD卡的根目录下,我可以手动打开它。当它到达startActivity(意图)

问题

应用被关闭。我认为问题出现在AndroidManifest.xml文件中,但我不知道如何正确输入。

AndroidManifest.xml中

<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 

<uses-sdk 
    android:minSdkVersion="8" 
    android:targetSdkVersion="8" /> 

<application 
    android:allowBackup="true" 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" 
    android:name="###.MyApplication" > <!--cant show complete name--> 
    <activity 
     android:name="###.MainActivity" 
     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=".FacturaActivity" > 
    </activity> 

</application> 

logcat的

07-03 15:49:13.094: E/AndroidRuntime(1032): FATAL EXCEPTION: main 
07-03 15:49:13.094: E/AndroidRuntime(1032): java.lang.IllegalStateException: Could not execute method of the activity 
(...) 
07-03 15:49:13.094: E/AndroidRuntime(1032): Caused by: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=file:///mnt/sdcard/201209_F2012212782.PDF typ=application/pdf flg=0x40000000 } 
07-03 15:49:13.094: E/AndroidRuntime(1032):  at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1408) 
07-03 15:49:13.094: E/AndroidRuntime(1032):  at android.app.Instrumentation.execStartActivity(Instrumentation.java:1378) 
07-03 15:49:13.094: E/AndroidRuntime(1032):  at android.app.Activity.startActivityForResult(Activity.java:2817) 
07-03 15:49:13.094: E/AndroidRuntime(1032):  at android.app.Activity.startActivity(Activity.java:2923) 

你能帮我完成AndroidManifest?或者我该如何打开该pdf?

+0

看来你的Android有没有安装任何PDF阅读器应用程序,添加一个 – 2013-07-03 16:20:29

+0

我可以打开ThinkFree的PDF查看器等PDF格式的文件。 – Lyd

+0

如果您尝试使用“ContentProvider”(现在推荐),此博客文章有助于您:http://www.blogc.at/2014/03/23/share-private-files-with-other-apps- fileprovider/ – Sakiboy

回答

69

问题是,没有安装应用程序来处理打开PDF。您应该使用意向选择器,像这样:

File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() +"/"+ filename); 
Intent target = new Intent(Intent.ACTION_VIEW); 
target.setDataAndType(Uri.fromFile(file),"application/pdf"); 
target.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY); 

Intent intent = Intent.createChooser(target, "Open File"); 
try { 
    startActivity(intent); 
} catch (ActivityNotFoundException e) { 
    // Instruct the user to install a PDF reader here, or something 
} 
+0

我已经在我的手机上试过了。当我打开另一个应用程序的PDF文件时,它说要注册ThinkFree,我说“稍后”和“适用于Android的ThinkFree PDF Viewer Mobile”已打开,显示我的PDF文件。使用您的代码,它只会要求ThinkFree注册。如果我说“稍后”,它会关闭。 – Lyd

+1

这与这段代码无关。您无法控制ThinkFree处理此意图的方式。尝试安装替代PDF查看器。 –

+0

谢谢,没关系。还有一件事...当我关闭PDF文件(用手机的后退按钮)我的应用程序关闭时出现错误。我该如何解决这个问题? – Lyd

5
String dir="/Attendancesystem"; 

public void displaypdf() { 

     File file = null; 
      file = new File(Environment.getExternalStorageDirectory()+dir+ "/sample.pdf"); 
     Toast.makeText(getApplicationContext(), file.toString() , Toast.LENGTH_LONG).show(); 
     if(file.exists()) { 
      Intent target = new Intent(Intent.ACTION_VIEW); 
      target.setDataAndType(Uri.fromFile(file), "application/pdf"); 
      target.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY); 

      Intent intent = Intent.createChooser(target, "Open File"); 
      try { 
       startActivity(intent); 
      } catch (ActivityNotFoundException e) { 
       // Instruct the user to install a PDF reader here, or something 
      } 
     } 
     else 
      Toast.makeText(getApplicationContext(), "File path is incorrect." , Toast.LENGTH_LONG).show(); 
    } 
+0

当您的文件存储在内部存储器中时,请使用上述代码。 –