Я пытаюсь создать PDF-файл макета, который находится на экране, но по какой-то причине он не будет создавать файл. Предполагается сделать копию экрана / макета и поместить ее в файл PDF, который затем сохраняется во внутреннем хранилище.
этот код был создан 2 года go, и я поднял его, и теперь он не работает правильно. я не уверен, что не так.
, пожалуйста, помогите.
'' '
/** PDF Gen should run in own thread to not slow the GUI */
public void printPDFshort2(){
if(isExternalStorageWritable()) {
String filename = getFileNamelong();
File file = new File(getAlbumStorageDir("Inspections"), filename);
try {
FileOutputStream outputStream = new FileOutputStream(file);
runshort2(outputStream);
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
}
}
sendfirstfile();
}
private File getAlbumStorageDir(String albumName) {
// Get the directory for the user's public pictures directory.
File file = new File(Environment.getExternalStorageDirectory() +"/"+ albumName);
if (!file.mkdirs()) {
file.mkdirs();
Log.e(LOG_TAG, "Directory not created");
}
return file;
}
private boolean isExternalStorageWritable() {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
return true;
}
return false;
}
/**
* Returns a name for the file that will be created
* @return String
*/
private String getFileNamelong() {
//TODO: 06/10/2015
return "READY "+store3+" "+name3+" "+lildate+".pdf";
}
private String getPastName() {
//TODO: 06/10/2015
return store3+" READY "+tvresult+" "+lildate+" "+name3;
}
public void runshort2(FileOutputStream outputStream) throws IOException {
// Create a shiny new (but blank) PDF document in memory
// We want it to optionally be printable, so add PrintAttributes
// and use a PrintedPdfDocument. Simpler: new PdfDocument().
PrintAttributes printAttrs = new PrintAttributes.Builder().
setColorMode(PrintAttributes.COLOR_MODE_COLOR).
setMediaSize(PrintAttributes.MediaSize.NA_LETTER).
setResolution(new Resolution("zooey", PRINT_SERVICE, 300, 300)).
setMinMargins(Margins.NO_MARGINS).
build();
PdfDocument document = new PrintedPdfDocument(this, printAttrs);
DisplayMetrics metrics = new DisplayMetrics(); //for all android versions
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int width = (metrics.widthPixels);
int height = (metrics.heightPixels);
View content = findViewById(R.id.page1);
// crate a page description
PageInfo pageInfo = new PageInfo.Builder(width, height, 1).create();
// create a new page from the PageInfo
Page page = document.startPage(pageInfo);
// repaint the user's text into the page
content.draw(page.getCanvas());
// do final processing of the page
document.finishPage(page);
document.writeTo(outputStream);
//close the document
document.close();
// Here you could add more pages in a longer doc app, but you'd have
// to handle page-breaking yourself in e.g., write your own word processor...
// Now write the PDF document to a file; it actually needs to be a file
// since the Share mechanism can't accept a byte[]. though it can
// accept a String/CharSequence. Meh.
}
' ''