【问题标题】:Finding Files Stored By My Own App查找我自己的应用程序存储的文件
【发布时间】:2014-11-12 00:46:43
【问题描述】:

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

这是电子邮件按钮代码

    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();
                                          }
                                 }
                        });

【问题讨论】:

  • 您必须将文件放在(共享)外部存储上,使其他应用程序可以读取内部存储文件,或者使用内容提供程序。即使您将通常私有的内部存储文件设为世界可读,电子邮件客户端也可能假设如果不实际尝试就无法读取它,因此不鼓励这种方法。
  • 有谁知道为什么这被否决了?我浏览了所有的说明。我错过了什么?
  • 我可以查看@ChrisStratton 的哪些内容提供商名称或共享外部存储形式?

标签: java android email-attachments


【解决方案1】:
    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)。

【讨论】:

    【解决方案2】:

    不要尝试使用绝对路径,就像字符串找出缓存目录的位置一样

    在你的类 File cacheDir 中添加一个 File 字段

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-12-19
      • 1970-01-01
      • 1970-01-01
      • 2010-09-06
      • 2015-05-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多