本文介绍了如何在原始Android中的PDF文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何创建一个PDF FIEL原..Eventhough我使用需要iText.jar我是歌厅的错误中指定文件未找到错误,并说文件无法打开。这里是code
/ **第一次创建活动时调用。 * /
的EditText EDITTEXT;
Button按钮;
私有静态字符串文件=C:/FirstPdf.pdf;
字符串的身体;
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main);
EDITTEXT =(EditText上)findViewById(R.id.editText1);
按钮=(按钮)findViewById(R.id.button1);
button.setOnClickListener(新View.OnClickListener(){
@覆盖
公共无效的onClick(视图v){
// TODO自动生成方法存根
。身体= editText.getText()的toString();
文献文件=新的文件();
尝试 {
PdfWriter.getInstance(文件,新的FileOutputStream(文件));
document.open();
document.addSubject(体);
document.close();
}赶上(例外五){
// TODO自动生成的catch块
e.printStackTrace();
}
}
});
}
解决方案
的EditText EDITTEXT;
Button按钮;
私有静态字符串FILE =;
字符串的身体;
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main);
FILE =(Environment.getExternalStorageDirectory()getAbsolutePath());
EDITTEXT =(EditText上)findViewById(R.id.editText1);
按钮=(按钮)findViewById(R.id.button1);
button.setOnClickListener(新View.OnClickListener(){
@覆盖
公共无效的onClick(视图v){
// TODO自动生成方法存根
。身体= editText.getText()的toString();
文献文件=新的文件();
尝试 {
文件临时=新的文件(FILE.getAbsolutePath(),abcd.pdf);
PdfWriter.getInstance(文件,新的FileOutputStream(临时));
document.open();
document.addSubject(体);
document.close();
}赶上(例外五){
// TODO自动生成的catch块
e.printStackTrace();
}
}
});
}
没有C:在Android中使用Environment.getExternalStorageDirectory()getAbsolutePath()来获得外部存储路径
在清单中添加权限写入外部存储
How to Create A PDF fiel in raw ..Eventhough I Was using iText.jar I was geting an error specifying File Not Found Error and also saying File cannot be Opened .Here is the code
/** Called when the activity is first created. */
EditText editText;
Button button;
private static String FILE = "C:/FirstPdf.pdf";
String body ;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
editText = (EditText)findViewById(R.id.editText1);
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
body = editText.getText().toString();
Document document = new Document();
try {
PdfWriter.getInstance(document, new FileOutputStream(FILE));
document.open();
document.addSubject(body);
document.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
解决方案
EditText editText;
Button button;
private static String FILE = "";
String body ;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
FILE=(Environment.getExternalStorageDirectory().getAbsolutePath());
editText = (EditText)findViewById(R.id.editText1);
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
body = editText.getText().toString();
Document document = new Document();
try {
File temp = new File(FILE.getAbsolutePath(),"abcd.pdf");
PdfWriter.getInstance(document, new FileOutputStream(temp));
document.open();
document.addSubject(body);
document.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
there is no C: in android use Environment.getExternalStorageDirectory().getAbsolutePath() to get external storage path
in manifest add permission to write external storage
这篇关于如何在原始Android中的PDF文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!