【问题标题】:Display and appear corectly a pdf file in android在android中正确显示和显示pdf文件
【发布时间】:2013-03-10 18:14:50
【问题描述】:

我正在尝试在我的应用程序中打开一个 pdf 文件。我的问题是我无法阅读该文档,因为它出现了奇怪的字符。另外,如果我显示一个 txt 文件,我可以阅读它,但它的文本并不像我想要的那样对齐。所有的线都居中。我的问题是我做错了什么,我该如何纠正这些错误?

我的 xml

<!-- Demonstrates styled string resources.
     See corresponding Java code com.android.sdk.content.StyledText -->
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textStyle="normal" />

</LinearLayout>

</ScrollView>

和班级

public class PrimulAjutor2A extends MainActivity2A
{
    @Override
  protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

       // See assets/res/any/layout/styled_text.xml for this
       // view layout definition.
        setContentView(R.layout.primulajutor2);

        // Programmatically load text from an asset and place it into the
       // text view.  Note that the text we are loading is ASCII, so we
       // need to convert it to UTF-16.
       try {
           InputStream is = getAssets().open("abc.pdf");

            // We guarantee that the available method returns the total
            // size of the asset...  of course, this does mean that a single
           // asset can't be more than 2 gigs.
            int size = is.available();

            // Read the entire asset into a local byte buffer.
            byte[] buffer = new byte[size];
           is.read(buffer);
            is.close();

           // Convert the buffer into a string.
           String text = new String(buffer);

            // Finally stick the string into the text view.
           TextView tv = (TextView)findViewById(R.id.text);
            tv.setText(text);
        } catch (IOException e) {
            // Should never happen!
            throw new RuntimeException(e);
        }
    }}

【问题讨论】:

  • 在 webview 中使用 google docs 打开您的 pdf。
  • PDF是二进制格式,不能转换成字符串

标签: android file pdf


【解决方案1】:

我建议您使用 google docs 通过 webview 显示您的 pdf。

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>

<WebView
    android:id="@+id/wv"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"/> 


</LinearLayout>

在您的活动类中使用以下代码。

       WebView wv= (WebView) findViewById(R.id.wv);
     wv.setVerticalScrollBarEnabled(true);
     wv.setHorizontalScrollBarEnabled(true);
     wv.getSettings().setBuiltInZoomControls(true);
     wv.getSettings().setJavaScriptEnabled(true);
     wv.setWebViewClient(new WebViewClient());
     wv.loadUrl("https://docs.google.com/file/d/0B8ZDVNCvRWK8c3YzQVFaWjg1bEU/edit");

要求用户登录谷歌帐户并需要文档所有者的许可。既然你给了我访问权限,我就可以在我的手机中查看相同的内容。在三星 Galaxy S3 上测试。 WebView 可以滚动和缩放。

如果您的设备上安装了 pdf 应用程序,您可以使用 Intent 提供必要的参数来打开您的 pdf 文件。

private void openFile(File f, String mimeType)
{
Intent viewIntent = new Intent();
viewIntent.setAction(Intent.ACTION_VIEW);
viewIntent.setDataAndType(Uri.fromFile(file), mimeType);
// using the packagemanager to query is faster than trying startActivity
// and catching the activity not found exception, which causes a stack unwind.
List<ResolveInfo> resolved = getPackageManager().queryIntentActivities(viewIntent, 0);
if(resolved != null && resolved.size() > 0)
{
    startActivity(viewIntent);
}
else
{
    // notify the user they can't open it.
}
}

http://ecestage.googlecode.com/svn-history/r12/branches/src/com/pdfviewer/PdfViewerActivity.java。我没有尝试链接中的代码。你可以试试这个。

【讨论】:

  • 他不能,因为他没有足够的声望。
  • 我输入了谷歌文档,但现在它是谷歌驱动器。我在那里上传了pdf,但网址是这样的:docs.google.com/file/d/0B8ZDVNCvRWK8c3YzQVFaWjg1bEU/edit。链接中没有 abc.pdf。我该怎么办?我是个女孩:D
  • 你可以用和上面完全相同的方式打开google。在网络视图中。如果您上传,您可以获得该链接。只需执行 webview.loadUrl("Google drive with pdf link");
  • 我已经尝试了几个小时来打开一个 pdf 文件,但我用 brio 失败了。好消息是我在一个网站上找到了正是我想要的代码。但是,我有一个问题:教程说我需要将 pdf 文件放在模拟器的 sdcard 中。当我完成应用程序时,我将制作一个 apk 并将其放在 google play 上。现在,如果我将 pdf 放在 sdcard 上,那么在我制作 sdk 后还会在那里吗?如果有人好奇的话,链接是edumobile.org/android/android-development/pdf-list-example
  • 它需要您的手机有pdf阅读器应用程序。和上面的代码一样。如果您有 hte pdf 阅读器,请将 Uri.fromFile(file) 替换为您的 pdf 文件的路径,它应该可以工作。
猜你喜欢
  • 1970-01-01
  • 2014-05-13
  • 1970-01-01
  • 2015-11-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-26
  • 1970-01-01
相关资源
最近更新 更多