Java 类android.os.FileUriExposedException 实例源码

项目:discover-android-n    文件:MainActivity.java   
private void handleDropEvent(View view, DragEvent dragEvent) {
    ClipData clipData = dragEvent.getClipData();
    ClipData.Item item = clipData.getItemAt(0);
    Uri uri = item.getUri();
    if (uri != null) {
        String mimeType = URLConnection.guessContentTypeFromName(new File(uri.getPath()).getName());
        if (mimeType == null) {
            Snackbar.make(mainLayout, "Can't open file. The file type is unknown.", Snackbar.LENGTH_LONG).show();
        } else if (mimeType.startsWith("image")) {
            handleImage(uri);
        } else {
            Intent newIntent = new Intent(Intent.ACTION_VIEW);
            Uri contentUri = FileProvider.getUriForFile(this, BuildConfig.APPLICATION_ID + ".provider", new File(uri.getPath()));

            newIntent.setDataAndType(contentUri, mimeType);
            newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            List<ResolveInfo> resInfoList = getPackageManager().queryIntentActivities(newIntent, PackageManager.MATCH_DEFAULT_ONLY);
            for (ResolveInfo resolveInfo : resInfoList) {
                String packageName = resolveInfo.activityInfo.packageName;
                grantUriPermission(packageName, contentUri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
            }
            try {
                startActivity(newIntent);
            } catch (ActivityNotFoundException e) {
                Snackbar.make(mainLayout, "Sorry, I found no handler for this type of file.", Snackbar.LENGTH_SHORT).show();
            } catch (FileUriExposedException ex) {
                Snackbar.make(mainLayout, "FileUriExposedException", Snackbar.LENGTH_SHORT).show();
            }
        }
    } else if (item.getText() != null) {
        handleText(item.getText());
    } else {
        Log.e(TAG, "Unknown content: " + clipData);
    }
}