Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google drive changes file name to Intent.EXTRA_SUBJECT when sharing a file via Intent.ACTION_SEND

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 = "[email protected]";
    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"));     
}
like image 604
saeed khalafinejad Avatar asked Feb 13 '16 16:02

saeed khalafinejad


1 Answers

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 = "[email protected]";
    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"));     
}
like image 97
jacob.enget Avatar answered Nov 15 '22 05:11

jacob.enget