Biginner
Biginner

Reputation: 253

How to delete file from SD Card after Mail send Successfully?

I want to delete File from SD Card After mail sent successfully to Receiver.How to do this?I found a lot of here on SO as well as on Google.I tried as well.my code is as:

if(myFile.exists())
    myFile.delete();

with the above code i delete the file which is stored in SD Card before sending it to on Receiver side.Please someone help me for my this issue.Thanks in Advance.

Upvotes: 0

Views: 1406

Answers (3)

Akanksha Rathore
Akanksha Rathore

Reputation: 3623

when you open e mailbox according to activity life cycle your current Activity move on onPause() when you return in your activity then On Resume method will call so write the blow code on your on Resume method.

protected void onResume() {

    File file= new File(filepath);
    if(file.exists())
    {
         file.delete();
    }
    super.onResume();
}

here filepath is path of external storage or where you saved your file, which you wants to delete.

Upvotes: 0

RobinHood
RobinHood

Reputation: 10969

  file.deleteOnExit();

It will delete your file when activity will be close. check this

Or

Used Alarmmanager and set one time to delete your file after appropriate time, lets say after one hour or half an hour.

Upvotes: 0

Niranj Patel
Niranj Patel

Reputation: 33248

You receive Mail sending status on onActivityResult, so start intent with startActivityForResult..

here is sample code..

Send Mail:

int EMAIL = 101;

Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("text/html");

emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,new String[]{});
emailIntent.putExtra(android.content.Intent.EXTRA_BCC,new String[]{});
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
emailIntent.putExtra(android.content.Intent.EXTRA_STREAM, pngUri);
startActivityForResult(emailIntent,EMAIL);

Sending Result:

protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        // TODO Auto-generated method stub
        if(requestCode==EMAIL)
        {
            if(requestCode==EMAIL && resultCode==Activity.RESULT_OK)    
            {
                            if(myFile.exists())
                                myFile.delete();  
                Toast.makeText(mActivity, "Mail sent.", Toast.LENGTH_SHORT).show();
            }
            else if (requestCode==EMAIL && resultCode==Activity.RESULT_CANCELED)
            {
                Toast.makeText(mActivity, "Mail canceled.", Toast.LENGTH_SHORT).show();
            }
            else 
            {
                Toast.makeText(mActivity, "Please try again.", Toast.LENGTH_SHORT).show();
            }
        }   
    }

Upvotes: 1

Related Questions