微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

发送后删除文件android studio

如何解决发送后删除文件android studio

文件发送正常,但如何在发送后删除它,deleteOnExit() 不t work. Maybe I should save it in cache but when I我试图这样做(使用 getCacheDir())文件不发送 任何帮助将不胜感激

                String  fileName_Kon;
             fileName_Kon = ((EditText) popupWindow.getContentView().findViewById(R.id.userInputNameForFile)).getText().toString() + ((TextView) popupWindow.getContentView().findViewById(R.id.dateNameFile)).getText().toString();

                   File sharedir = new File(getFilesDir(),"files");
                if (!sharedir.exists()) sharedir.mkdir();
                String shareName = new String(fileName_Kon);


               try {
                   File shareFile =   File.createTempFile(shareName,".txt",sharedir);
               } catch (IOException e) {
                   e.printstacktrace();
               }
//shareFile.deleteOnExit();
               try {
                    FileOutputStream flogs = new FileOutputStream(shareFile);
                   
                        flogs.write("text").getBytes());
                    
                    flogs.flush();
                    flogs.close();
                } catch (IOException e) {
                    e.printstacktrace();
                }
                final Intent emailIntent = new Intent(Intent.ACTION_SEND_MULTIPLE);
                emailIntent.setType("message/rfc822");
          
              emailIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                Uri contentUri = FileProvider.getUriForFile(MainActivity.this,AUTHORITY,shareFile);
                ArrayList uris = new ArrayList<Uri>();
                uris.add(contentUri);
                emailIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM,uris);
                MainActivity.this.startActivity(Intent.createChooser(emailIntent,"Send..."));

解决方法

等待您发送文件的应用程序活动完成,然后删除文件。

  1. 注册请求代码(作为类的字段):
private static final int REQUEST_CODE = 10;
  1. 将文件保存在应用私有文件夹中:
try {
  File shareFile = new File(sharedir,shareName + ".txt");
  shareFile.createNewFile();
} catch (IOException e) {
  e.printStackTrace();
}

// write to file,as you do in the snippet
  1. 启动 Activity,并在用户从所选应用返回时通知结果:
MainActivity.this.startActivityForResult(
  Intent.createChooser(emailIntent,"Send..."),REQUEST_CODE
);
  1. 对结果执行操作:
@Override
protected void onActivityResult(int requestCode,int resultCode,Intent data) {
  if (requestCode == REQUEST_CODE) {
     // delete file here
     File shareFile = new File(sharedir,shareName + ".txt");
     if (shareFile.exists()) {
        shareFile.delete();
     }
  }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?