Java Code Examples for android.support.v4.content.FileProvider#getUriForFile()
The following examples show how to use
android.support.v4.content.FileProvider#getUriForFile() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ApkUtil.java From AppUpdate with Apache License 2.0 | 6 votes |
/** * 安装一个apk * * @param context 上下文 * @param authorities Android N 授权 * @param apk 安装包文件 */ public static void installApk(Context context, String authorities, File apk) { Intent intent = new Intent(Intent.ACTION_VIEW); intent.setAction(Intent.ACTION_VIEW); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.addCategory(Intent.CATEGORY_DEFAULT); Uri uri; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { uri = FileProvider.getUriForFile(context, authorities, apk); intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); } else { uri = Uri.fromFile(apk); } intent.setDataAndType(uri, "application/vnd.android.package-archive"); context.startActivity(intent); }
Example 2
Source File: UpdateService.java From AppSmartUpdate with Apache License 2.0 | 6 votes |
/** * 安装apk文件 * * @return */ private PendingIntent getInstallIntent(String apkFilePath) { File appFile = new File(apkFilePath); Intent intent = new Intent(Intent.ACTION_VIEW); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); Uri fileUri = FileProvider.getUriForFile(this, this.getApplicationContext().getPackageName() + ".fileProvider", appFile); intent.setDataAndType(fileUri, "application/vnd.android.package-archive"); } else { intent.setDataAndType(Uri.fromFile(appFile), "application/vnd.android.package-archive"); } PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); return pendingIntent; }
Example 3
Source File: UpdateManager.java From imsdk-android with MIT License | 6 votes |
/** * 安装apk */ private void installApk(){ File apkfile = new File(apkFilePath); if (!apkfile.exists()) { return; } Intent i = new Intent(Intent.ACTION_VIEW); if (Build.VERSION.SDK_INT >= 24) { i.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); Uri contentUri = FileProvider.getUriForFile(mContext, ProviderUtil.getFileProviderName(mContext), apkfile); i.setDataAndType(contentUri, "application/vnd.android.package-archive"); }else{ // i.setDataAndType(Uri.parse("file://" + apkfile.toString()), "application/vnd.android.package-archive"); i.setDataAndType(Uri.fromFile(apkfile), "application/vnd.android.package-archive"); i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); } mContext.startActivity(i); }
Example 4
Source File: DownLoadServerice.java From PocketEOS-Android with GNU Lesser General Public License v3.0 | 6 votes |
/** * 通过隐式意图调用系统安装程序安装APK */ public static void install(Context context) { File file = new File( Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/pocketEos/App" , "pocketEos" + versionName + ".apk"); 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.oraclechain.pocketeos.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); }
Example 5
Source File: VersionUtil.java From AndroidWallet with GNU General Public License v3.0 | 6 votes |
private static void installApk(Context context, String downloadApk) { try { File file = new File(downloadApk); if (!file.exists()) { ToastUtils.showLong(R.string.package_not_exist); return; } Intent intent = new Intent(Intent.ACTION_VIEW); String type = getMimeType(file); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { //7.0 及以上 Uri apkUri = FileProvider.getUriForFile(context, context.getPackageName() + ".provider", file); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); intent.setDataAndType(apkUri, type); } else { //6.0 及以下 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); Uri uri = Uri.fromFile(file); intent.setDataAndType(uri, type); } context.startActivity(intent); } catch (Exception e) { CheckApkExistUtil.checkApkExist(context, context.getPackageName()); } }
Example 6
Source File: MediaSnippetsActivity.java From Wrox-ProfessionalAndroid-4E with Apache License 2.0 | 6 votes |
private void listing17_15(Context context) { // Listing 17-15: Requesting a full-size picture using an Intent // Create an output file. File outputFile = new File( context.getExternalFilesDir(Environment.DIRECTORY_PICTURES), "test.jpg"); Uri outputUri = FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".files", outputFile); // Generate the Intent. Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); intent.putExtra(MediaStore.EXTRA_OUTPUT, outputUri); // Launch the camera app. startActivityForResult(intent, TAKE_PICTURE); }
Example 7
Source File: UserDataActivity.java From ToDoList with Apache License 2.0 | 6 votes |
/** * 自动获取相机权限 */ private void autoObtainCameraPermission() { if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CAMERA)) { Toasty.info(this, "您已经拒绝过一次", Toast.LENGTH_SHORT, true).show(); } ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA, Manifest.permission.READ_EXTERNAL_STORAGE}, CAMERA_PERMISSIONS_REQUEST_CODE); } else {//有权限直接调用系统相机拍照 if (hasSdcard()) { imageUri = Uri.fromFile(fileUri); //通过FileProvider创建一个content类型的Uri if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { imageUri = FileProvider.getUriForFile(UserDataActivity.this, "com.example.fileprovider", fileUri); } PhotoUtils.takePicture(this, imageUri, CODE_CAMERA_REQUEST); } else { Toasty.info(this, "设备没有SD卡", Toast.LENGTH_SHORT, true).show(); } } }
Example 8
Source File: ImagePicker.java From QuickerAndroid with GNU General Public License v3.0 | 6 votes |
public static Intent getPickImageIntent(Context context) { Intent chooserIntent = null; List<Intent> intentList = new ArrayList<>(); Intent pickIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); Intent takePhotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); takePhotoIntent.putExtra("return-data", true); Uri uri = FileProvider.getUriForFile(context, "cuiliang.android.fileprovider", getTempFile(context)); takePhotoIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri); intentList = addIntentsToList(context, intentList, pickIntent); intentList = addIntentsToList(context, intentList, takePhotoIntent); if (intentList.size() > 0) { chooserIntent = Intent.createChooser(intentList.remove(intentList.size() - 1), context.getString(R.string.pick_image_intent_text)); chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentList.toArray(new Parcelable[]{})); } return chooserIntent; }
Example 9
Source File: MainActivity.java From landlord_client with Apache License 2.0 | 5 votes |
@Override protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { super.onActivityResult(requestCode, resultCode, data); if(resultCode == RESULT_OK) { switch (requestCode) { //相册选取,不需要压缩,但是需要剪切 case REQUEST_IMAGE_GET: if(data == null) return; Uri pictureUri = data.getData(); if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { String path = AvatarChangeUtil.formatUri(this, pictureUri); pictureUri = FileProvider.getUriForFile(this, AvatarChangeUtil.FILEPROVIDER, new File(path)); AvatarChangeUtil.crop(this, pictureUri); } else { AvatarChangeUtil.crop(this, pictureUri); } break; //拍照,拍照不需要剪切,但是需要压缩 case REQUEST_IMAGE_CAPTURE: if(cameraUri != null) //压缩需要异步,不能在UI线程 EventBus.getDefault().post(new PictureCompressEvent(cameraUri)); break; //crop进行图片剪切 case REQUEST_IMAGE_CROP: if(cropUri != null) userAvatar.setImageURI(cropUri); break; } } }
Example 10
Source File: MediaStoreCompat.java From FilePicker with MIT License | 5 votes |
public void dispatchCaptureIntent(Context context, int requestCode) { Intent captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); if (captureIntent.resolveActivity(context.getPackageManager()) != null) { File photoFile = null; try { photoFile = createImageFile(); } catch (IOException e) { e.printStackTrace(); } if (photoFile != null) { mCurrentPhotoPath = photoFile.getAbsolutePath(); mCurrentPhotoUri = FileProvider.getUriForFile(mContext.get(), mCaptureStrategy.authority, photoFile); captureIntent.putExtra(MediaStore.EXTRA_OUTPUT, mCurrentPhotoUri); captureIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION); if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { List<ResolveInfo> resInfoList = context.getPackageManager() .queryIntentActivities(captureIntent, PackageManager.MATCH_DEFAULT_ONLY); for (ResolveInfo resolveInfo : resInfoList) { String packageName = resolveInfo.activityInfo.packageName; context.grantUriPermission(packageName, mCurrentPhotoUri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION); } } if (mFragment != null) { mFragment.get().startActivityForResult(captureIntent, requestCode); } else { mContext.get().startActivityForResult(captureIntent, requestCode); } } } }
Example 11
Source File: FinishedFragment.java From v9porn with MIT License | 5 votes |
/** * 调用系统播放器播放本地视频 * * @param v9PornItem item */ private void openMp4File(V9PornItem v9PornItem) { File file = new File(v9PornItem.getDownLoadPath(presenter.getCustomDownloadVideoDirPath())); if (file.exists()) { Uri uri; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { uri = FileProvider.getUriForFile(context, "com.u9porn.fileprovider", file); } else { uri = Uri.fromFile(file); } Intent intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "video/mp4"); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION); intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); PackageManager pm = context.getPackageManager(); ComponentName cn = intent.resolveActivity(pm); if (cn == null) { showMessage("你手机上未安装任何可以播放此视频的播放器!", TastyToast.INFO); return; } startActivity(intent); } else { showReDownloadFileDialog(v9PornItem); } }
Example 12
Source File: UserDataActivity.java From ToDoList with Apache License 2.0 | 5 votes |
@Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); switch (requestCode) { //调用系统相机申请拍照权限回调 case CAMERA_PERMISSIONS_REQUEST_CODE: { if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { if (hasSdcard()) { imageUri = Uri.fromFile(fileUri); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) imageUri = FileProvider.getUriForFile(UserDataActivity.this, "com.example.fileprovider", fileUri);//通过FileProvider创建一个content类型的Uri PhotoUtils.takePicture(this, imageUri, CODE_CAMERA_REQUEST); } else { Toasty.info(this, "设备没有SD卡", Toast.LENGTH_SHORT, true).show(); } } else { Toasty.info(this, "请允许打开相机", Toast.LENGTH_SHORT, true).show(); } break; } //调用系统相册申请Sdcard权限回调 case STORAGE_PERMISSIONS_REQUEST_CODE: if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { PhotoUtils.openPic(this, CODE_GALLERY_REQUEST); } else { Toasty.info(this, "请允许操作SD卡", Toast.LENGTH_SHORT, true).show(); } break; default: } }
Example 13
Source File: AccountFragment.java From zom-android-matrix with Apache License 2.0 | 5 votes |
/** * Get URI to image received from capture by camera. */ private Uri getCaptureImageOutputUri() { Uri outputFileUri = null; File getImage = getActivity().getExternalCacheDir(); if (getImage != null) { getImage = (new File(getImage.getPath(), "pickImageResult.jpg")); outputFileUri = FileProvider.getUriForFile(getActivity(), getContext().getPackageName() + ".provider", getImage); } return outputFileUri; }
Example 14
Source File: DownloadApk.java From AndroidAppUpdateLibrary with MIT License | 5 votes |
private static Uri getUriFromFile(String location) { if(Build.VERSION.SDK_INT<24){ return Uri.fromFile(new File(location + "app-debug.apk")); } else{ return FileProvider.getUriForFile(context, context.getApplicationContext().getPackageName() + ".provider", new File(location + "app-debug.apk")); } }
Example 15
Source File: MediaStoreCompat.java From AndroidDownload with Apache License 2.0 | 5 votes |
public void dispatchCaptureIntent(Context context, int requestCode) { Intent captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); if (captureIntent.resolveActivity(context.getPackageManager()) != null) { File photoFile = null; try { photoFile = createImageFile(); } catch (IOException e) { e.printStackTrace(); } if (photoFile != null) { mCurrentPhotoPath = photoFile.getAbsolutePath(); mCurrentPhotoUri = FileProvider.getUriForFile(mContext.get(), mCaptureStrategy.authority, photoFile); captureIntent.putExtra(MediaStore.EXTRA_OUTPUT, mCurrentPhotoUri); captureIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION); if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { List<ResolveInfo> resInfoList = context.getPackageManager() .queryIntentActivities(captureIntent, PackageManager.MATCH_DEFAULT_ONLY); for (ResolveInfo resolveInfo : resInfoList) { String packageName = resolveInfo.activityInfo.packageName; context.grantUriPermission(packageName, mCurrentPhotoUri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION); } } if (mFragment != null) { mFragment.get().startActivityForResult(captureIntent, requestCode); } else { mContext.get().startActivityForResult(captureIntent, requestCode); } } } }
Example 16
Source File: ImagePickerActivity.java From ImagePicker with Apache License 2.0 | 5 votes |
/** * 跳转相机拍照 */ private void showCamera() { if (isSingleType) { //如果是单类型选取,判断添加类型是否满足(照片视频不能共存) ArrayList<String> selectPathList = SelectionManager.getInstance().getSelectPaths(); if (!selectPathList.isEmpty()) { if (MediaFileUtil.isVideoFileType(selectPathList.get(0))) { //如果存在视频,就不能拍照了 Toast.makeText(this, getString(R.string.single_type_choose), Toast.LENGTH_SHORT).show(); return; } } } //拍照存放路径 File fileDir = new File(Environment.getExternalStorageDirectory(), "Pictures"); if (!fileDir.exists()) { fileDir.mkdir(); } mFilePath = fileDir.getAbsolutePath() + "/IMG_" + System.currentTimeMillis() + ".jpg"; Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); Uri uri; if (Build.VERSION.SDK_INT >= 24) { uri = FileProvider.getUriForFile(this, ImagePickerProvider.getFileProviderName(this), new File(mFilePath)); } else { uri = Uri.fromFile(new File(mFilePath)); } intent.putExtra(MediaStore.EXTRA_OUTPUT, uri); startActivityForResult(intent, REQUEST_CODE_CAPTURE); }
Example 17
Source File: CompatHelper.java From star-zone-android with Apache License 2.0 | 5 votes |
public static Uri getUri(@NonNull File file) { try { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { return FileProvider.getUriForFile(AppContext.getAppContext(), PROVIDER_AUTHER, file); } else { return Uri.fromFile(file); } } catch (Exception e) { e.printStackTrace(); return null; } }
Example 18
Source File: AndroidSharing.java From QtAndroidTools with MIT License | 5 votes |
public boolean shareFile(boolean FileAvailable, String MimeType, String FilePath) { final String PackageName = mActivityInstance.getApplicationContext().getPackageName(); final Intent ReturnFileIntent = new Intent(PackageName + ".ACTION_RETURN_FILE"); if(FileAvailable == true) { Uri FileUri; try { FileUri = FileProvider.getUriForFile(mActivityInstance, PackageName + ".qtandroidtoolsfileprovider", new File(FilePath) ); } catch(IllegalArgumentException e) { Log.e(TAG, "The selected file can't be shared: " + FilePath); return false; } ReturnFileIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); ReturnFileIntent.setDataAndType(FileUri, MimeType); mActivityInstance.setResult(Activity.RESULT_OK, ReturnFileIntent); } else { ReturnFileIntent.setDataAndType(null, ""); mActivityInstance.setResult(Activity.RESULT_CANCELED, ReturnFileIntent); } return true; }
Example 19
Source File: DownloadService.java From QNRTC-Android with Apache License 2.0 | 5 votes |
private void installApk(Context context, File apkFile) { Intent intent = new Intent(Intent.ACTION_VIEW); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // for Android 7.0 and newer version, we need to use FileProvider to pass the file path, otherwise it will throw FileUriExposedException if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { Uri uri = FileProvider.getUriForFile(context, context.getPackageName() + ".update.provider", apkFile); intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); intent.setDataAndType(uri, "application/vnd.android.package-archive"); } else { intent.setDataAndType(getApkUri(apkFile), "application/vnd.android.package-archive"); } startActivity(intent); }
Example 20
Source File: EnvironmentUtil.java From apkextractor with GNU General Public License v3.0 | 4 votes |
/** * 传入的file须为主存储下的文件,且对file有完整的读写权限 */ public static Uri getUriForFileByFileProvider(@NonNull Context context,@NonNull File file){ return FileProvider.getUriForFile(context,"com.github.ghmxr.apkextractor.FileProvider",file); }