2012-10-08 18 views
2

Im使用這個https://github.com/jblough/Android-Pdf-Viewer-Library 我已經把6 pdfs放在我的資產文件夾中,並嘗試從我的tabactivity, 加載它們,但它保持「加載PDF頁」,而我確信PDF文件並不那麼大:P如何查看PdfViewerActivity中的本地PDF?

這是我使用的代碼:

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 
+0

任何日誌? – njzk2

+0

對不起,你是對的!更新它 –

+1

'文件「文件:///android_asset/page1.pdf」不found'所以不是大不大,說他們不存在 – njzk2

回答

-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動作,即類實際上拉文件,並將其存儲到一個臨時目錄,以便它可以正常查看。

0

沒有您的文件路徑的問題。從你的路徑中刪除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(); 
     } 
}