6

I have the following code to share a file via Intent.ACTION_SEND. The last line shows a chooser so that the user can pick an appropriate app. When I chose the email everything is fine and the file is attached to the email. On the other hand, when I pick Google drive the file is uploaded to the google drive but the name of the file is changed to "backup" which is the subject. That is, if I call shareBackup("/sdcard/001.mks") then the file name on the Google drive is "Backup" not "001.mks". Is there any problem with my code?

public void shareBackup(String path) {  
    String to = "YourEmail@somewhere.com";
    String subject = "Backup";
    String message = "Your backup is attached";
    Intent email = new Intent(Intent.ACTION_SEND);
    email.putExtra(Intent.EXTRA_EMAIL, new String[] { to });
    email.putExtra(Intent.EXTRA_SUBJECT, subject);
    email.putExtra(Intent.EXTRA_TEXT, message);
    File f = new File(path);
    email.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(f));       
    email.setType("text/*");
    startActivity(Intent.createChooser(email, "Send"));     
}
saeed khalafinejad
  • 1,139
  • 9
  • 22
  • Could you display how you handle the incoming intent when Google Drive is selected? – gerardnimo Feb 15 '16 at 15:46
  • The last line shows the android chooser. Then the I select Google Drive. After that Google Drive app is displayed and the upload is continued from Google Drive app not mine. At this step a dialog with the title "save to drive" is displayed where I simply select a folder on Google Drive an press the "save" button. The file is uploaded successfully to the drive. When I download the file to my SDCard by using Google Drive app the file name is "Backup" not "001.mks". – saeed khalafinejad Feb 16 '16 at 09:17
  • What I mean is the code on how you handle the intent because the error might be located there. – gerardnimo Feb 17 '16 at 09:15
  • 2
    I've been having the same problem. Google Drive takes the "subject" and uses it as the file name, so if you don't want to put the file name in the subject, you're screwed. – beyondtheteal May 12 '16 at 13:09

1 Answers1

5

I encountered this issue as well, and one workaround I discovered was using the action Intent.ACTION_SEND_MULTIPLE instead of Intent.ACTION_SEND to share the file. In that case the shared file retains its name when shared with google drive (note: I have no understanding of why this issue exists, or if this 'fix' will continue to work as time marches on. When searching for solutions to this issue I only encountered this unanswered SO post, didn't locate any existing bug reports, and didn't take the time to file one myself. So hopefully this post helps some).

Note that when providing the file Uris to the intent you'll have to use Intent.putParcelableArrayListExtra instead of Intent.putExtra, and wrap your single Uri in an ArrayList.

With those changes your above code should look like:

public void shareBackup(String path) {  
    String to = "YourEmail@somewhere.com";
    String subject = "Backup";
    String message = "Your backup is attached";
    Intent email = new Intent(Intent.ACTION_SEND_MULTIPLE);
    email.putExtra(Intent.EXTRA_EMAIL, new String[] { to });
    email.putExtra(Intent.EXTRA_SUBJECT, subject);
    email.putExtra(Intent.EXTRA_TEXT, message);
    File f = new File(path);
    email.putParcelableArrayListExtra(Intent.EXTRA_STREAM, new ArrayList<>(Arrays.asList(Uri.fromFile(f))));       
    email.setType("text/*");
    startActivity(Intent.createChooser(email, "Send"));     
}
jacob.enget
  • 116
  • 2
  • 6