簡體   English   中英

如何在Android KitKat上創建.txt文件並通過電子郵件發送

[英]How to create a .txt file on Android KitKat and send it with an email intent

因此,我已經嘗試了一天,在SD卡上創建.txt文件,只是發現不是我的代碼錯了,但是KitKat不再允許開發人員在外部SD卡上存儲文件; 周圍是否有諸如臨時.txt文件之類的東西,或者有一些創建文件然后發送的新代碼? 這是我當前的代碼不起作用

 String DataIn = PhoneNumber + "," + dataLong + "," + dataLat;
        try
        {
        File storageDirectory = new File ("/sdcard/LocationData.txt");
            storageDirectory.createNewFile();
            FileOutputStream fout = new FileOutputStream(storageDirectory);
            OutputStreamWriter myoutwriter = new OutputStreamWriter(fout);
            myoutwriter.append(DataIn);
            myoutwriter.close();Toast.makeText(getBaseContext(),"Saved", Toast.LENGTH_SHORT).show();
        }
        catch (Exception e)
        {
            Toast.makeText(getBaseContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
        }
        Intent emailintent = new Intent(Intent.ACTION_SEND);
        emailintent.setType("text/plain");
        emailintent.putExtra(Intent.EXTRA_EMAIL, new String[]{"xxxxxxxx@xxxxxxxxx.com"});
        emailintent.putExtra(Intent.EXTRA_SUBJECT, "Data");
        emailintent.putExtra(Intent.EXTRA_TEXT, "Hello World!");
        File root = Environment.getRootDirectory();
        String DataAttachment = "/sdcard/LocationData.txt";
        File filer = new File(root, DataAttachment);
        if (!filer.exists() || filer.canRead())
        {
            return;
        }
        Uri uri = Uri.fromFile(filer);
        emailintent.putExtra(Intent.EXTRA_STREAM, uri);
        startActivity(Intent.createChooser(emailintent, "Choose an Email provider"));

我使用此代碼在外部存儲上創建.txt文件,並且在所有版本上均能正常工作

 File rootPath = new File(Environment.getExternalStorageDirectory(), DNAME);
    if(!rootPath.exists()) {
        rootPath.mkdirs();
    }
    File dataFile = new File(rootPath, FILENAME);
    if(!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
        Toast.makeText(this, "Cannot use storage.", Toast.LENGTH_SHORT).show();
        finish();
        return;
    }
    try {           
        FileOutputStream mOutput = new FileOutputStream(dataFile, false);
        String data = "DATA";
        mOutput.write(data.getBytes());
        mOutput.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
        File dirs[] = ContextCompat.getExternalFilesDirs(getApplicationContext(),null);

        String Info = "getExternalFilesDirs()\ndirs.length: " + dirs.length;
        int nr = -1;            
        while ( ++nr < dirs.length )
            Info += "\n"+  dirs[nr].getAbsolutePath();

        Toast.makeText(this, Info, Toast.LENGTH_LONG).show();

您需要/Project/libs/android-support-v4.jar文件。

對我來說,以下解決方案有效。 創建一個文本文件使用此

    File root = new File(DIRECTORY_PATH);
    File gpxfile = new File(root, "samples.txt");
    FileWriter writer = new FileWriter(gpxfile);
    writer.append("First string is here to be written.");
    writer.flush();
    writer.close();

用於電子郵件

    Intent shareIntent = new Intent(Intent.ACTION_SEND);
    shareIntent.setData(Uri.parse("mailto:"));
    shareIntent.putExtra(Intent.EXTRA_EMAIL, new String[] { "email" });
    shareIntent.putExtra(Intent.EXTRA_SUBJECT, "test " +    "Subject");
    shareIntent.putExtra(Intent.EXTRA_TEXT, "test body");
    shareIntent.setType("application/octet-stream");
            shareIntent.putExtra(Intent.EXTRA_STREAM,Uri.parse("file://"+DIRECTORY_PATH+"/samples.txt"));
    startActivityForResult(Intent.createChooser(shareIntent, "Choose service"),EMAIL_ACTIVITY_REQUEST_CODE);

您好使用此方法將文件存儲在SD卡中:

 public void generateNoteOnSD(String sFileName, String sBody) {
    try {
        File root = new File(Environment.getExternalStorageDirectory(), "hChat");
        if (!root.exists()) {
            root.mkdirs();
        }
        File gpxfile = new File(root, sFileName);
        FileWriter writer = new FileWriter(gpxfile);
        writer.append(sBody);
        writer.flush();
        writer.close();
        Toast.makeText(context, "Saved", Toast.LENGTH_SHORT).show();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

並有意發送此文件,然后使用這些LOC

** File sd = Environment.getExternalStorageDirectory();
            File fileDir= new File(sd, "dir_path");
            Intent email = new Intent(Intent.ACTION_SEND);
            email.putExtra(Intent.EXTRA_STREAM, Uri.parse(fileDir.getAbsolutePath() + "/"
                            + "hchatDB.txt"));
                    startActivity(Intent.createChooser(email , "Email: Text File"));**

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM