本文整理汇总了Java中android.support.v4.content.FileProvider类的典型用法代码示例。如果您正苦于以下问题:Java FileProvider类的具体用法?Java FileProvider怎么用?Java FileProvider使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
FileProvider类属于android.support.v4.content包,在下文中一共展示了FileProvider类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: installApk
import android.support.v4.content.FileProvider; //导入依赖的package包/类
private void installApk() {
File[] files = mRxDownload.getRealFiles(mData.downloadUrl);
if (files != null) {
Uri uri = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
uri = FileProvider.getUriForFile(mContext, mContext.getApplicationInfo().packageName + ".provider", files[0]);
} else {
uri = Uri.fromFile(files[0]);
}
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setDataAndType(uri, "application/vnd.android.package-archive");
mContext.startActivity(intent);
} else {
Toast.makeText(mContext, "File not exists", Toast.LENGTH_SHORT).show();
}
}
示例2: onContextItemSelected
import android.support.v4.content.FileProvider; //导入依赖的package包/类
@Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case TAKE_PHOTO:
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File f = new File(Environment.getExternalStorageDirectory() + TMP_METACOM_JPG);
Uri uri = FileProvider.getUriForFile(getContext(), AUTHORITY_STRING, f);
takePictureIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, uri);
takePictureIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
if (takePictureIntent.resolveActivity(getActivity().getPackageManager()) != null) {
startActivityForResult(takePictureIntent, PICK_IMAGE_FROM_CAMERA);
}
return true;
case FILE_EXPLORER:
Intent intent = new Intent();
intent.setType("*/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, getString(R.string
.select_file)),
PICK_IMAGE_FROM_EXPLORER);
return true;
default:
return super.onContextItemSelected(item);
}
}
示例3: install
import android.support.v4.content.FileProvider; //导入依赖的package包/类
/**
* 通过隐式意图调用系统安装程序安装APK
*/
public static void install(Context context) {
File file = new File(
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
, apkname);
Intent intent = new Intent(Intent.ACTION_VIEW);
// 由于没有在Activity环境下启动Activity,设置下面的标签
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
if (Build.VERSION.SDK_INT >= 24) { //判读版本是否在7.0以上
//参数1 上下文, 参数2 Provider主机地址 和配置文件中保持一致 参数3 共享的文件
Uri apkUri =
FileProvider.getUriForFile(context, "com.hzecool.slhStaff.fileprovider", file);
//添加这一句表示对目标应用临时授权该Uri所代表的文件
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setDataAndType(apkUri, "application/vnd.android.package-archive");
} else {
intent.setDataAndType(Uri.fromFile(file),
"application/vnd.android.package-archive");
}
context.startActivity(intent);
}
示例4: takeAPhoto
import android.support.v4.content.FileProvider; //导入依赖的package包/类
@NeedsPermission({Manifest.permission.CAMERA})
void takeAPhoto() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(this.getPackageManager()) != null) {
Observable.just(this)
.observeOn(Schedulers.newThread())
.map((ctx) -> presenter.createImageFile(this))
.subscribe(photoFile -> {
Timber.d(Thread.currentThread().getName());
if (photoFile != null) {
Uri photoURI = FileProvider.getUriForFile(this, BuildConfig.APPLICATION_ID
+ ".provider", presenter.createImageFile(this));
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(takePictureIntent, TAKE_PHOTO);
}
}, t -> onLocationError());
}
}
示例5: openFile
import android.support.v4.content.FileProvider; //导入依赖的package包/类
private 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 {
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();
}
}
示例6: do_add_video
import android.support.v4.content.FileProvider; //导入依赖的package包/类
@OnClick(R.id.fab_add_video)
public void do_add_video()
{
Intent capture = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
if (capture.resolveActivity(getPackageManager()) != null) {
try {
File videoFile = createFileName("traxyvid", ".mp4");
mediaUri = FileProvider.getUriForFile(this,
getPackageName() + ".provider", videoFile);
capture.putExtra(MediaStore.EXTRA_OUTPUT, mediaUri);
startActivityForResult(capture, CAPTURE_VIDEO_REQUEST);
} catch (IOException e) {
e.printStackTrace();
}
}
}
示例7: takePicture
import android.support.v4.content.FileProvider; //导入依赖的package包/类
public void takePicture() {
try {
mPhotoPath = sdcardPath + "/icon.jpg";
mPhotoFile = new File(mPhotoPath);
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
if (Build.VERSION.SDK_INT >= 23) {
Uri uri = FileProvider.getUriForFile(this, getApplicationContext().getPackageName() + ".provider", mPhotoFile);
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
} else {
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(mPhotoFile));
}
startActivityForResult(intent, 1);
} catch (Exception e) {
}
}
示例8: installApk
import android.support.v4.content.FileProvider; //导入依赖的package包/类
private void installApk() {
File[] files = mRxDownload.getRealFiles(data.record.getUrl());
if (files != null) {
Uri uri = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
uri = FileProvider.getUriForFile(mContext, mContext.getApplicationInfo().packageName + ".provider", files[0]);
} else {
uri = Uri.fromFile(files[0]);
}
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setDataAndType(uri, "application/vnd.android.package-archive");
mContext.startActivity(intent);
} else {
Toast.makeText(mContext, "File not exists", Toast.LENGTH_SHORT).show();
}
}
示例9: installApk
import android.support.v4.content.FileProvider; //导入依赖的package包/类
private void installApk() {
File[] files = mRxDownload.getRealFiles(url);
if (files != null) {
Uri uri = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
uri = FileProvider.getUriForFile(this, getApplicationInfo().packageName + ".provider", files[0]);
} else {
uri = Uri.fromFile(files[0]);
}
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setDataAndType(uri, "application/vnd.android.package-archive");
startActivity(intent);
} else {
Toast.makeText(this, "File not exists", Toast.LENGTH_SHORT).show();
}
}
示例10: newCrop
import android.support.v4.content.FileProvider; //导入依赖的package包/类
public void newCrop() {
File file = new File(Environment.getExternalStorageDirectory(),
"/temp/" + System.currentTimeMillis() + ".jpg");
if (!file.getParentFile().exists()) file.getParentFile().mkdirs();
//输出路径只能用Uri.fromFile,不然报错
//outputUri = FileProvider.getUriForFile(this, "com.liuguoquan.module.ui.provider",file);
outputUri = Uri.fromFile(file);
Uri imageUri = FileProvider.getUriForFile(getActivity(), "com.michael.materialdesign.provider",
new File("/storage/emulated/0/temp/1476865100115.jpg"));//通过FileProvider创建一个content类型的Uri
//grantUriPermission(getPackageName(),imageUri,Intent.FLAG_GRANT_READ_URI_PERMISSION);
Intent intent = new Intent("com.android.camera.action.CROP");
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setDataAndType(mImageUri, "image/*");
intent.putExtra("crop", "true");
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("scale", true);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputUri);
intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
intent.putExtra("noFaceDetection", true); // no face detection
startActivityForResult(intent, 1008);
}
示例11: shareImage
import android.support.v4.content.FileProvider; //导入依赖的package包/类
/**
* Helper method for sharing an image.
*
* @param context The image context.
* @param imagePath The path of the image to be shared.
*/
static void shareImage(Context context, String imagePath) {
// Create the share intent and start the share activity
File imageFile = new File(imagePath);
Intent shareIntent = new Intent(Intent.ACTION_SEND);
//type determines the type of application an intent would choose
shareIntent.setType("image/*");
//To share a file with another app using a content URI, your app has to generate
// the content URI. To generate the content URI, create a new File for the file,
// then pass the File to getUriForFile(). You can send the content URI returned by
// getUriForFile() to another app in an Intent. The client app that receives the content
// URI can open the file and access its contents by calling
// ContentResolver.openFileDescriptor to get a ParcelFileDescriptor.
Uri photoURI = FileProvider.getUriForFile(context, FILE_PROVIDER_AUTHORITY, imageFile);
shareIntent.putExtra(Intent.EXTRA_STREAM, photoURI);
context.startActivity(shareIntent);
}
示例12: installUpdate
import android.support.v4.content.FileProvider; //导入依赖的package包/类
/**Installs the update by invoking the default packet installer*/
private void installUpdate(File downloadPath) {
File apk = new File(downloadPath, ChromiumUpdater.CHROMIUM_SWE_APK);
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri uri;
if(android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
uri = FileProvider.getUriForFile(context, context.getApplicationContext()
.getPackageName() + ".provider", apk);
context.grantUriPermission("com.android.packageinstaller", uri,
Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
context.grantUriPermission("com.google.android.packageinstaller", uri,
Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
} else {
uri = Uri.fromFile(apk);
}
intent.setDataAndType(uri, "application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
示例13: dispatchTakePictureIntentTest4
import android.support.v4.content.FileProvider; //导入依赖的package包/类
private void dispatchTakePictureIntentTest4() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
// Create the File where the photo should go
File photoFile = null;
try {
photoFile = createImageFile(false);
} catch (IOException ex) {
// Error occurred while creating the File
ex.printStackTrace();
}
// Continue only if the File was successfully created
if (photoFile != null) {
Uri photoURI = FileProvider.getUriForFile(this,
"com.ge.droid.takingphotossimply.fileprovider",
photoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(takePictureIntent, REQUEST_TAKE_PICTURE_TEST_4);
}
}
}
示例14: takePicture
import android.support.v4.content.FileProvider; //导入依赖的package包/类
public void takePicture() {
try {
mPhotoPath = sdcardPath + "/icon.png";
mPhotoFile = new File(mPhotoPath);
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
if (Build.VERSION.SDK_INT >= 23) {
Uri uri = FileProvider.getUriForFile(this, getApplicationContext().getPackageName() + ".provider", mPhotoFile);
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
} else {
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(mPhotoFile));
}
startActivityForResult(intent, 1);
} catch (Exception e) {
}
}
示例15: installApk
import android.support.v4.content.FileProvider; //导入依赖的package包/类
private void installApk(Context context, String apkPath) {
if (context == null || TextUtils.isEmpty(apkPath)) {
return;
}
File file = new File(apkPath);
Intent intent = new Intent(Intent.ACTION_VIEW);
//判读版本是否在7.0以上
if (Build.VERSION.SDK_INT >= 24) {
Uri apkUri = FileProvider.getUriForFile(context, context.getPackageName() + ".AgentWebFileProvider", file);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setDataAndType(apkUri, "application/vnd.android.package-archive");
} else {
intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
}
context.startActivity(intent);
}