Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete file after sharing via intent

I'm trying to delete a temporary file after sharing it via android's Intent.ACTION_SEND feature. Right now I am starting the activity for a result and in OnActivityResult, I am deleting the file. Unfortunately this only works if I am debugging it with a breakpoint, but when I let it run freely and say, email the file, the email has no attachment.

I think what is happening is my activity is deleting the file before it had been emailed. What I don't get is why, shouldn't onActivityResult only be called AFTER the other activity is finished?

I have also tried deleting the file in onResume, but no luck.

Is there a better way to do this?

like image 604
Matt Avatar asked Jan 05 '11 21:01

Matt


People also ask

How do I delete a file in Filepath?

File deleteFile = new File(s. getFilepath()); boolean delete = deleteFile. delete();

How to use action_ Open_ document?

So, you use ACTION_OPEN_DOCUMENT to request that the user choose a document. You take the Uri that you get and eventually you call openOutputStream() on a ContentResolver , passing in that Uri . You then try to write content out to that stream, to be able to store that content in the user-chosen document.


2 Answers

I noticed the same behavior with a similar approach. While watching logcat for errors I saw gmail complaining that it couldnt find the attachment. So, yes, it seems the intent returns BEFORE gmail has actually read the file for attachment.

I havent gotten around to a solution yet but it's likely going to be something like:

  • move the file to some directory so I know it's one I've decided to send
  • send it as attachment via ACTION_SEND
  • at next onResume for my start screen activity, delete files in the "sent" directory that are older than some time frame that's reasonably long enough for the send to actually have happened

Choosing an appropriate time frame might be tricky since it's probably the case that gmail (or other ACTION_SEND providers) dont actually read the file until it has a network connection. I'm thinking 24 hours should be reasonable and in my case I'm dealing with diagnostic logs so there's no real harm in deleting one too soon if the user has been off network for a long period of time.

If the content of your file is text and it's not obscenely large a simpler approach may be to read the contents of the file and use Intent.putExtra(android.content.Intent.EXTRA_TEXT, yourText) to inline it into the body of the message.

like image 117
mmeyer Avatar answered Sep 25 '22 21:09

mmeyer


What I did is the following.

I used the:

myfile.deleteOnExit();

However, as D.R. mentioned in the comment below correct answer, this does not guarantee the file deletion. This is why I am also deleting the file after the Shared Activity returns. I delete the file if file exists. Because the app crashed sometimes, I put it inside try{} and it works.

I do not know why it does not work for you, but for me it works at least for Gmail attachement, TextSecure, Hangouts.

In class delcaration:

static File file;

In method that calles the Intent:

        Intent share = new Intent(Intent.ACTION_SEND);
        share.setType("image/png");

        // Compress the bitmap to PNG
        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, bytes);

        // Temporarily store the image to Flash
        File sdCard = Environment.getExternalStorageDirectory();
        File dir = new File (sdCard.getAbsolutePath() + "/FolderName");
        dir.mkdirs();

        // This file is static.
        file = new File(dir, "FileName.png");

        try {
            file.createNewFile();
            FileOutputStream fo = new FileOutputStream(file);
            fo.write(bytes.toByteArray());
            fo.flush();
            fo.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        // Share compressed image
        share.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///"+file.getPath()));

        /** START ACTIVITY **/
        startActivityForResult(Intent.createChooser(share,"Share Image"),1);

        // Delete Temporary file
        file.deleteOnExit();     // sometimes works

In an extra method:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {   
        // Because app crashes sometimes without the try->catch 
        try {
            // if file exists in memory
            if (file.exists()) {
                file.delete();
            }
        } catch (Exception e) {
            Log.d(LOG,"Some error happened?");
        }

    }
like image 44
KrNeki Avatar answered Sep 22 '22 21:09

KrNeki