2017-11-02 16 views
0

私はテキストとイメージをpdfにエクスポートしています。そしてそれはデバイスメモリに保存されます。 テキストが正常にエクスポートされています。 イメージのパスをdatabase.Soに保存しています。イメージをpdfにエクスポートするとイメージのパスが表示されます。 イメージパスからイメージを取得してpdfドキュメントに追加するにはどうすればよいですか? アダプタでpdf機能を実行しています。私を助けてください。ここでイメージのパスからイメージを取得してPDFに変換する

は私のコードです:

 //pdf// 
     pdf = (TextView)convertView.findViewById(R.id.pdf); 
     pdf.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 

///// 
       String FILE = Environment.getExternalStorageDirectory().toString() 
         + "/PDF/" + list.get(position).getTitle()+".pdf"; 

       // Add Permission into Manifest.xml 
       // <uses-permission 
       // android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 

       // Create New Blank Document 
       Document document = new Document(PageSize.A4); 

       // Create Directory in External Storage 
       String root = Environment.getExternalStorageDirectory().toString(); 
       File myDir = new File(root + "/PDF"); 
       myDir.mkdirs(); 

       // Create Pdf Writer for Writting into New Created Document 
       try { 
        PdfWriter.getInstance(document, new FileOutputStream(FILE)); 

        // Open Document for Writting into document 
        document.open(); 

        // User Define Method 
       //document.add(list.get(position).getDate()); 
       addMetaData(document); 
       addTitlePage(document,list.get(position).getDate(),list.get(position).getTitle(),list.get(position).getContent(),list.get(position).getPicture()); 
       } catch (FileNotFoundException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } catch (DocumentException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
       // Close Document after writting all content 
       document.close(); 

       //Toast.makeText(this, "PDF File is Created. Location : " + FILE, 
        //  Toast.LENGTH_LONG).show(); 

       ////// 
      } 
     }); 
     //pdf// 





//pdf// 
// Set PDF document Properties 
public void addMetaData(Document document) { 
document.addTitle("All memories"); 
} 
public void addTitlePage(Document document,String date, String title,  String content, String picture) throws DocumentException, IOException { 
    // Font Style for Document 
    Font catFont = new Font(Font.FontFamily.TIMES_ROMAN, 18, Font.BOLD); 
    Font titleFont = new Font(Font.FontFamily.TIMES_ROMAN, 22, Font.BOLD 
      | Font.UNDERLINE, BaseColor.GRAY); 
    Font smallBold = new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.BOLD); 
    Font normal = new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.NORMAL); 

    // Start New Paragraph 
    Paragraph prHead = new Paragraph(); 
    // Set Font in this Paragraph 
    prHead.setFont(titleFont); 
    // Add item into Paragraph 
    prHead.add("All Memories"); 
    //prHead.add(date + "\n"); 
    // Create Table into Document with 1 Row 
    PdfPTable myTable = new PdfPTable(1); 
    // 100.0f mean width of table is same as Document size 
    myTable.setWidthPercentage(100.0f); 

    // Create New Cell into Table 
    PdfPCell myCell = new PdfPCell(new Paragraph("")); 
    myCell.setBorder(Rectangle.BOTTOM); 

    // Add Cell into Table 
    myTable.addCell(myCell); 

    prHead.setFont(catFont); 
    //prHead.add("\nName1 Name2\n"); 
    prHead.setAlignment(Element.ALIGN_CENTER); 

    // Add all above details into Document 
    document.add(prHead); 
    document.add(myTable); 

    document.add(myTable); 

    // Now Start another New Paragraph 
    Paragraph prPersinalInfo = new Paragraph(); 
    prPersinalInfo.setFont(smallBold); 
    prPersinalInfo.add(date+"\n"); 
    prPersinalInfo.add(title+"\n"); 
    prPersinalInfo.add(content+"\n"); 
    prPersinalInfo.add(picture+"\n"); 

// Convert the Image to Bitmap 


    Image img =Image.getInstance(picture); 

     img.setAlignment(Image.ALIGN_CENTER | Image.ALIGN_BOTTOM); 

     prPersinalInfo.add(img); 

     // Convert the Image to Bitmap 

// Convert the Image to Bitmap 

    prPersinalInfo.setAlignment(Element.ALIGN_CENTER); 

    document.add(prPersinalInfo); 
    document.add(myTable); 

    document.add(myTable); 

    // Create new Page in PDF 
    document.newPage(); 
} 


//pdf// 
+0

私はあなたが ' 'PDFに画像を変換したいのか理解していませんか? –

+0

この回答を見る https://stackoverflow.com/a/36305827/5333711 –

答えて

0
    // Convert the Image to Bitmap 
        Bitmap bitmap = convertImageToBitmap(img.getPath()); 

        ByteArrayOutputStream st = new ByteArrayOutputStream(); 
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, st); 
        byte[] byteArray = st.toByteArray(); 
        Image im = Image.getInstance(byteArray); 
        document.add(im); 
+0

はい、試しました。それは "ImageI = Image.getInstance(byteArray)"行のメソッド "convertImageToBitmap(?)"と "未処理例外:java.io.IO.Exception"を解決できないことを示しています – teddy

+0

実際に私はこれを行いました。 img = Image.getInstance(picture); img.setAlignment(Image.ALIGN_CENTER | Image.ALIGN_BOTTOM); prPersinalInfo.add(img);リストにピクチャが含まれていない場合は、nullポインタ例外を示します。どのように画像を持っていないためにこれを解決するには? – teddy

+0

img.getPath!= nullかどうかを確認し、存在する場合はイメージを取得します。 –

0

このコードは動作します。..

pdf.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 

///// 
       String FILE = Environment.getExternalStorageDirectory().toString() 
         + "/PDF/" + list.get(position).getTitle()+".pdf"; 

       // Add Permission into Manifest.xml 
       // <uses-permission 
       // android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 

       // Create New Blank Document 
       Document document = new Document(PageSize.A4); 

       // Create Directory in External Storage 
       String root = Environment.getExternalStorageDirectory().toString(); 
       File myDir = new File(root + "/PDF"); 
       myDir.mkdirs(); 

       // Create Pdf Writer for Writting into New Created Document 
       try { 
        PdfWriter.getInstance(document, new FileOutputStream(FILE)); 

        // Open Document for Writting into document 
        document.open(); 

        // User Define Method 
       //document.add(list.get(position).getDate()); 
       addMetaData(document); 
       addTitlePage(document,list.get(position).getDate(),list.get(position).getTitle(),list.get(position).getContent(),list.get(position).getPicture()); 
       } catch (FileNotFoundException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } catch (DocumentException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
       // Close Document after writting all content 
       document.close(); 

       //Toast.makeText(this, "PDF File is Created. Location : " + FILE, 
        //  Toast.LENGTH_LONG).show(); 

       ////// 
      } 
     }); 



// Set PDF document Properties 
public void addMetaData(Document document) { 
document.addTitle("All memories"); 
} 
public void addTitlePage(Document document,String date, String title, String content, String picture) throws DocumentException, IOException { 
    // Font Style for Document 
    Font catFont = new Font(Font.FontFamily.TIMES_ROMAN, 18, Font.BOLD); 
    Font titleFont = new Font(Font.FontFamily.TIMES_ROMAN, 22, Font.BOLD 
      | Font.UNDERLINE, BaseColor.GRAY); 
    Font smallBold = new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.BOLD); 
    Font normal = new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.NORMAL); 

    // Start New Paragraph 
    Paragraph prHead = new Paragraph(); 
    // Set Font in this Paragraph 
    prHead.setFont(titleFont); 
    // Add item into Paragraph 
    prHead.add("All Memories"); 
    //prHead.add(date + "\n"); 
    // Create Table into Document with 1 Row 
    PdfPTable myTable = new PdfPTable(1); 
    // 100.0f mean width of table is same as Document size 
    myTable.setWidthPercentage(100.0f); 

    // Create New Cell into Table 
    PdfPCell myCell = new PdfPCell(new Paragraph("")); 
    myCell.setBorder(Rectangle.BOTTOM); 

    // Add Cell into Table 
    myTable.addCell(myCell); 

    prHead.setFont(catFont); 
    //prHead.add("\nName1 Name2\n"); 
    prHead.setAlignment(Element.ALIGN_CENTER); 

    // Add all above details into Document 
    document.add(prHead); 
    document.add(myTable); 

    document.add(myTable); 

    // Now Start another New Paragraph 
    Paragraph prPersinalInfo = new Paragraph(); 
    prPersinalInfo.setFont(smallBold); 
    prPersinalInfo.add(date+"\n"); 
    prPersinalInfo.add(title+"\n"); 
    prPersinalInfo.add(content+"\n"); 


// Convert the Image to Bitmap 

if(picture!=null) { 
Image img = Image.getInstance(picture); 


img.setAlignment(Image.ALIGN_CENTER | Image.ALIGN_BOTTOM); 

prPersinalInfo.add(img); 
} 
     // Convert the Image to Bitmap 

// Convert the Image to Bitmap 

    prPersinalInfo.setAlignment(Element.ALIGN_CENTER); 

    document.add(prPersinalInfo); 
    document.add(myTable); 

    document.add(myTable); 

    // Create new Page in PDF 
    document.newPage(); 
} 
関連する問題