为android marshmallow 6.0编写PDF文件

时间:2017-01-31 22:52:46

标签: android pdf android-6.0-marshmallow

我的Android设备5.1.1版本的代码绝对运行良好,但是在Android 6.0的其他设备上却没有。

我正在使用itext。

List<Bill_reportmodel>   total_elect_read = null;

File pdfFolder = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DOCUMENTS), "pdfdemo");
if (!pdfFolder.exists()) {
    pdfFolder.mkdir();
    Log.i("path", "Pdf Directory created");
}
Date date1 = new Date();
int total_flat=0;
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(date1);
String curr_date=new SimpleDateFormat("MMddyyyy").format(date1);
File myFile = new File(pdfFolder + timeStamp + ".pdf");


OutputStream output = null;
try {
    output = new FileOutputStream(myFile);
} catch (FileNotFoundException e) {
    e.printStackTrace();
}
Document document = new Document();
try {
    PdfWriter.getInstance(document, output);
} catch (DocumentException e) {
    e.printStackTrace();
}
// for(int i=0;i<=0;i++) {
total_elect_read = dBhelper.get_Bill_report_Mounthly( 1, "flat", _ID_property, 1);

//  }

document.open();


PdfPTable table = new PdfPTable(new float[]{2, 1, 2, 1, 1, 1, 1});
table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell("DATE");
table.addCell("FLATE_ID");
table.addCell("BILLTYPE");
table.addCell("PROPERTY");
table.addCell("ELECTICITY_USED");
table.addCell("ELCT_CHARG");
table.addCell("PER_FLATE");


table.setHeaderRows(1);

PdfPCell[] cells = table.getRow(0).getCells();
for (int j = 0; j < cells.length; j++) {
    cells[j].setBackgroundColor(BaseColor.GRAY);
}

for (int i = 0; i < total_elect_read.size(); i++) {

    table.addCell(total_elect_read.get(i).getDate());
    table.addCell(String.valueOf(total_elect_read.get(i).getFlate_id()));
    table.addCell(String.valueOf(total_elect_read.get(i).getBill_type()));
    table.addCell(String.valueOf(total_elect_read.get(i).getProperty_id()));
    table.addCell(String.valueOf(total_elect_read.get(i).getElecticity_used()));
    table.addCell(String.valueOf(total_elect_read.get(i).getElecticity_used_charg()));
    table.addCell(String.valueOf(total_elect_read.get(i).getPer_flate()));
    //  total_flat +=  list_bill.get(i).getTotal_electicity_used_charge();
    bll_type =  total_elect_read.get(i).getBill_type();
    if(bll_type.equals("flat")){

        total_flat +=  total_elect_read.get(i).getElecticity_used();
        //    table.addCell(String.valueOf(list_bill.get(i).getPer_flate()));

    }

}
try {

    Paragraph p1 = new Paragraph("Total flate Meter reading = "+total_flat,subFont);
    //  p1.Font paraFont= new Font(Font.COURIER);
    p1.setAlignment(Paragraph.ALIGN_CENTER);
    // p1.setFont(paraFont);


    Paragraph subPara = new Paragraph("Month Meter Reading "+PROPERTY_NAME, subFont);
    subPara.setAlignment(Paragraph.ALIGN_CENTER);


    document.add(subPara);

    document.add(table);
    document.add(p1);
    /*document.add(p1);
    document.add(p2);*/
} catch (DocumentException e) {
    e.printStackTrace();
}
document.close();

System.out.println("Done");
}
});
}

build.gradle

mainifest.xml

这是我的build.gradle         apply plugin:'com.android.application'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.0"
    defaultConfig {
        applicationId "com.example.rehankhan.amannn"
        minSdkVersion 19
        targetSdkVersion 24
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:24.2.1'
    compile 'com.google.android.gms:play-services-drive:7.8.0'

    testCompile 'junit:junit:4.12'
    compile 'com.itextpdf:itextg:5.5.10'
}

这是我在6.0.1上遇到的错误

02-01 18:05:32.800 28361-28361/com.example.rehankhan.amannn E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.rehankhan.amannn, PID: 28361
java.lang.NullPointerException: Attempt to invoke virtual method 'void java.io.OutputStream.write(byte[], int, int)' on a null object reference
at java.io.BufferedOutputStream.flushInternal(BufferedOutputStream.java:185)
at java.io.BufferedOutputStream.flush(BufferedOutputStream.java:85)
at com.itextpdf.text.pdf.OutputStreamCounter.flush(OutputStreamCounter.java:89)
at com.itextpdf.text.DocWriter.close(DocWriter.java:233)
at com.itextpdf.text.pdf.PdfWriter.close(PdfWriter.java:1340)
at com.itextpdf.text.pdf.PdfDocument.close(PdfDocument.java:900)
at com.itextpdf.text.Document.close(Document.java:415)
at com.example.rajput_pc.aman.myhouse$6$1.onClick(myhouse.java:484)
at android.view.View.performClick(View.java:5264)
at android.view.View$PerformClick.run(View.java:21297)
at android.os.Handler.handleCallback(Handler.java:743)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:150)
at android.app.ActivityThread.main(ActivityThread.java:5546)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:794)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:684)

0 个答案:

没有答案