Example usage for android.content Intent FLAG_GRANT_READ_URI_PERMISSION

List of usage examples for android.content Intent FLAG_GRANT_READ_URI_PERMISSION

Introduction

In this page you can find the example usage for android.content Intent FLAG_GRANT_READ_URI_PERMISSION.

Prototype

int FLAG_GRANT_READ_URI_PERMISSION

To view the source code for android.content Intent FLAG_GRANT_READ_URI_PERMISSION.

Click Source Link

Document

If set, the recipient of this Intent will be granted permission to perform read operations on the URI in the Intent's data and any URIs specified in its ClipData.

Usage

From source file:ca.rmen.android.scrumchatter.export.Export.java

/**
 * Bring up the chooser to send the file.
 *///  ww  w.ja va2 s  .c o  m
static void share(Context context, File file, String mimeType) {
    Intent sendIntent = new Intent();
    sendIntent.setAction(Intent.ACTION_SEND);
    sendIntent.putExtra(Intent.EXTRA_SUBJECT, context.getString(R.string.export_message_subject));
    sendIntent.putExtra(Intent.EXTRA_TEXT, context.getString(R.string.export_message_body));
    Uri uri = FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".fileprovider", file);
    sendIntent.putExtra(Intent.EXTRA_STREAM, uri);
    sendIntent.setType(mimeType);
    if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) {
        List<ResolveInfo> resInfoList = context.getPackageManager().queryIntentActivities(sendIntent,
                PackageManager.MATCH_DEFAULT_ONLY);
        for (ResolveInfo resolveInfo : resInfoList) {
            String packageName = resolveInfo.activityInfo.packageName;
            context.grantUriPermission(packageName, uri, Intent.FLAG_GRANT_READ_URI_PERMISSION);
            Log.v(TAG, "grant permission to " + packageName);
        }
    }
    context.startActivity(
            Intent.createChooser(sendIntent, context.getResources().getText(R.string.action_share)));
}

From source file:org.andstatus.app.util.UriUtils.java

/** See http://stackoverflow.com/questions/25999886/android-content-provider-uri-doesnt-work-after-reboot */
@TargetApi(Build.VERSION_CODES.KITKAT)//from   ww w  .  j  a va  2s  .  c om
public static void takePersistableUriPermission(Context context, Uri uri, int takeFlagsIn) {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
        return;
    }
    if ((takeFlagsIn & Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION) != 0) {
        final int takeFlags = takeFlagsIn
                & (Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
        try {
            context.getContentResolver().takePersistableUriPermission(uri, takeFlags);
        } catch (SecurityException e) {
            MyLog.i(context, "Exception while taking persistable URI permission for '" + uri + "'", e);
        }
    } else {
        MyLog.i(context, "No persistable URI permission for '" + uri + "'");
    }
}

From source file:org.jak_linux.dns66.ItemActivity.java

@Override
public void onActivityResult(int requestCode, int resultCode, Intent resultData) {
    if (requestCode == READ_REQUEST_CODE && resultCode == Activity.RESULT_OK) {

        if (resultData != null) {
            Uri uri = resultData.getData();
            try {
                getContentResolver().takePersistableUriPermission(uri, Intent.FLAG_GRANT_READ_URI_PERMISSION);

                locationText.setText(uri.toString());
            } catch (SecurityException e) {
                new AlertDialog.Builder(this).setIcon(R.drawable.ic_warning)
                        .setTitle(R.string.permission_denied)
                        .setMessage(R.string.persistable_uri_permission_failed)
                        .setPositiveButton(android.R.string.yes, null).show();
            }//from w w w .  jav a 2 s  .c o  m
        }
    }
}

From source file:ru.ming13.muzei.earthview.api.WallpaperFiler.java

public Uri getFileUri(@NonNull String wallpaperId, @NonNull Set<String> wallpaperSubscribers) {
    Uri fileUri = FileProvider.getUriForFile(context, context.getString(R.string.authority_files),
            getFile(wallpaperId));/*from   www . j  av a 2  s .  co m*/

    for (String wallpaperSubscriber : wallpaperSubscribers) {
        context.grantUriPermission(wallpaperSubscriber, fileUri, Intent.FLAG_GRANT_READ_URI_PERMISSION);
    }

    return fileUri;
}

