如何在android studio中以csv文件或pdf格式发送表单数据

问题描述 投票:0回答:1

我有一个表单,我在其中接受用户输入,其中包括复选框和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>
android csv email pdf itext
1个回答
0
投票

[请参考本教程以发送带有附件的电子邮件:https://www.google.com/amp/s/javatutorial.net/send-email-with-attachments-android/amp您也不需要“ text / csv”模仿类型,这是针对电子邮件的,因为您需要纯文本或html,所以它是错误的。除此之外,您还具有文件读取权限吗?

© www.soinside.com 2019 - 2024. All rights reserved.