2012-07-09 146 views
1

我正在致力于pdf.I尝试使用下面的代码从我的应用程序中打开一个pdf文件,但我未能打开。以编程方式打开pdf文件

private void openPdf() { 

     File file = new File("mnt/sdcard.test.pdf"); 
     Uri path = Uri.fromFile(file); 
     Intent intent = new Intent(Intent.ACTION_VIEW); 
     intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
     intent.setAction(Intent.ACTION_VIEW); 
     intent.setData(path); 
     intent.setType("application/pdf"); 
     try { 
      startActivity(intent); 
     } catch (ActivityNotFoundException e) { 
      Toast.makeText(getActivity(), "No application found", 
        Toast.LENGTH_SHORT).show(); 
     } 

    } 

当我试图在模拟器此代码,它显示了一个烤面包说“不应用程序中发现”(bcoz,通常没有PDF查看应用程序被安装在仿真器)。当i的装置测试的相同的事情(特别是在funbook选项卡和索尼选项卡),它既没有显示Toast消息也没有打开PDF文件。任何人都可以指出我的代码中的错误。其实我第一次使用pdf。所以我的问题是,

  1. 在设备中没有显示吐司消息,这意味着我的手机中安装了一个 pdf查看应用程序?是不是?
  2. 如果是这样,为什么不使用第三方应用程序打开pdf。
  3. 如果我想列出安装在我的 手机中的所有pdf查看应用程序给用户,我应该在这段代码中做些什么改变?

    在此先感谢....

回答

3

尝试LuxuryMode的方法:https://stackoverflow.com/a/8221594/1500067

我认为你只是缺少土坯包intent.setPackage( “com.adobe.reader”);

+0

我避免了一块代码intentionly,bcoz我使用所有的applcation要打开PDF文件,而不仅仅是使用Adobe Reader仅 – 2012-07-09 15:46:33

+0

什么叫所有的应用程序是什么意思?您是否希望在您点击PDF文件时专门打开您的应用程序? – firefistace 2012-07-09 15:47:47

+0

找到任何可用于显示PDF的应用程序 – 2012-07-09 16:37:21

0

我有几乎相同的代码,工作正常,但我不打开从我的应用程序从SD卡中的文件。

Activity mActivity = /* your activity */...; 
String mFileName = /* path of my PDF file */...; 

Uri uri = Uri.fromFile(mActivity.getFileStreamPath(mFileName)); 

try 
{ 
    Intent intentUrl = new Intent(Intent.ACTION_VIEW); 
    intentUrl.setDataAndType(uri, "application/pdf"); 
    intentUrl.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
    mActivity.startActivity(intentUrl); 
} 
catch (ActivityNotFoundException e) 
{ 
    Toast.makeText(mActivity, "No PDF Viewer Installed", Toast.LENGTH_LONG).show(); 
} 

所以你的方法是正确的。确保你可以先打开文件...即使用mActivity.openFileInput()来确保你有一个可读的PDF。

1

我解决了上面的问题,所以试试一次;

步骤: -

  1. 下您的应用程序的名称创建SRC资产的文件夹。

  2. 在这个assets文件夹中保留你的pdf文件例如schedule1.pdf。

  3. 现在出现在你的活动上的任何UI组件即MainActivity.java

  4. setListener你想要的东西即(ButtonImageViewImageButton);

  5. 在这个监听器调用一个用户定义的方法,即openPDFFiles()

的openPDFFiles()方法有下面的代码:

private void openPDFFiles() 

{ 
AssetManager assetManager = getAssets(); 

InputStream in = null; 
OutputStream out = null; 
File file = new File(getFilesDir(), “schedule1.pdf”);//here schedule1.pdf is the pdf file name which is keep in assets folder. 
try { 
in = assetManager.open(“schedule1.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() + “/schedule1.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); 
} 
} 
0

这里下载(Display PDF file inside my android application

源代码添加在您的摇篮文件这种依赖性:

compile 'com.github.barteksc:android-pdf-viewer:2.0.3' 

activity_main.xml中

<RelativeLayout android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#ffffff" 
    xmlns:android="http://schemas.android.com/apk/res/android" > 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="40dp" 
     android:background="@color/colorPrimaryDark" 
     android:text="View PDF" 
     android:textColor="#ffffff" 
     android:id="@+id/tv_header" 
     android:textSize="18dp" 
     android:gravity="center"></TextView> 

    <com.github.barteksc.pdfviewer.PDFView 
     android:id="@+id/pdfView" 
     android:layout_below="@+id/tv_header" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"/> 


    </RelativeLayout> 

MainActivity.java

import android.app.Activity; 
import android.database.Cursor; 
import android.net.Uri; 
import android.provider.OpenableColumns; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.widget.ImageView; 
import android.widget.RelativeLayout; 

import com.github.barteksc.pdfviewer.PDFView; 
import com.github.barteksc.pdfviewer.listener.OnLoadCompleteListener; 
import com.github.barteksc.pdfviewer.listener.OnPageChangeListener; 
import com.github.barteksc.pdfviewer.scroll.DefaultScrollHandle; 
import com.shockwave.pdfium.PdfDocument; 

import java.util.List; 

public class MainActivity extends Activity implements OnPageChangeListener,OnLoadCompleteListener{ 
    private static final String TAG = MainActivity.class.getSimpleName(); 
    public static final String SAMPLE_FILE = "android_tutorial.pdf"; 
    PDFView pdfView; 
    Integer pageNumber = 0; 
    String pdfFileName; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 


     pdfView= (PDFView)findViewById(R.id.pdfView); 
     displayFromAsset(SAMPLE_FILE); 
    } 

    private void displayFromAsset(String assetFileName) { 
     pdfFileName = assetFileName; 

     pdfView.fromAsset(SAMPLE_FILE) 
       .defaultPage(pageNumber) 
       .enableSwipe(true) 

       .swipeHorizontal(false) 
       .onPageChange(this) 
       .enableAnnotationRendering(true) 
       .onLoad(this) 
       .scrollHandle(new DefaultScrollHandle(this)) 
       .load(); 
    } 


    @Override 
    public void onPageChanged(int page, int pageCount) { 
     pageNumber = page; 
     setTitle(String.format("%s %s/%s", pdfFileName, page + 1, pageCount)); 
    } 


    @Override 
    public void loadComplete(int nbPages) { 
     PdfDocument.Meta meta = pdfView.getDocumentMeta(); 
     printBookmarksTree(pdfView.getTableOfContents(), "-"); 

    } 

    public void printBookmarksTree(List<PdfDocument.Bookmark> tree, String sep) { 
     for (PdfDocument.Bookmark b : tree) { 

      Log.e(TAG, String.format("%s %s, p %d", sep, b.getTitle(), b.getPageIdx())); 

      if (b.hasChildren()) { 
       printBookmarksTree(b.getChildren(), sep + "-"); 
      } 
     } 
    } 

}