【问题标题】:How to view local Pdf in PdfViewerActivity?如何在 PdfViewerActivity 中查看本地 Pdf?
【发布时间】:2012-10-08 11:38:14
【问题描述】:

我正在使用这个https://github.com/jblough/Android-Pdf-Viewer-Library 我在我的资产文件夹中放了 6 个 pdf,并尝试从我的 tabactivity 中加载它们, 但是它一直在“加载 PDF 页面”,而我确定 pdf 文件不是那么大:P

这是我正在使用的代码:

public class MainActivity extends TabActivity {

@Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    tabHost = getTabHost();

setupTabHost();

}
private void setupTabHost() {
addTab("page1", R.drawable.thumbnail1, PdfActivity.class, 1);
addTab("page2", R.drawable.thumbnail2, PdfActivity.class, 2);
addTab("page3", R.drawable.thumbnail3, PdfActivity.class, 3);
addTab("page4", R.drawable.thumbnail4, PdfActivity.class, 4);
addTab("page5", R.drawable.thumbnail5, PdfActivity.class, 5);
addTab("page6", R.drawable.thumbnail6, PdfActivity.class, 6);
    }

private void addTab(String tag, int drawableId, Class<?> c, int pagenumber) {
//create tab
TabHost.TabSpec spec = tabHost.newTabSpec(tag);

    //set layout
View thumbnail = LayoutInflater.from(this).inflate(R.layout.thumbnail, getTabWidget(), false);
ImageView icon = (ImageView) thumbnail.findViewById(R.id.thumbnail_icon);
icon.setImageResource(drawableId);
    spec.setIndicator(thumbnail);

//add intent
Intent intent = new Intent(this, c);
                       intent.putExtra(net.sf.andpdf.pdfviewer.PdfViewerActivity.EXTRA_PDFFILENAME, "file:///android_asset/page"+pagenumber+".pdf");
spec.setContent(intent);

//add tab
    tabHost.addTab(spec);
     }
}


public class PdfActivity extends PdfViewerActivity {


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}



public int getPreviousPageImageResource() { return R.drawable.left_arrow; }
public int getNextPageImageResource() { return R.drawable.right_arrow; }
public int getZoomInImageResource() { return R.drawable.zoom_in; }
public int getZoomOutImageResource() { return R.drawable.zoom_out; }
public int getPdfPasswordLayoutResource() { return R.layout.pdf_file_password; }
public int getPdfPageNumberResource() { return R.layout.dialog_pagenumber; }
public int getPdfPasswordEditField() { return R.id.etPassword; }
public int getPdfPasswordOkButton() { return R.id.btOK; }
public int getPdfPasswordExitButton() { return R.id.btExit; }
public int getPdfPageNumberEditField() { return R.id.pagenum_edit; }

}

我的日志中有这个,但我的资产文件夹中肯定有 page1.pdf!

10-09 16:55:00.223: I/PDFVIEWER(3650): onCreate
10-09 16:55:00.223: E/PDFVIEWER(3650): restoreInstance
10-09 16:55:00.308: D/dalvikvm(3650): GC_CONCURRENT freed 168K, 4% free 12979K/13383K, paused 10ms+2ms
10-09 16:55:00.373: I/PDFVIEWER(3650): Intent { cmp=xxxx (has extras) }
10-09 16:55:00.378: I/PDFVIEWER(3650): ST='file 'file:///android_asset/page1.pdf' not found'
10-09 16:55:00.383: I/PDFVIEWER(3650): ST='reading page 1, zoom:1.0'
10-09 16:55:00.403: D/dalvikvm(3650): GC_CONCURRENT freed 107K, 3% free 13358K/13703K, paused 2ms+14ms
10-09 16:55:00.438: D/dalvikvm(3650): GC_FOR_ALLOC freed 127K, 3% free 13626K/14023K, paused 13ms
10-09 16:55:00.573: D/dalvikvm(3650): GC_CONCURRENT freed 155K, 4% free 13963K/14407K, paused 2ms+24ms

【问题讨论】:

  • 对不起你是对的!更新了
  • file 'file:///android_asset/page1.pdf' not found 不大以至于它们不存在
  • 资产文件夹中仍然有一个名为 page1.pdf 的文件
  • 我刚刚在 pdfviewer 代码中看到它尝试创建一个 File 对象,然后检查文件是否有数据。当我找不到任何数据时,它会输出“找不到文件”消息
  • 一个快速的解决方案是复制 sdcard 上的文件并查看该文件(资产可能很棘手)

标签: android pdf


【解决方案1】:

您的文件路径有问题。从路径中删除 file://,如下所示:

File pdfFile = new File(Environment.getExternalStorageDirectory(),"bell.pdf");
String path = pdfFile.getAbsolutePath();
                openPdfIntent(path);

    protected void openPdfIntent(String path) {
    // TODO Auto-generated method stub
     try {
            final Intent intent = new Intent(MainActivity.this, Second.class);
            intent.putExtra(PdfViewerActivity.EXTRA_PDFFILENAME,path);
            startActivity(intent);
        } catch (Exception e) {
            e.printStackTrace();
        }
}

【讨论】:

    【解决方案2】:

    当我遇到类似问题时查看了源代码并亲自使用它后,我能够通过在调用活动时使用 VIEW 意图来使其工作。此外,我必须为该文件使用某个路径,而我只能从res\raw 文件夹中获取它。您也可以调整它以从 asset 文件夹中提取,但我从来没有走那么远。

    // This is wherever you are launching the activity from another activity
    Intent pdfIntent = new Intent(this, PdfActivity.class);
    pdfIntent.setAction(Intent.ACTION_VIEW);
    pdfIntent.setDataAndType(Uri.parse("android.resource://com.your.package/raw/filename"), "application/pdf");
    
    startActivity(pdfIntent);
    

    当您使用 VIEW 操作时,该类实际上会拉取文件并将其存储到一个临时目录中,以便它可以正确查看它。

    【讨论】:

      猜你喜欢
      • 2013-03-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-17
      • 2019-03-21
      • 1970-01-01
      • 2019-06-21
      相关资源
      最近更新 更多