From source file:com.pranavpandey.smallapp.permission.PermissionSelectExternalStorage.java

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@Override/*w ww.ja  v  a  2  s .c o m*/
public final void onActivityResult(final int requestCode, final int resultCode, final Intent resultData) {
    if (requestCode == REQUEST_CODE_STORAGE_ACCESS) {
        Uri treeUri = null;
        if (resultCode == Activity.RESULT_OK) {
            // Get Uri from Storage Access Framework.
            treeUri = resultData.getData();

            // Persist URI in shared preference so that you can use it later.
            // Use your own framework here instead of PreferenceUtil.
            SmallUtils.savePrefs(this, PREF_EXTERNAL_STORAGE_URI, treeUri.toString());
            Toast.makeText(this, treeUri.toString(), Toast.LENGTH_LONG).show();

            // Persist access permissions.
            final int takeFlags = resultData.getFlags()
                    & (Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
            getContentResolver().takePersistableUriPermission(treeUri, takeFlags);
        }
    }
    finish();
}

From source file:com.commonsware.android.documents.consumer.DurablizerService.java

private boolean obtainDurablePermission(Uri document) {
    boolean weHaveDurablePermission = false;

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        int perms = Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION;

        try {// w  w  w  . jav a 2 s.co m
            getContentResolver().takePersistableUriPermission(document, perms);

            for (UriPermission perm : getContentResolver().getPersistedUriPermissions()) {
                if (perm.getUri().equals(document)) {
                    weHaveDurablePermission = true;
                }
            }
        } catch (SecurityException e) {
            // OK, we were not offered any persistable permissions
        }
    }

    return (weHaveDurablePermission);
}

From source file:com.commonsware.android.videorecord.MainActivity.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if (savedInstanceState == null) {
        output = new File(new File(getFilesDir(), VIDEOS), FILENAME);

        if (output.exists()) {
            output.delete();//  www  . j a  va2  s. c  om
        } else {
            output.getParentFile().mkdirs();
        }
    } else {
        output = (File) savedInstanceState.getSerializable(EXTRA_FILENAME);
    }

    outputUri = FileProvider.getUriForFile(this, AUTHORITY, output);

    if (savedInstanceState == null) {
        Intent i = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);

        i.putExtra(MediaStore.EXTRA_OUTPUT, outputUri);
        i.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            i.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
        } else {
            List<ResolveInfo> resInfoList = getPackageManager().queryIntentActivities(i,
                    PackageManager.MATCH_DEFAULT_ONLY);

            for (ResolveInfo resolveInfo : resInfoList) {
                String packageName = resolveInfo.activityInfo.packageName;

                grantUriPermission(packageName, outputUri,
                        Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
            }
        }

        startActivityForResult(i, REQUEST_ID);
    }
}

From source file:net.nym.utilslibrary.utils.IntentUtils.java

public static void openFile(Context context, File file, String authority) {
    try {/*from ww  w  .  j  a va 2s .com*/

        Intent intent = new Intent(Intent.ACTION_VIEW);
        Uri data;
        // 7.0
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            // "cn.com.firstcare.mobile.aph.fileprovider"???authorities
            data = FileProvider.getUriForFile(context, authority, file);
            // ?
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        } else {
            data = Uri.fromFile(file);
        }
        intent.setDataAndType(data, FileUtils.getMimeType(file));
        Logger.e("getMimeType=%s", FileUtils.getMimeType(file));
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    } catch (Exception e) {
        e.printStackTrace();
        Toaster.toaster(context, "??app");
    }
}

From source file:info.schnatterer.logbackandroiddemo.OpenLogActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    /* Get URIs for log files using android.support.v4.content.FileProvider */
    final Intent openFile = new Intent(Intent.ACTION_VIEW);
    Uri uriForFile = FileProvider.getUriForFile(this, getString(R.string.authority_log_file_provider),
            Logs.findNewestLogFile(this));
    openFile.setDataAndType(uriForFile, "text/plain");
    openFile.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    startActivity(openFile);//ww  w .  j  a v  a2 s .c  om

    finish();
}

From source file:org.catrobat.paintroid.WelcomeActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    session = new Session(this);
    if (!session.isFirstTimeLaunch() && getIntent().getFlags() != Intent.FLAG_GRANT_READ_URI_PERMISSION) {
        launchHomeScreen();//w  ww.  ja v a  2 s.  c o  m
    }
    getIntent().setFlags(0);

    getStyleAttributesFromXml();

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        getWindow().getDecorView().setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
    }

    setContentView(R.layout.activity_welcome);

    viewPager = (ViewPager) findViewById(R.id.view_pager);
    dotsLayout = (LinearLayout) findViewById(R.id.layoutDots);
    btnSkip = (Button) findViewById(R.id.btn_skip);
    btnNext = (Button) findViewById(R.id.btn_next);

    colorActive = ContextCompat.getColor(getApplicationContext(), R.color.dot_active);
    colorInactive = ContextCompat.getColor(getApplicationContext(), R.color.dot_inactive);

    layouts = new int[] { R.layout.islide_welcome, R.layout.islide_tools, R.layout.islide_possibilities,
            R.layout.islide_landscape, R.layout.islide_getstarted };

    changeStatusBarColor();
    initViewPager();

    if (isRTL(this)) {
        addBottomDots(layouts.length - 1);
    } else {
        addBottomDots(0);
    }

    btnSkip.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            launchHomeScreen();
        }
    });

    btnNext.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            boolean finished;
            int current = getItem(+1);

            finished = current > layouts.length - 1;

            if (isRTL(WelcomeActivity.this)) {
                current = getItem(-1);
                finished = current < 0;
            }

            if (finished) {
                launchHomeScreen();
            } else {
                viewPager.setCurrentItem(current);
            }
        }
    });

}