Example usage for android.support.v4.content FileProvider getUriForFile

List of usage examples for android.support.v4.content FileProvider getUriForFile

Introduction

In this page you can find the example usage for android.support.v4.content FileProvider getUriForFile.

Prototype

public static Uri getUriForFile(Context context, String str, File file) 

Source Link

Usage

From source file:com.android.contacts.util.ContactPhotoUtils.java

public static Uri generateTempCroppedImageUri(Context context) {
    final String fileProviderAuthority = context.getResources()
            .getString(R.string.photo_file_provider_authority);
    return FileProvider.getUriForFile(context, fileProviderAuthority,
            new File(pathForTempPhoto(context, generateTempCroppedPhotoFileName())));
}

From source file:com.commonsware.android.cp.v4file.FilesCPDemo.java

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

    File f = new File(getFilesDir(), "test.pdf");

    if (!f.exists()) {
        AssetManager assets = getResources().getAssets();

        try {//from   w w  w.  j a v a2 s  .  c o m
            copy(assets.open("test.pdf"), f);
        } catch (IOException e) {
            Log.e("FileProvider", "Exception copying from assets", e);
        }
    }

    Intent i = new Intent(Intent.ACTION_VIEW, FileProvider.getUriForFile(this, AUTHORITY, f));

    i.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    startActivity(i);
    finish();
}

From source file:com.artwl.update.StorageUtils.java

public static Uri getFileUri(Context context, File apkFile) {
    if (Build.VERSION.SDK_INT >= 24) {
        return FileProvider.getUriForFile(context, context.getPackageName(), apkFile);
    } else {//from  w  w  w.  j av  a 2 s.  co m
        return Uri.fromFile(apkFile);
    }
}

From source file:com.example.android.supportv4.content.FileProviderExample.java

public void onShareFileClick(View view) {
    // Save a thumbnail to file
    final File thumbsDir = new File(getFilesDir(), "thumbs");
    thumbsDir.mkdirs();/*from  w w w  .  jav  a  2  s .  c  o m*/
    final File file = new File(thumbsDir, "private.png");
    saveThumbnail(view, file);

    // Now share that private file using FileProvider
    final Uri uri = FileProvider.getUriForFile(this, AUTHORITY, file);
    final Intent intent = new Intent(Intent.ACTION_SEND);
    intent.setType("image/png");
    intent.putExtra(Intent.EXTRA_STREAM, uri);
    startActivity(intent);
}

From source file:com.android.contacts.common.vcard.ShareVCardActivity.java

@Override
public synchronized void onServiceConnected(ComponentName name, IBinder binder) {
    if (DEBUG)/* www .jav a2  s.  c  om*/
        Log.d(LOG_TAG, "connected to service, requesting a destination file name");
    mConnected = true;
    mService = ((VCardService.MyBinder) binder).getService();

    clearExportFiles();

    final File file = getLocalFile();
    try {
        file.createNewFile();
    } catch (IOException e) {
        Log.e(LOG_TAG, "Failed to create .vcf file, because: " + e);
        unbindAndFinish();
        return;
    }

    final Uri contentUri = FileProvider.getUriForFile(this,
            getString(R.string.contacts_file_provider_authority), file);
    if (DEBUG)
        Log.d(LOG_TAG, "exporting to " + contentUri);

    final ExportRequest request = new ExportRequest(contentUri);
    // The connection object will call finish().
    mService.handleExportRequest(request, new NotificationImportExportListener(ShareVCardActivity.this));
    unbindAndFinish();
}

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();// w  ww.  ja va 2  s  .  c  o  m
        } 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:de.baumann.quitsmoking.helper.helper_main.java

public static void openFile(Activity activity, File file, String string, View view) {

    Intent intent = new Intent();
    intent.setAction(Intent.ACTION_VIEW);
    intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        Uri contentUri = FileProvider.getUriForFile(activity,
                activity.getApplicationContext().getPackageName() + ".provider", file);
        intent.setDataAndType(contentUri, string);

    } else {/*from ww  w.j  a v a  2 s  .  c o  m*/
        intent.setDataAndType(Uri.fromFile(file), string);
    }

    try {
        activity.startActivity(intent);
    } catch (ActivityNotFoundException e) {
        Snackbar.make(view, R.string.toast_install_app, Snackbar.LENGTH_LONG).show();
    }
}

From source file:com.android.contacts.vcard.ShareVCardActivity.java

@Override
public synchronized void onServiceConnected(ComponentName name, IBinder binder) {
    if (DEBUG)/*from   w  w  w. ja va 2  s  .c  o  m*/
        Log.d(LOG_TAG, "connected to service, requesting a destination file name");
    mConnected = true;
    mService = ((VCardService.MyBinder) binder).getService();

    clearExportFiles();

    final File file = getLocalFile();
    try {
        file.createNewFile();
    } catch (IOException e) {
        FeedbackHelper.sendFeedback(this, LOG_TAG, "Failed to create .vcf file", e);
        finish();
        return;
    }

    final Uri contentUri = FileProvider.getUriForFile(this,
            getString(R.string.contacts_file_provider_authority), file);
    if (DEBUG)
        Log.d(LOG_TAG, "exporting to " + contentUri);

    final ExportRequest request = new ExportRequest(contentUri);
    // The connection object will call finish().
    mService.handleExportRequest(request, new NotificationImportExportListener(ShareVCardActivity.this));
    finish();
}

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 w  ww  .j  av  a 2  s . c  o m

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

    return fileUri;
}

From source file:io.plaidapp.ui.ShareDribbbleImageTask.java

@Override
protected void onPostExecute(File result) {
    if (result == null) {
        return;//from  w  ww  .j a  v  a  2s .  c o  m
    }
    // glide cache uses an unfriendly & extension-less name,
    // massage it based on the original
    String fileName = shot.images.best();
    fileName = fileName.substring(fileName.lastIndexOf('/') + 1);
    File renamed = new File(result.getParent(), fileName);
    result.renameTo(renamed);
    Uri uri = FileProvider.getUriForFile(activity, activity.getString(R.string.share_authority), renamed);
    ShareCompat.IntentBuilder.from(activity).setText(getShareText()).setType(getImageMimeType(fileName))
            .setSubject(shot.title).setStream(uri).startChooser();
}