android.os.FileUriExposedException Java Examples

The following examples show how to use android.os.FileUriExposedException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ArticleActionsHelper.java    From android-app with GNU General Public License v3.0 4 votes vote down vote up
public void openUrl(Context context, String url) {
    Log.d(TAG, "openUrl() url: " + url);
    if (TextUtils.isEmpty(url)) return;

    Uri uri = Uri.parse(url);
    if (uri.getScheme() == null) {
        Log.i(TAG, "openUrl() scheme is null, appending default scheme");
        uri = Uri.parse("http://" + url);
    }
    Log.d(TAG, "openUrl() uri: " + uri);

    Intent intent = new Intent(Intent.ACTION_VIEW, uri);

    boolean errorMessage = false;

    if (intent.resolveActivity(context.getPackageManager()) != null) {
        try {
            context.startActivity(intent);
        } catch (RuntimeException e) {
            boolean rethrow = true;

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                if (e instanceof FileUriExposedException) {
                    // apparently happens when user clicks on some file URI in an article
                    Log.w(TAG, "openUrl()", e);
                    errorMessage = true;
                    rethrow = false;
                }
            }

            if (rethrow) throw e;
        }
    } else {
        Log.w(TAG, "openUrl() no activity to handle intent");
        errorMessage = true;
    }

    if (errorMessage) {
        Toast.makeText(context, R.string.message_couldNotOpenUrl, Toast.LENGTH_SHORT).show();
    }
}