у меня есть форма, откуда я взял пользовательский ввод, и она включает в себя флажок и поле edittext; Мне нужно отправить это по электронной почте. Я попытался использовать itext для PDF и даже CSV-файлов, но ни один из них не работает. Мой CSV-файл отправляет электронную почту, но не отправляет данные вместе с ним.
MainActivity:
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.FileProvider;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import java.io.File;
import java.io.FileOutputStream;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void export(View view) {
//generate data
StringBuilder data = new StringBuilder();
data.append("Time,Distance");
for(int i = 0; i<5; i++){
data.append("\n"+String.valueOf(i)+","+String.valueOf(i*i));
}
try{
//saving the file into device
FileOutputStream out = openFileOutput("data.csv", Context.MODE_PRIVATE);
out.write((data.toString()).getBytes());
out.close();
//exporting
Context context = getApplicationContext();
File filelocation = new File(getFilesDir(), "data.csv");
Uri path = FileProvider.getUriForFile
(context, "com.example.exportcsv.fileprovider", filelocation);
Intent fileIntent = new Intent(Intent.ACTION_SEND);
fileIntent.setType("text/csv");
fileIntent.putExtra(Intent.EXTRA_SUBJECT, "Data");
fileIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
fileIntent.putExtra(Intent.EXTRA_STREAM, path);
startActivity(Intent.createChooser(fileIntent, "Send mail"));
}
catch(Exception e){
e.printStackTrace();
}
}
}
Provider_paths:
<paths>
<files-path
name="data"
path="."/>
</paths>
Activity_main:
<ScrollView
android:id="@+id/scrollView2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="1dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#72C8E8"
android:padding="4sp"
android:paddingLeft="4sp"
android:paddingTop="4sp"
android:paddingRight="4sp"
android:paddingBottom="4sp"
android:text="@string/name"
android:textColor="@color/colorPrimaryDark"
android:textSize="30sp"
android:textStyle="bold" />
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName" />
<TextView
android:id="@+id/textView2"
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#72C8E8"
android:text="@string/age"
android:padding="4sp"
android:paddingLeft="4sp"
android:paddingTop="4sp"
android:paddingRight="4sp"
android:paddingBottom="4sp"
android:textColor="@color/colorPrimaryDark"
android:textSize="30sp"
android:textStyle="bold" />
<EditText
android:id="@+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number" />
<TextView
android:id="@+id/textView3"
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#72C8E8"
android:padding="4sp"
android:paddingLeft="4sp"
android:paddingTop="4sp"
android:paddingRight="4sp"
android:paddingBottom="4sp"
android:text="@string/gender"
android:textColor="@color/colorPrimaryDark"
android:textSize="30sp"
android:textStyle="bold" />
<CheckBox
android:id="@+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:onClick="onCheckboxClicked"
android:text="@string/m"
android:textSize="24sp" />
<CheckBox
android:id="@+id/checkBox2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:onClick="onCheckboxClicked"
android:text="@string/f"
android:textSize="24sp" />
<TextView
android:id="@+id/textView4"
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#72C8E8"
android:padding="4sp"
android:paddingLeft="4sp"
android:paddingTop="4sp"
android:paddingRight="4sp"
android:paddingBottom="4sp"
android:text="@string/address"
android:textColor="@color/colorPrimaryDark"
android:textSize="30sp"
android:textStyle="bold" />
<EditText
android:id="@+id/editText3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPostalAddress" />
<TextView
android:id="@+id/textView5"
android:layout_width="match_parent"
android:layout_marginTop="20dp"
android:layout_height="wrap_content"
android:background="#72C8E8"
android:text="@string/phone"
android:padding="4sp"
android:paddingLeft="4sp"
android:paddingTop="4sp"
android:paddingRight="4sp"
android:paddingBottom="4sp"
android:textColor="@color/colorPrimaryDark"
android:textSize="30sp"
android:textStyle="bold" />
<EditText
android:id="@+id/editText4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number" />
<Button
android:id="@+id/button"
android:layout_marginTop="30dp"
android:layout_gravity="center_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="24dp"
android:textSize="24sp"
android:onClick="export"
android:padding="4sp"
android:background="#72C8E8"
android:paddingLeft="4sp"
android:paddingTop="4sp"
android:paddingRight="4sp"
android:paddingBottom="4sp"
android:text="@string/export" />
</LinearLayout>
</ScrollView>