Meryem Uysal
Meryem Uysal

Reputation: 191

Android sharing intent error if app is not exist

I am trying share image with Facebook,Twitter,Google+,Pinterest. I used following codes:

Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_TEXT, "Some Text");
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(android.os.Environment.getExternalStorageDirectory(),"Share/share.jpg")));
intent.setType("image/jpeg");
intent.setPackage("com.twitter.android");
startActivity(intent);

It works. But if twitter app is not exist my phone, it gives error. How can i share image and text with browser when i get error?

Upvotes: 0

Views: 521

Answers (1)

jvlach
jvlach

Reputation: 565

You could check if twitter (or any app) is installed on the phone:

boolean installed = appInstalledOrNot("com.twitter.android");  
//check if app is installed
private boolean appInstalledOrNot(String uri)
{
    PackageManager pm = this.getContext().getPackageManager();
    boolean app_installed = false;
    try
    {
           pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES);
           app_installed = true;
    }
    catch (PackageManager.NameNotFoundException e)
    {
           app_installed = false;
    }
    return app_installed ;
}

Upvotes: 1

Related Questions