显示并显示在Android corectly PDF文件文件、Android、corectly、PDF

2023-09-07 04:05:27 作者:猛虎嗅蔷薇

我想在我的应用程序中打开PDF文件。我的问题是,由于它出现怪异字符我看不懂的文件。另外,如果我展示一个txt文件,我可以读取它,但文本它不是allign因为我想。所有的线都centerd。我的问题是什么我做错了,我怎么能纠正这些错误呢?

我的XML

 <! - 展示风格的字符串资源。     请参见相应的Java code com.android.sdk.content.StyledText  - ><滚动型的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android    机器人:layout_width =WRAP_CONTENT    机器人:layout_height =WRAP_CONTENT>< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android    机器人:layout_width =match_parent机器人:layout_height =match_parent    机器人:方向=垂直>    <的TextView        机器人:ID =@ + ID /文        机器人:layout_width =match_parent        机器人:layout_height =WRAP_CONTENT        机器人:TEXTSTYLE =正常/>< / LinearLayout中>< /滚动型> 

和类

 公共类PrimulAjutor2A扩展MainActivity2A{    @覆盖  保护无效的onCreate(捆绑savedInstanceState)    {        super.onCreate(savedInstanceState);       //见资产/ RES /任何/布局/ styled_text.xml此       //视图布局定义。        的setContentView(R.layout.primulajutor2);        //编程方式从资产加载文本,并将其放入       //文本视图。需要注意的是,我们正在加载的文字是ASCII,所以我们       //需要将其转换为UTF-16。       尝试{           InputStream为= getAssets()开(abc.pdf);            //我们保证可用的方法返回的总            //资产的规模......当然,这是否意味着一个           //资产不能超过2演出。            INT大小= is.​​available();            //读取整个资产到本地字节的缓冲区。            字节[]缓冲区=新的字节[大小]           is.read(缓冲液);            is.close();           //将缓冲器中的字符串。           字符串文本=新的String(缓冲);            //最后坚持串入文本视图。           TextView的电视=(的TextView)findViewById(R.id.text);            tv.setText(文本);        }赶上(IOException异常五){            //应该永远不会发生!            抛出新的RuntimeException(E);        }    }} 

解决方案

我建议你使用谷歌文档使用web视图来显示您的PDF。

 < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android的xmlns:工具=htt​​p://schemas.android.com/tool​​s机器人:layout_width =FILL_PARENT机器人:layout_height =FILL_PARENT><的WebView    机器人:ID =@ + ID / WV    机器人:layout_width =FILL_PARENT    机器人:layout_height =FILL_PARENT/>< / LinearLayout中> 
如何把pdf分解成多个pdf 这个拆分方法简单又快捷

在你的活动类中使用以下code。

 的WebView WV =(的WebView)findViewById(R.id.wv);     wv.setVerticalScrollBarEnabled(真);     wv.setHorizo​​ntalScrollBarEnabled(真);     。wv.getSettings()setBuiltInZoomControls(真);     wv.getSettings()setJavaScriptEnabled(真)。     wv.setWebViewClient(新WebViewClient());     wv.loadUrl(https://docs.google.com/file/d/0B8ZDVNCvRWK8c3YzQVFaWjg1bEU/edit); 

需要用户登录到Google帐户,并要求从文档的所有者许可。既然你给我的访问,我能够在我的手机查看相同。测试在三星galaxy S3。的WebView可以滚动和缩放。

如果您已经安装在设备上PDF应用程序,你可以使用意图提供必要的参数,打开PDF文件。

 私人无效中openFile(文件f,串mime类型){意图viewIntent =新的Intent();viewIntent.setAction(Intent.ACTION_VIEW);viewIntent.setDataAndType(Uri.fromFile(文件),mime类型);//使用软件包管理系统来查询是不是试图startActivity快//和追赶活动未发现异常,从而导致堆栈回卷。清单< ResolveInfo>解决= getPackageManager()queryIntentActivities(viewIntent,0)。如果(解决=空&放大器;!&放大器; resolved.size()大于0){    startActivity(viewIntent);}其他{    //通知他们不能打开它的用户。}} 

http://ecestage.google$c$c.com/svn-history/r12/branches/src/com/pdfviewer/PdfViewerActivity.java.我还没有尝试在链接的code。你可以试试这个。

I am trying to open a pdf file in my app. My problem is that i cannot read the document because it appears with weird characters. Also, if i display a txt file, i can read it but the text it's not allign as i want to. All the lines are centerd. My question is what am I doing wrong and how can I correct these mistakes?

My 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>

and the class

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

解决方案

I suggest you to use google docs to display your pdf using a webview.

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

In your activity class use the following code.

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

Requires user to login to google account and requires permission from the owner of the document. Since you gave me the access i am able to view the same in my phone. Tested on Samsung galaxy S3. WebView can be scrolled and zoomed.

If you have a pdf application installed on your device , you can use intent to provide necessary parameters to open your pdf file.

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. I haven't tried the code in the link. You can give this a try.