此代码来自另一篇文章:Android open pdf file
这是我在1个类中的所有代码,名为openFile.class
package com.test.android;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.os.Environment;
import java.io.File;
public class openFile {
String namafile ;
public openFile(String namafile){
this.namafile = namafile;
}
File file = new
File(Environment.getExternalStorageDirectory().getAbsolutePath() +"/"+
namafile);
Intent target = new Intent(Intent.ACTION_VIEW);
target.setDataAndType(Uri.fromFile(file),"application/pdf");
target.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
Intent intent = Intent.createChooser(target, "Open File");
try {
startActivity(intent);
} catch (ActivityNotFoundException e) {
// Instruct the user to install a PDF reader here, or something
}
}
任何建议都将被接受