下面列出了怎么用android.os.FileUriExposedException的API类实例代码及写法,或者点击链接到github查看源代码。
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();
}
}