抱歉我的英文。我使用android pdf writer library(请不要向我提供iText - 因为这样的库对我来说没有很好的许可证)。它的libruary工作,但当我想创建例如每页4页有1个图像时,它说我内存不足。我不知道为什么我有这个错误,我花了很多时间来修复这个错误,但这没有用。
我的代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String pdfcontent = generateHelloWorldPDF();
outputToScreen(R.id.text, pdfcontent);
outputToFile("helloworld.pdf", pdfcontent, "ISO-8859-1");
}
//generation pdf
private String generateHelloWorldPDF(){
String s = null;
try{
//this list have image(size, name)
ArrayList<FileObject> listFile = ....
AssetManager mngr = getAssets();
PDFWriter mPDFWriter = new PDFWriter(500, 600);
for(int i = 0; i < listFile.size(); i++) {
if(i != 0) mPDFWriter.newPage();
Bitmap image = BitmapFactory.decodeFile(String.valueOf(new File(Environment.getExternalStorageDirectory() + "/test/" + listFile.get(i).getName())));
mPDFWriter.addImage(0, 0, image, Transformation.DEGREES_0_ROTATION);
s = mPDFWriter.asString();
}
s = mPDFWriter.asString();
}catch (Exception e) {
Log.e("error", e.toString());
}
return s;
}
//create file
private void outputToFile(String fileName, String pdfContent, String encoding) {
File newFile = new File(Environment.getExternalStorageDirectory() + "/" + fileName);
try {
newFile.createNewFile();
try {
FileOutputStream pdfFile = new FileOutputStream(newFile);
pdfFile.write(pdfContent.getBytes(encoding));
pdfFile.close();
} catch(FileNotFoundException e) {
//
}
} catch(IOException e) {
//
}
}
我有
E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.OutOfMemoryError
at java.io.ByteArrayOutputStream.toByteArray(ByteArrayOutputStream.java:122)
at com.example.alexy.converttopdf.XObjectImage.encodeImageData(XObjectImage.java:114)
at com.example.alexy.converttopdf.XObjectImage.processImage(XObjectImage.java:132)
at com.example.alexy.converttopdf.XObjectImage.<init>(XObjectImage.java:43)
at com.example.alexy.converttopdf.PDFWriter.addImage(PDFWriter.java:96)
at com.example.alexy.converttopdf.MainActivity.generateHelloWorldPDF(MainActivity.java:90)
at com.example.alexy.converttopdf.MainActivity.onCreate(MainActivity.java:59)
at android.app.Activity.performCreate(Activity.java:5008)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
at android.app.ActivityThread.access$600(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
MainActivity59 String pdfcontent = generateHelloWorldPDF();
MaintActivity90 mPDFWriter.addImage(0, 0, image, Transformation.DEGREES_0_ROTATION);
例如,如果我做了for(int i = 0; i < 2; i++)
它的工作。如果我喜欢这个for(int i = 0; i < 3; i++)
- &gt; 内存不足
答案 0 :(得分:2)
Android PDF Writer (APW)库只需要很多内存,因为它
Strings
或`StringBuilder&#39;的内存中。它有效地使内存使用量加倍,并强制执行对它们进行更多的二进制文件的ASCII85编码,byte[]
StringBuilder
,此时再次有效地将所需内存加倍,因此,在使用此库时,请始终牢记这种资源,并避免添加多个大型实体(例如图像)。
BTW,APW在创建内部PDF交叉引用时有一个错误,参见后this answer中的后记。