通过Android应用程序邮寄多个文件的问题

时间:2014-11-13 18:12:52

标签: android email android-intent email-attachments android-sdcard

我制作了一个Android应用程序,并帮助我调试我放入一个小程序,将会话日志写入文件,然后将该文件存储在SD卡中。我想更进一步,并实现备份和删除如此创建的文件。我实现了删除部分,但我似乎无法弄清楚如何通过电子邮件将这些文件发送给自己。我在这里看了其他类似的问题,这是我现在的代码,

    private void BackupAndClearDirectory(){
    File f = new File(Environment.getExternalStorageDirectory()+"/Music/");
    if(f.isDirectory() && f.exists()) Log.i("MyActivity", f.getAbsolutePath()+" exists");
    File [] files = f.listFiles();
    if(files == null){
        Log.i("MyActivity",f.getAbsolutePath()+" doesn't contain files");
        return;
    }
   // else Log.i("MyActivity","Directory doesn't exist");
    final int MAXFILEAGE = 7200000; //Max file age is 2 hrs

    Intent emailIntent = new Intent(Intent.ACTION_SEND_MULTIPLE);
    emailIntent.setData(Uri.parse("mailto:"));
    emailIntent.setType("text/plain");
    emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Test Mail");

    String[] fileList;
    fileList = new String[1000];
    fileList = f.list();
    if(fileList == null || fileList.length == 0) Log.i("MyActivity","Empty filelist");
      /*for(File fl : files){
        long lastModified = fl.lastModified();
        if(lastModified + MAXFILEAGE<System.currentTimeMillis())
        {
            fileList.add(Environment.getExternalStorageDirectory()+"/Download/"+fl.getName());
            //Log.i("MyActivity","Deleting file "+fl);
            //files[i].delete();
        }
    }*/

   // fileList.add(files[0].toString());
   // fileList.add(files[1].toString());

    Log.i("MyActivity","List of files created");

    ArrayList<Uri> uris = new ArrayList<Uri>();
   //convert from paths to Android friendly Parcelable Uri's

    for (int i=0;i<fileList.length;i++)
    {
        File fileIn = new File(fileList[i]);
        Uri u = Uri.fromFile(fileIn);
        uris.add(u);
    }

    Log.i("MyActivity","List of uris created");

    emailIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
    con.startActivity(emailIntent);
 }

每当我尝试运行它时,我的应用程序崩溃,我得到一个NullPointerException。谁能告诉我我做错了什么。任何帮助将不胜感激。

Logcat输出:

11-14 16:21:49.823 19014-19014 / com.example.abhishek.deletiontesterapp I / MyActivity:/ storage / emulated / 0 / Music exists

11-14 16:21:49.823 19014-19014 / com.example.abhishek.deletiontesterapp I / MyActivity:/ storage / emulated / 0 / Music不包含文件

当我对文件路径进行硬编码时,它可以正常工作,这里是带有硬编码路径的代码:

    private void BackupAndClearDirectory(){
    File f = new File(Environment.getExternalStorageDirectory()+"/Music/");
    /*if(f.isDirectory() && f.exists()) Log.i("MyActivity", f.getAbsolutePath()+" exists");
    File [] files = f.listFiles();
    if(files == null){
        Log.i("MyActivity",f.getAbsolutePath()+" doesn't contain files");
        return;
    }
   // else Log.i("MyActivity","Directory doesn't exist");
    final int MAXFILEAGE = 7200000; //Max file age is 2 hrs
   */
    Intent emailIntent = new Intent(Intent.ACTION_SEND_MULTIPLE);
    emailIntent.setData(Uri.parse("mailto:"));
    emailIntent.setType("text/plain");
    emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Test Mail");

    ArrayList<String> fileList = new ArrayList<String>();
    //if(fileList == null || fileList.length == 0) Log.i("MyActivity","Empty filelist");
      /*for(File fl : files){
        long lastModified = fl.lastModified();
        if(lastModified + MAXFILEAGE<System.currentTimeMillis())
        {
            fileList.add(Environment.getExternalStorageDirectory()+"/Download/"+fl.getName());
            //Log.i("MyActivity","Deleting file "+fl);
            //files[i].delete();
        }
    }*/

   fileList.add(Environment.getExternalStorageDirectory()+"/Music/1.txt");
   fileList.add(Environment.getExternalStorageDirectory()+"/Music/2.txt");
   // fileList.add(files[1].toString());

    Log.i("MyActivity","List of files created");

    ArrayList<Uri> uris = new ArrayList<Uri>();
    //convert from paths to Android friendly Parcelable Uri's

    for (int i=0;i<fileList.size();i++)
    {
        File fileIn = new File(fileList.get(i));
        Uri u = Uri.fromFile(fileIn);
        uris.add(u);
    }

    Log.i("MyActivity","List of uris created");

    emailIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
    con.startActivity(emailIntent);
 }

0 个答案:

没有答案