Susnt:
私は、データベースを取得したフォルダの文書で.xlsファイルを作成し、電子メールまたはのWhatsAppでの添付ファイルとして送信するためのボタンを作成しています。しかし、私もマニフェストファイルへのアクセス権を追加した後mailjava.io.FileNotFoundExceptionエラーを取得しています。私は、私が行方不明です小さなその何かを知っているが、把握することはできません。
- 私は、ファイルが正常に作成されますが、電子メールまたはのWhatsAppの添付ファイルとして取り込むことができません内部キャッシュディレクトリ内のファイルを作成しようとしています。
- 私は添付ファイルとして送信した後、とにかくそれを削除していますので、私は、ファイルを保存しなければならない問題ありません。
メール意図で、私はUri.parseとしてウリ( "内容://"、file.getAbsolutePath())を果たしているfile.toUri(); ウリのuri = Uri.fromFile(ファイル); すべては、このエラーを示す同じ結果を持っています。親クラスは、すべての変数を定義しています
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 protected]"}); 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に添付ファイルとして取られるべきです
Susnt:
唯一のインスタンスは、実際のファイルを作成していなかったように私はちょうどmailjava.io.FileNotFoundExceptionの責任誤りを見つけました。
基本的には、この責任2個のエラーがありました。
- 私は(ないのonClickメソッドで)クラス自体にFileインスタンス(ファイル)を宣言しました。それはクラス変数で宣言されたので、それは確かにcatchブロックでキャッチURIを暴露FileUriExposedExceptionをスローします。
- 私はによるとGmailの5.0に許可されていない内部のキャッシュされたディレクトリから、そのファイルを取得しようとしていたこの問題
解決
- onClickの方法自体で宣言ファイルインスタンスURIが露出しないように。
- それは世界でアクセス可能で、GMailの5.0で受け入れられているためExternalSDでファイルを作成します。
imbtnMail.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
Cursor cursor = dbManager.fetch(DatabaseHelper.TICKET_TABLE);
File file = new File(Environment.getExternalStorageDirectory(),"Report-"+DateUtil.timeMilisToString(System.currentTimeMillis(),"hh-MM-yy")+".xls");
Toast.makeText(getApplicationContext(),"File saved",Toast.LENGTH_LONG).show();
WorkbookSettings wbSettings = new WorkbookSettings();
wbSettings.setLocale(new Locale("en", "EN"));
WritableWorkbook workbook;
workbook = Workbook.createWorkbook(file, wbSettings);
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("text/plain");
intent.putExtra(Intent.EXTRA_EMAIL, new String[] {"[email protected]"});
intent.putExtra(Intent.EXTRA_SUBJECT, "subject here");
intent.putExtra(Intent.EXTRA_TEXT, "body text");
if (!file.exists() || !file.canRead()) {
Toast.makeText(getApplicationContext(), "Attachment Error", Toast.LENGTH_SHORT).show();
return;
}
Uri uri = Uri.fromFile(file);
intent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(intent, "Send email..."));
}
catch (Exception e){e.printStackTrace();}
}
});