Cannot create and open PDF using JAVA android -


i create pdf document using pdfdocument api. this, have created demo function called getticketaspdf , has sub function called addtext add text pdf document.

all seems run, when choose file dialog appears , select adobe reader, error message saying

this file not accessed. check location or network , try again

i understand documents saved in application need have uri generated using fileprovider have done, , works csv files generate using stringbuilder, not pdf, dont think problem fileprovider.

here code

private void getticketaspdf() {     try {         int width = 595;         int height = 842;          // create new document         pdfdocument document = new pdfdocument();          // crate page description         pdfdocument.pageinfo pageinfo = new pdfdocument.pageinfo.builder(width, height, 1).create();          // start page         pdfdocument.page page = document.startpage(pageinfo);          // canvas         canvas canvas = page.getcanvas();          // color         paint paint = new paint();         paint.setcolor(color.parsecolor("#ff0000"));          // draw header square         canvas.drawrect(25, 25, width - 25, 50, paint);         addtext(canvas, "my header", "#ffffff", 100, 50, 50, 1);         addtext(canvas, "and simple text", "#ff0000", 25, 100, 100, 1);          // finish page         document.finishpage(page);          // write document content         file file = new file(context.getfilesdir(), "test.pdf");         fileoutputstream fileoutputstream = new fileoutputstream(file);         document.writeto(fileoutputstream);         document.close();         fileoutputstream.close();          // make file available         uri contenturi = fileprovider.geturiforfile(context, "com.mycompany.myapp.fileprovider", file);         intent target = new intent(intent.action_view);         target.setflags(intent.flag_activity_new_task);         target.setdataandtype(contenturi, "application/pdf");          intent intent = intent.createchooser(target, "open file");         context.startactivity(intent);      } catch (exception e) {         e.printstacktrace();     } }  private static void addtext(canvas canvas, string text, string color, int size, int x, int y, int align) {     paint paint = new paint();     paint.setcolor(color.parsecolor(color));     paint.settextalign(align == 0 ? paint.align.left : align == 1 ? paint.align.center : paint.align.right);     //paint.settypeface(font);     paint.settextsize(size);      canvas.drawtext(text, x, y, paint); } 

as said have setup fileprovider , works on function, encase different pdf, here is

res\xml\file_paths.xml

<?xml version="1.0" encoding="utf-8"?> <paths xmlns:android="http://schemas.android.com/apk/res/android">     <files-path name="my_files" path="" /> </paths> 

androidmanifest.xml

<provider android:authorities="com.mycompany.myapp.fileprovider" android:exported="false" android:granturipermissions="true" android:name="android.support.v4.content.fileprovider">     <meta-data android:name="android.support.file_provider_paths" android:resource="@xml/file_paths" /> </provider> 


Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -