2015-11-04 45 views
0

任何人都可以幫助解釋爲什麼此代碼在Geneymotion模擬器中工作,但不在任何實際的Android設備中?代碼在模擬器中完美工作,我對它沒有任何問題,我完全不知道爲什麼它在實際設備中不起作用。AndroidStudio將PDF附加到電子郵件問題

這裏的方法,該方法被調用時,點擊「創建PDF」按鈕:

try { 

     String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/invoices"; 

     File dir = new File(path); 
     if(!dir.exists()) 
      dir.mkdirs(); 

     Log.d("PDFCreator", "PDF Path: " + path); 

     File delete = new File(dir, "invoice.pdf"); 
     if (delete.exists()) 
      delete.delete(); 

     File file = new File(dir, "invoice.pdf"); 
     FileOutputStream fOut = new FileOutputStream(file); 

     PdfWriter.getInstance(doc, fOut); 

     //open the document 
     doc.open(); 
     Log.d("test", "PDF Doccument Opened for input"); 

在模擬器: - 當我點擊「創建PDF」按鈕,郵件意圖被調用,我可以選擇使用電子郵件客戶端並通過它發送PDF。

在實際的Android設備中: 當我在實際設備中單擊「創建PDF」時,絕對沒有任何反應。

我認爲問題可能圍繞我要存儲PDF的目錄進行,但我無法解決它。

如果有人能幫助我,將不勝感激。提前致謝。

注意:我使用DroidText庫來生成PDF。從實際設備上測試

日誌說:

11-05 21:09:14.839 10375-10375/motawaze.com.invoicepdf D/dalvikvm﹕ GC_EXTERNAL_ALLOC freed 93K, 47% free 2913K/5447K, external 0K/0K, paused 30ms 
11-05 21:09:25.989 10375-10375/motawaze.com.invoicepdf D/CLIPBOARD﹕ Hide Clipboard dialog at Starting input: finished by someone else... ! 
11-05 21:09:29.849 10375-10378/motawaze.com.invoicepdf D/dalvikvm﹕ GC_CONCURRENT freed 153K, 47% free 3043K/5639K, external 171K/1281K, paused 7ms+2ms 
11-05 21:09:39.899 10375-10378/motawaze.com.invoicepdf D/dalvikvm﹕ GC_CONCURRENT freed 351K, 48% free 3105K/5895K, external 171K/1281K, paused 7ms+3ms 
11-05 21:09:53.639 10375-10375/motawaze.com.invoicepdf D/test﹕ set the fields for PDF input 
11-05 21:09:53.649 10375-10375/motawaze.com.invoicepdf D/test﹕ set the document 
11-05 21:09:53.669 10375-10375/motawaze.com.invoicepdf D/test﹕ set the calender 
11-05 21:09:53.669 10375-10375/motawaze.com.invoicepdf D/PDFCreator﹕ PDF Path: /mnt/sdcard/Download/invoices 
11-05 21:09:53.669 10375-10375/motawaze.com.invoicepdf E/PDFCreator﹕ ioException:java.io.FileNotFoundException: /mnt/sdcard/Download/invoices/Invoice.pdf (No such file or directory) 
11-05 21:10:31.420 10375-10378/motawaze.com.invoicepdf D/dalvikvm﹕ GC_CONCURRENT freed 358K, 48% free 3152K/5959K, external 171K/1281K, paused 7ms+2ms 
+0

請在這裏發表您的日誌。 –

+0

如果代碼在模擬器中沒有問題,我看不到如何發佈日誌? –

+0

我只想看看你在這一行中得到了什麼:Log.d(「PDFCreator」,「PDF Path:」+ path); –

回答

1

由於在聊天中提到過。看起來你的PDF沒有被創建,因此你得到FileNotFoundException。查看你的PDF創建代碼。那就是問題所在。

更改這些行:

if(!dir.exists()) 
    dir.mkdirs(); 

到:

boolean created = false; 
if(!dir.exists()) 
    created = dir.mkdirs(); 

if(created) 
    Log.d("test", "Path created"); 

這將檢查是否創建與否的目錄。

+0

如果你可以在我的場景中解釋什麼可能會導致應用程序在模擬器中工作,但在實際設備中拋出FileNotFoundException,那麼需要了解更多的細節來幫助我們。如果你這樣做,我會選擇你的答案是正確的。 –

+0

將設備的整個日誌放在這裏。 –

+0

我在薑餅android設備上測試它。我假設它與訪問目錄有關,但我已經在Android清單文件中設置了它:'<使用權限android:name =「android.permission.READ_EXTERNAL_STORAGE」/> '。這被放在'' –

0

此代碼可能會對您有所幫助。

我使用DroidText.0.2.jar進行PDF創建。

public void createPDF() 
    { 
     Document doc = new Document(); 

     try { 
      path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/droidText"; 
//   File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS); 

      File dir = new File(path); 
      if(!dir.exists()) 
       dir.mkdirs(); 

      Log.d("PDFCreator", "PDF Path: " + path); 


      file = new File(path, "HomeInventory.pdf"); 
      FileOutputStream fOut = new FileOutputStream(file); 

      PdfWriter.getInstance(doc, fOut); 

      //open the document 
      doc.open(); 


      Paragraph p1 = new Paragraph("Hi! I am generating my first PDF using DroidText"); 
      Font paraFont= new Font(Font.COURIER); 
      p1.setAlignment(Paragraph.ALIGN_CENTER); 
      p1.setFont(paraFont); 

      //add paragraph to document 
      doc.add(p1); 

      Paragraph p2 = new Paragraph("This is an example of a paragraph"); 
      Font paraFont2= new Font(Font.COURIER,14.0f, Color.GREEN); 
      p2.setAlignment(Paragraph.ALIGN_CENTER); 
      p2.setFont(paraFont2); 

      doc.add(p2); 

      ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
//   Bitmap bitmap = BitmapFactory.decodeResource(getBaseContext().getResources(), R.drawable.android); 
      Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.action_search); 
      bitmap.compress(Bitmap.CompressFormat.JPEG, 100 , stream); 
      Image myImg = Image.getInstance(stream.toByteArray()); 
      myImg.setAlignment(Image.MIDDLE); 
      //add image to document 
      doc.add(myImg); 

      //set footer 
      Phrase footerText = new Phrase("This is an example of a footer"); 
      HeaderFooter pdfFooter = new HeaderFooter(footerText, false); 
      doc.setFooter(pdfFooter); 



     } catch (DocumentException de) { 
      Log.e("PDFCreator", "DocumentException:" + de); 
     } catch (IOException e) { 
      Log.e("PDFCreator", "ioException:" + e); 
     } 
     finally 
     { 
      doc.close(); 
     } 
    } 

裝上此創建PDF文件以電子郵件這樣的方式..

String[] mailto = {"[email protected]"}; 
       Uri uri = Uri.fromFile(file); 

       Intent emailIntent = new Intent(Intent.ACTION_SEND,Uri.parse("mailto:")); 
       emailIntent.setType("text/plain"); 
       emailIntent.putExtra(Intent.EXTRA_EMAIL, mailto); 
       emailIntent.putExtra(Intent.EXTRA_SUBJECT, "My Subject"); 
       emailIntent.putExtra(Intent.EXTRA_TEXT, "My Body"); 

       emailIntent.putExtra(Intent.EXTRA_STREAM, uri); 

       emailIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // this will make such that when user returns to your app, your app is displayed, instead of the email app. 
       getActivity().startActivity(emailIntent);