#前提・実現したいこと
GmailにCSVファイルを添付しようとしています。
Android Studio上でのエラーは無いのですが、エミュレータで送信ボタンをタップするとGmailが起動し「ファイルを添付できませんでした」とメッセージ(Toast風)が表示されます。
当然ですが、そのまま送信すると添付ファイルが無いままメール送信します。CSVファイルは/storage/emulated/0/tmp.csvとして作成されていてGmailから手動であれば添付ファイルを設定できます。また、そのファイルの内容も意図したものに都度上書きされています。
どこを直せばよいのか教えていただけないでしょうか?
##該当のソースコード
Android
1//CountActivity.java// 2ImageButton btnMail = findViewById(R.id.btnSend2); 3 btnMail.setOnClickListener(new View.OnClickListener() { 4 5 @Override 6 public void onClick(View v) { 7 8 String tmpCsvPath = Environment.getExternalStorageDirectory().getPath() + "/tmp.csv"; 9 10 try { 11 // csvファイル作成 12 BufferedWriter bw = 13 new BufferedWriter( 14 new OutputStreamWriter( 15 new FileOutputStream( tmpCsvPath, false), "Shift_JIS")); 16 bw.write("No.," + den.getText().toString()); 17 bw.newLine(); 18 bw.write("日付," + da.getText().toString()); 19 bw.newLine(); 20 // 中略 ------ 21 bw.flush(); 22 bw.close(); 23 24 Intent intent = new Intent(); 25 intent.setAction(Intent.ACTION_SEND); 26 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);// ファイルを読み込む権限を与える 27 intent.putExtra(Intent.EXTRA_SUBJECT, ""); 28 intent.putExtra(Intent.EXTRA_TEXT,""); 29 // Gmailにファイルの添付 30 Uri attachments = Uri.parse("content://" + tmpCsvPath); 31 intent.putExtra(Intent.EXTRA_STREAM, attachments); 32 intent.setType("message/rfc822"); 33 intent.setPackage("com.google.android.gm"); 34 try { 35 startActivity(intent); 36 } catch (android.content.ActivityNotFoundException ex) { 37 ex.printStackTrace(); 38 Toast.makeText(CountActivity.this.getApplicationContext(), "client not found", Toast.LENGTH_LONG).show(); 39 } 40 } catch (UnsupportedEncodingException e) { 41 e.printStackTrace(); 42 Toast.makeText(CountActivity.this, "UnsupportedEncoding", Toast.LENGTH_LONG).show(); 43 } catch (FileNotFoundException e) { 44 e.printStackTrace(); 45 Toast.makeText(CountActivity.this, "FileNotFound", Toast.LENGTH_LONG).show(); 46 } catch (IOException e) { 47 e.printStackTrace(); 48 Toast.makeText(CountActivity.this, "IOException", Toast.LENGTH_LONG).show(); 49 } 50 } 51 });
xml
1<?xml version="1.0" encoding="utf-8"?> 2<manifest xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:tools="http://schemas.android.com/tools" 4 package="com. "> 5 6 <uses-permission android:name="android.permission.INTERNET"/> 7 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> 8 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 9 <uses-permission android:name="android.permission.ACCESS_PROVIDER"/> 10 <uses-permission android:name="android.permission.READ_ATTACHMENT"/> 11 <application 12 android:allowBackup="true" 13 android:icon="@mipmap/ic_launcher" 14 android:label="@string/app_name" 15 android:roundIcon="@mipmap/ic_launcher_round" 16 android:supportsRtl="true" 17 android:theme="@style/AppTheme" 18 tools:ignore="AllowBackup,GoogleAppIndexingWarning"> 19 <provider 20 android:name="android.support.v4.content.FileProvider" 21 android:authorities="${applicationId}.provider" 22 android:exported="false" 23 android:grantUriPermissions="true"> 24 <meta-data 25 android:name="android.support.FILE_PROVIDER_PATHS" 26 android:resource="@xml/provider_paths"/> 27 </provider> 28 29 <activity android:name=" .MainActivity" 30 android:screenOrientation="portrait" 31 android:windowSoftInputMode="adjustResize"> 32 <intent-filter> 33 <action android:name="android.intent.action.MAIN" /> 34 35 <category android:name="android.intent.category.LAUNCHER" /> 36 </intent-filter> 37 </activity> 38 39 <activity android:name=" .CountActivity" 40 android:screenOrientation="portrait"> 41 <intent-filter> 42 <action android:name="android.intent.action.MAIN" /> 43 44 <category android:name="android.intent.category.LAUNCHER" /> 45 </intent-filter> 46 </activity> 47 </application> 48 49</manifest>

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。