Monday, September 21, 2020

 





                Here are the basic queries & their solutions related to android programming.  There are sample methods & related dependency configurations too.


                In this blog I have shared the possible scenarios, which can really help you to improve your android applications along with configurations & dependencies, which are generally missing in tutorials. This blog is helpful for beginners. As these are the issues which I faced during development & found hard to get exact solutions. 

               

    Sharing File with an Intent in android using fileProvider 

   
     if you want to share file using intent in android or getting  android.os.FileUriExposedException

    if you are using targetSdkVersio >= 24 then you will have to use FileProvider. normally intent.send will not work. 

   Add a FileProvier <provider> tag in 
AndroidManifest.xml  under <application> tag.

   <?xml version="1.0" encoding="utf-8"?>
   <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    ....
    <application
        .....
        <provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="${applicationId}.provider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/provider_paths"/>
       </provider>
    </application>
  </manifest

 
Second step is to create provider_paths.xml file in res/xmlfolder.
And paste below content in the file.     

 <?xml version="1.0" encoding="utf-8"?>
 <paths>
    <external-path name="external_files" path="."/>
</paths>


Third Step is to write code in you java file,where you are calling method to share file.

   

     File file = new File(context.getExternalFilesDir("")+"/"+filename);

     Uri uri =FileProvider.getUriForFile(context,contextgetApplicationContext()
                                                .getPackageName()".provider", file);

     Intent shareIntent = new Intent();

     shareIntent.setAction(Intent.ACTION_SEND);

     shareIntent.putExtra(Intent.EXTRA_STREAM, uri);

     shareIntent.setType("text/*");

     shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

     startActivity(Intent.createChooser(shareIntent,"Share With..."));