我對android更新穎。我的任務是在android設備上打開一個pdf文件。我搜索了一個教程並試了一下。但我無法打開PDF文件。我不知道我在哪裏做的錯誤?請幫助我的人。如何在android設備上打開PDF文件?
activity_main.xml中
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="106dp"
android:text="open" />
</RelativeLayout>
MainActivity.java
package com.example.pdftest;
import java.io.File;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener{
private Button b1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1=(Button)findViewById(R.id.button1);
b1.setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
File pdffile=new File(Environment.getExternalStorageDirectory(),"/sdcard/abc.pdf");
try
{
if(pdffile.exists())
{
Uri path=Uri.fromFile(pdffile);
Intent objintent=new Intent(Intent.ACTION_VIEW);
objintent.setDataAndType(path, "application/pdf");
objintent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(objintent);
}
else
{
Toast.makeText(MainActivity.this,"File Not Found",Toast.LENGTH_SHORT).show();
}
}
catch(ActivityNotFoundException e)
{
Toast.makeText(MainActivity.this,"No viewer application Found",Toast.LENGTH_SHORT).show();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
的AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.pdftest"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.pdftest.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
的[打開PDF文件編程(可能的複製http://stackoverflow.com/questions/11398239/open-a-pdf-file-programatically ) – 0X0nosugar
你可以發佈你的錯誤? –