2013-07-03 28 views
22

Tôi đang phát triển một ứng dụng Android và tôi phải mở một số tệp.Mở tệp pdf

này được mã của tôi sử dụng ý:

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); 
    } 
} 

Tệp nằm trong thư mục gốc của thẻ SD và tôi có thể tự mở nó.

Vấn đề

ứng dụng được đóng lại khi nó đến lúc startActivity (mục đích). Tôi nghĩ rằng vấn đề là trong tập tin AndroidManifest.xml, nhưng tôi không biết làm thế nào để đặt nó một cách chính xác.

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) 

bạn có thể giúp tôi để hoàn thành AndroidManifest? Hoặc làm thế nào tôi có thể mở pdf?

+0

có vẻ như Android của bạn không có ứng dụng nào đọc pdf được cài đặt, thêm một –

+0

tôi có thể mở các file khác pdf với ThinkFree PDF Viewer. – Lyd

+0

Bài đăng trên blog này giúp bạn nếu bạn đang cố gắng sử dụng 'Trình cung cấp nội dung' (hiện được khuyến nghị): http://www.blogc.at/2014/03/23/share-private-files-with-other-apps- fileprovider/ – Sakiboy

Trả lời

69

Vấn đề là không có ứng dụng nào được cài đặt để xử lý việc mở tệp PDF. Bạn nên sử dụng Chooser Ý định, như vậy:

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

Tôi đã thử nó trong điện thoại di động của mình. Khi tôi mở một số pdf của một ứng dụng khác, nó nói để đăng ký ThinkFree, tôi nói "Sau" và "ThinkFree PDF Viewer Mobile cho Android" được mở, hiển thị cho tôi tập tin pdf. Với mã của bạn, nó chỉ đến để yêu cầu đăng ký ThinkFree. Nếu tôi nói "Sau", nó sẽ đóng lại. – Lyd

+1

Điều đó không liên quan gì đến mã này. Bạn không thể kiểm soát cách ThinkFree xử lý ý định này. Thử cài đặt trình xem PDF thay thế. –

+0

Cảm ơn, nó là ok. Một điều nữa ... Khi tôi đóng tệp pdf (với nút quay lại của điện thoại), ứng dụng của tôi sẽ bị lỗi. Làm thế nào tôi có thể giải quyết điều đó? – 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

Sử dụng mã trên, khi tệp của bạn được lưu trữ trong bộ nhớ trong. –