How to use support FileProvider for sharing content to other apps?

前端 未结 9 2109
野的像风
野的像风 2020-11-22 14:01

I\'m looking for a way to correctly share (not OPEN) an internal file with external application using Android Support library\'s FileProvider.

Following the example

9条回答
  •  有刺的猬
    2020-11-22 14:39

    In my app FileProvider works just fine, and I am able to attach internal files stored in files directory to email clients like Gmail,Yahoo etc.

    In my manifest as mentioned in the Android documentation I placed:

    
            
        
    

    And as my files were stored in the root files directory, the filepaths.xml were as follows:

     
    
    

    Now in the code:

     File file=new File(context.getFilesDir(),"test.txt");
    
     Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND_MULTIPLE);
    
     shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,
                                         "Test");
    
     shareIntent.setType("text/plain");
    
     shareIntent.putExtra(android.content.Intent.EXTRA_EMAIL,
                                     new String[] {"email-address you want to send the file to"});
    
       Uri uri = FileProvider.getUriForFile(context,"com.package.name.fileprovider",
                                                       file);
    
                    ArrayList uris = new ArrayList();
                    uris.add(uri);
    
                    shareIntent .putParcelableArrayListExtra(Intent.EXTRA_STREAM,
                                                            uris);
    
    
                    try {
                       context.startActivity(Intent.createChooser(shareIntent , "Email:").addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));                                                      
    
    
                    }
                    catch(ActivityNotFoundException e) {
                        Toast.makeText(context,
                                       "Sorry No email Application was found",
                                       Toast.LENGTH_SHORT).show();
                    }
                }
    

    This worked for me.Hope this helps :)

提交回复
热议问题