查找我自己的应用程序存储的文件

时间:2014-11-12 00:46:43

标签: java android email-attachments

我正在制作一个制作文件的应用,然后我想通过电子邮件发送给自己。我选择电子邮件管理器后,程序只是说:“无法附加文件”。我想我可能写了错误的文件目录,但如果它不是我已经拥有的,我不知道它是什么。

这是电子邮件按钮代码

    btnEmailForm.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String filelocation="data/data/CyberEye/files/form.txt";  // I think the problem is here

            Intent emailIntent = new Intent(Intent.ACTION_SEND);
            // set the type to 'email'
            emailIntent .setType("vnd.android.cursor.dir/email");
            String to[] = {"myemail@place.edu"};
            emailIntent .putExtra(Intent.EXTRA_EMAIL, to);
            // the attachment
            emailIntent .putExtra(Intent.EXTRA_STREAM,  filelocation);
            // the mail subject
            emailIntent .putExtra(Intent.EXTRA_SUBJECT, "AppName");
            startActivity(Intent.createChooser(emailIntent, "Send email..."));
        }
    });

以下是我保存文件的代码

 btnSaveForm.setOnClickListener(new View.OnClickListener() {
                             @Override
                             public void onClick(View v) {
                                      // TODO Auto-generated method stub
                                      textEventName[0] = editEventName.getText().toString();
                                 try {
                                               FileOutputStream fou = openFileOutput("form.txt", MODE_WORLD_READABLE);
                                               OutputStreamWriter osw = new OutputStreamWriter(fou);
                                               try {
                                                        osw.write(String.valueOf(textEventName));
                                                        osw.flush();
                                                        osw.close();
                                                        Toast.makeText(getBaseContext(), "Data saved", Toast.LENGTH_LONG).show();

                                                   } catch (IOException e) {
                                                        // TODO Auto-generated catch block
                                                        e.printStackTrace();
                                                   }
                                          } catch (FileNotFoundException e) {
                                               // TODO Auto-generated catch block
                                               e.printStackTrace();
                                          }
                                 }
                        });

2 个答案:

答案 0 :(得分:0)

    btnSendForm.setOnClickListener(new View.OnClickListener() {
                         @Override
                         public void onClick(View v) {
                             writeToSDFile();
                             }
                    });
@Override
protected void onCreate(Bundle savedInstanceState) {

private void writeToSDFile(){

    // Find the root of the external storage.
    // See http://developer.android.com/guide/topics/data/data-  storage.html#filesExternal

    File root = android.os.Environment.getExternalStorageDirectory();
    //tv.append("\nExternal file system root: "+root);

    // See http://stackoverflow.com/questions/3551821/android-write-to-sd-card-folder

    File dir = new File (root.getAbsolutePath());
    dir.mkdirs();
    File file = new File(dir, "myData.txt");

    try {
        FileOutputStream f = new FileOutputStream(file);
        PrintWriter pw = new PrintWriter(f);
        String stringeventname = editEventName.getText().toString();


        pw.println("Event Name: "+stringeventname);
        pw.println("Hello");
        pw.flush();
        pw.close();
        f.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        Log.i(TAG, "******* File not found. Did you" +
                " add a WRITE_EXTERNAL_STORAGE permission to the   manifest?");
    } catch (IOException e) {
        e.printStackTrace();
    }
    //tv.append("\n\nFile written to "+file);
}

}

    btnEmailForm.setOnClickListener(new View.OnClickListener() {
        @Override
                     public void onClick(View view) {

            final Intent ei = new Intent(Intent.ACTION_SEND_MULTIPLE);
            ei.setType("plain/text");
            ei.putExtra(android.content.Intent.EXTRA_EMAIL,new String[]{"myemail.edu"});
            ei.putExtra(android.content.Intent.EXTRA_SUBJECT, "Testing");
            ei.putExtra(android.content.Intent.EXTRA_TEXT, "Here is another test email");

            File f = new File(Environment.getExternalStorageDirectory().toString());
            for (File temp : f.listFiles()) {
                if (temp.getName().equals("temp.jpg")) {
                    f = temp;
                    break;
                }
            }
            f.setReadable(true, false);                     // This allows external program access
            Uri U = Uri.fromFile(f);
            ArrayList<Uri> uris = new ArrayList<Uri>();
            uris.add(U);

            File fi = new File(Environment.getExternalStorageDirectory().toString());
            for (File tempe : fi.listFiles()) {
                if (tempe.getName().equals("myData.txt")) {
                    fi = tempe;
                    break;
                }
            }


            Uri Us = Uri.fromFile(fi);
            uris.add(Us);



            ei.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
            startActivityForResult(Intent.createChooser(ei, "Sending multiple attachment"), 12345);

这最终为我工作了。请注意,不包含收集图片的代码,因此如果您想使用此代码,您还必须制作temp.jpg文件。

此代码将编辑文本的内容发送到一个文件中,将图片发送到另一个文件中,然后将此信息发送到电子邮件地址(myemail.edu)。

答案 1 :(得分:-1)

尝试使用绝对路径,就像字符串找出缓存目录

的位置一样

将文件字段添加到类File cacheDir

cacheDir = new File(mContext.getCacheDir() + File.separator + "files");

File txtFile = new File(cacheDir + File.separator + "form.txt");

当您编写文件时执行此操作

FileOutputStream fos = new FileOutputStream(txtFile)

附加意图附加

emailIntent.putExtra(Intent.EXTRA_STREAM,txtFile.getAbsolutePath()); //not sure about that part