AndroidStudio将PDF附加到电子邮件问题
任何人都可以帮助解释为什么此代码在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
由于在聊天中提到过。看起来你的PDF没有被创建,因此你得到FileNotFoundException
。查看你的PDF创建代码。那就是问题所在。
更改这些行:
if(!dir.exists())
dir.mkdirs();
到:
boolean created = false;
if(!dir.exists())
created = dir.mkdirs();
if(created)
Log.d("test", "Path created");
这将检查是否创建与否的目录。
如果你可以在我的场景中解释什么可能会导致应用程序在模拟器中工作,但在实际设备中抛出FileNotFoundException,那么需要了解更多的细节来帮助我们。如果你这样做,我会选择你的答案是正确的。 –
将设备的整个日志放在这里。 –
我在姜饼android设备上测试它。我假设它与访问目录有关,但我已经在Android清单文件中设置了它:'
此代码可能会对您有所帮助。
我使用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);
请在这里发表您的日志。 –
如果代码在模拟器中没有问题,我看不到如何发布日志? –
我只想看看你在这一行中得到了什么:Log.d(“PDFCreator”,“PDF Path:”+ path); –