【发布时间】:2019-02-02 18:45:39
【问题描述】:
我正在创建一个按钮,它将获取数据库,在文档文件夹中创建 .xls 文件并将其作为电子邮件或 whatsapp 的附件发送。但是,即使在向清单文件添加权限后,我也会收到 mailjava.io.FileNotFoundException 错误。我知道我缺少一些小东西,但无法弄清楚。
- 我尝试在内部缓存目录中创建文件,该文件已成功创建,但无法作为电子邮件或 whatsapp 的附件获取。
- 我没有任何问题应该保存文件,因为我在作为附件发送后无论如何都要删除它。
-
在 Email Intent 中,我使用 Uri 作为 Uri.parse("content://", file.getAbsolutePath()) file.toUri(); Uri uri = Uri.fromFile(file);都有相同的结果显示此错误。父类有所有定义的变量
imbtnMail.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { try { Cursor cursor = dbManager.fetch(DatabaseHelper.TICKET_TABLE); String filename = "Report-" + DateUtil.timeMilisToString(System.currentTimeMillis(), "ddMMyy"); String xlsfile = filename + ".xlx"; file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS), xlsfile); Log.i("File written at:",file.getAbsolutePath()); //file path WorkbookSettings wbSettings = new WorkbookSettings(); wbSettings.setLocale(new Locale("en", "EN")); WritableWorkbook workbook; workbook = Workbook.createWorkbook(file, wbSettings); //Excel sheet name. 0 represents first sheet WritableSheet sheet = workbook.createSheet("Daily Report", 0); // column and row sheet.addCell(new Label(0, 0, "Pass Report")); sheet.addCell(new Label(1, 0, "User")); if (cursor.moveToFirst()) { do { String Rno = cursor.getString(cursor.getColumnIndex(DatabaseHelper.TICKET_RECEIPT)); String vno = cursor.getString(cursor.getColumnIndex(DatabaseHelper.TICKET_V_NO)); String vtype = cursor.getString(cursor.getColumnIndex(DatabaseHelper.TICKET_V_TYPE)); String vin = cursor.getString(cursor.getColumnIndex(DatabaseHelper.TICKET_V_IN_TIME)); String vout = cursor.getString(cursor.getColumnIndex(DatabaseHelper.TICKET_V_OUT_TIME)); int i = cursor.getPosition() + 1; sheet.addCell(new Label(0, i, Rno)); sheet.addCell(new Label(1, i, vno)); sheet.addCell(new Label(2, i, vtype)); sheet.addCell(new Label(3, i, vin)); sheet.addCell(new Label(4, i, vout)); } while (cursor.moveToNext()); } //closing cursor cursor.close(); workbook.write(); workbook.close(); Intent intent = new Intent(Intent.ACTION_SEND); intent.setType("*/*"); intent.putExtra(Intent.EXTRA_EMAIL, new String[]{"email@example.com"}); intent.putExtra(Intent.EXTRA_SUBJECT, "subject here"); intent.putExtra(Intent.EXTRA_TEXT, "body text"); if (!file.exists() || !file.canRead()) { Toast.makeText(ViewLog.this, "Attachment Error", Toast.LENGTH_SHORT).show(); return; } Uri uri = Uri.fromFile(file); intent.putExtra(Intent.EXTRA_STREAM, uri); startActivityForResult(Intent.createChooser(intent, "Send email..."),ViewLog.REQUEST_WRITE_STORAGE); } catch (Exception e) { System.out.println("is exception raises during sending mail" + e); } } });
这是我点击此按钮后遇到的错误
I/System.out:发送期间是否引发异常 mailjava.io.FileNotFoundException: /storage/emulated/0/Documents/Report-020219.xlx(没有这样的文件或 目录)
Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="mypackagename">
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.READ_PROFILE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme"
tools:ignore="GoogleAppIndexingWarning">
<activity android:name=".VehicleSetup"></activity>
<activity android:name=".About" />
<activity android:name=".Parked" />
<activity android:name=".AppSetup" />
<activity android:name=".ViewLog" />
<activity android:name=".LoginActivity" />
<activity android:name=".Main" />
<activity android:name=".Splashscreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
我希望将此文件写入任何位置,但应作为附件在任何电子邮件客户端或 whatsapp 中使用
【问题讨论】:
标签: java android android-intent