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..."));




Tuesday, June 4, 2019

Home



Rating Android application Programmatically 

   
     To enable rating feature in your android application You can use below code.

   gradle dependency.

   dependencies{
          implementation 'com.github.hotchemi:android-rate:1.0.1'
    }


     
@override
  protected void onCreate(@Nullable Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);

  AppRate.with(this).setInstallDays(1)
           .setLaunchTimes(3)
           .setRemindInterval(1)
           .monitor();

  AppRate.showRateDialogIfMeetsConditions(this);




setInstallDays(1): After Number of days(1) of installation it triggers rate dialog.

setLaunchTimes(2): It says, after launching application two times, dialog will appear.

setRemindInterval(1): if you press Remind me later. then next time rating dialog will appear after one day.

AppRate.showRateDialogIfMeetsConditions(this) :  This line calls shows rating dialog by comparing all above mentioned conditions.


Friday, May 3, 2019

add app to Open_with Intent.




List your android application to Open_with Intent.



Edit your AndroidManifest file with blow intent inside activity tag.


<activity

    android:name=".MyActivity"

    android:label="@string/title_activity_main">

    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>

    <intent-filter>
        <action android:name="android.intent.action.VIEW"/>
        <category android:name="android.intent.category.DEFAULT" />
        <data android:mimeType="audio/*" />
    </intent-filter>

</activity>




Then in your activity class use below mentioned code:

public void onCreate(Bundle savedInstanceState) {

      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);

      Intent intent = getIntent();
      String action = intent.getAction();
      String type = intent.getType();

      if(action != null && type != null){
           Uri url = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM);

             // write your code
   }
}