android.support.v4.content.FileProvider Java Examples

The following examples show how to use android.support.v4.content.FileProvider. 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: SettingsActivity.java    From TelePlus-Android with GNU General Public License v2.0 7 votes vote down vote up
private void sendLogs() {
    try {
        ArrayList<Uri> uris = new ArrayList<>();
        File sdCard = ApplicationLoader.applicationContext.getExternalFilesDir(null);
        File dir = new File(sdCard.getAbsolutePath() + "/logs");
        File[] files = dir.listFiles();

        for (File file : files) {
            if (Build.VERSION.SDK_INT >= 24) {
                uris.add(FileProvider.getUriForFile(getParentActivity(), BuildConfig.APPLICATION_ID + ".provider", file));
            } else {
                uris.add(Uri.fromFile(file));
            }
        }

        if (uris.isEmpty()) {
            return;
        }
        Intent i = new Intent(Intent.ACTION_SEND_MULTIPLE);
        if (Build.VERSION.SDK_INT >= 24) {
            i.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        }
        i.setType("message/rfc822");
        i.putExtra(Intent.EXTRA_EMAIL, "");
        i.putExtra(Intent.EXTRA_SUBJECT, "last logs");
        i.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
        getParentActivity().startActivityForResult(Intent.createChooser(i, "Select email application."), 500);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example #2
Source File: UpdateDownloadService.java    From v9porn with MIT License 6 votes vote down vote up
private void installApk(String path) {

        File file = new File(path);
        Uri uri;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            uri = FileProvider.getUriForFile(getApplicationContext(), BuildConfig.APPLICATION_ID+".fileprovider", file);
            //兼容8.0
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                boolean hasInstallPermission = getPackageManager().canRequestPackageInstalls();
                if (!hasInstallPermission) {
                    TastyToast.makeText(getApplicationContext(), "请在设置中打开允许安装未知来源", TastyToast.LENGTH_LONG,TastyToast.ERROR);
                    startInstallPermissionSettingActivity();
                    return;
                }
            }
        } else {
            uri = Uri.fromFile(file);
        }
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(uri, "application/vnd.android.package-archive");
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        startActivity(intent);
    }
 
Example #3
Source File: ImageUpdater.java    From TelePlus-Android with GNU General Public License v2.0 6 votes vote down vote up
public void openCamera() {
    if (parentFragment == null || parentFragment.getParentActivity() == null) {
        return;
    }
    try {
        if (Build.VERSION.SDK_INT >= 23 && parentFragment.getParentActivity().checkSelfPermission(Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
            parentFragment.getParentActivity().requestPermissions(new String[]{Manifest.permission.CAMERA}, 19);
            return;
        }
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        File image = AndroidUtilities.generatePicturePath();
        if (image != null) {
            if (Build.VERSION.SDK_INT >= 24) {
                takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, FileProvider.getUriForFile(parentFragment.getParentActivity(), BuildConfig.APPLICATION_ID + ".provider", image));
                takePictureIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
                takePictureIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            } else {
                takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(image));
            }
            currentPicturePath = image.getAbsolutePath();
        }
        parentFragment.startActivityForResult(takePictureIntent, 13);
    } catch (Exception e) {
        FileLog.e(e);
    }
}
 
Example #4
Source File: TestIntentActivity.java    From Android-utils with Apache License 2.0 6 votes vote down vote up
public void testCapture(View v) {
    PermissionUtils.checkPermissions(this, new OnGetPermissionCallback() {
        @Override
        public void onGetPermission() {
            File file = new File(PathUtils.getExternalAppFilesPath() + File.separator + "capture.jpeg");
            Uri data;
            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
                data = Uri.fromFile(file);
            } else {
                String authority = getPackageName() + ".fileProvider";
                data = FileProvider.getUriForFile(TestIntentActivity.this, authority, file);
            }
            startActivityForResult(IntentUtils.getCaptureIntent(data), REQUEST_CODE_CAPTURE);
        }
    }, Permission.CAMERA, Permission.STORAGE);
}
 
Example #5
Source File: MainActivity.java    From CarRecognition with MIT License 6 votes vote down vote up
private void dispatchTakePictureIntent() {
    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();
        } catch (IOException ex) {
            // Error occurred while creating the File
        }
        // Continue only if the File was successfully created
        if (photoFile != null) {
            photoURI = FileProvider.getUriForFile(this,
                    "uz.shukurov.carrecognition.fileprovider", photoFile);
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
            startActivityForResult(takePictureIntent, RequestCode.REQUEST_TAKE_PHOTO);
        }
    }
}
 
Example #6
Source File: UpdateManager.java    From imsdk-android with MIT License 6 votes vote down vote up
/**
 * 安装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 #7
Source File: ShareUtil.java    From imsdk-android with MIT License 6 votes vote down vote up
public static void shareFile(Context context, File file, String shareType, String title) {
    Intent intent = new Intent(Intent.ACTION_SEND);
    Uri fileUri;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        if (TYPE_VIDEO.equals(shareType)) {
            fileUri = getVideoContentUri(context, file);
        } else if (TYPE_IMAGE.equals(shareType)) {
            fileUri = getImageContentUri(context, file);
        } else {
            fileUri = FileProvider.getUriForFile(context, context.getPackageName() + ".fileProvider", file);
        }
        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    } else {
        fileUri = Uri.fromFile(file);
    }
    intent.putExtra(Intent.EXTRA_STREAM, fileUri);
    intent.setType(shareType);
    Intent chooser = Intent.createChooser(intent, title);

    if (intent.resolveActivity(context.getPackageManager()) != null) {
        context.startActivity(chooser);
    }
}
 
Example #8
Source File: ReceiveDialogFragment.java    From nano-wallet-android with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public void onClickShare(View view) {
    analyticsService.track(AnalyticsEvents.SHARE_DIALOGUE_VIEWED);
    binding.receiveCard.cardLayout.setVisibility(View.VISIBLE);
    saveImage(setViewToBitmapImage(binding.receiveCard.cardLayout));
    File imagePath = new File(getContext().getCacheDir(), "images");
    File newFile = new File(imagePath, fileName);
    Uri imageUri = FileProvider.getUriForFile(getContext(), "co.nano.nanowallet.fileprovider", newFile);
    Intent shareIntent = new Intent(Intent.ACTION_SEND);
    shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    shareIntent.putExtra(Intent.EXTRA_TEXT, address.getAddress());
    shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
    shareIntent.setDataAndType(imageUri, getActivity().getContentResolver().getType(imageUri));
    shareIntent.setType("image/*");
    startActivity(Intent.createChooser(shareIntent, getString(R.string.receive_share_title)));
    binding.receiveCard.cardLayout.setVisibility(View.INVISIBLE);
}
 
Example #9
Source File: DownloadApkService.java    From FimiX8-RE with MIT License 6 votes vote down vote up
public void onSuccess(Object responseObj) {
    Uri data;
    DownloadApkService.this.handler.sendEmptyMessage(1);
    Toast.makeText(DownloadApkService.this, R.string.fimi_sdk_apk_down_success, 0).show();
    Intent intent = new Intent("android.intent.action.VIEW");
    intent.setFlags(NTLMConstants.FLAG_UNIDENTIFIED_11);
    if (VERSION.SDK_INT >= 24) {
        data = FileProvider.getUriForFile(DownloadApkService.this, DownloadApkService.this.getPackageName() + ".fileprovider", new File(DownloadApkService.this.path));
        intent.addFlags(1);
    } else {
        data = Uri.fromFile(new File(DownloadApkService.this.path));
    }
    intent.setDataAndType(data, "application/vnd.android.package-archive");
    DownloadApkService.this.startActivity(intent);
    DownloadApkService.this.notificationManager.cancel(DownloadApkService.NOTIFACTION_ID);
    DownloadApkService.isDownApking = false;
    DownloadApkService.this.stopSelf();
}
 
Example #10
Source File: ImagePicker.java    From QuickerAndroid with GNU General Public License v3.0 6 votes vote down vote up
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 #11
Source File: FilePicker.java    From Tok-Android with GNU General Public License v3.0 6 votes vote down vote up
private static void jumpCamera(Activity activity) {
    try {
        File saveFile = FileUtilsJ.createTempImgFile();
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            imgCameraUri =
                FileProvider.getUriForFile(activity, GlobalParams.PROVIDER_AUTH, saveFile);
            takePictureIntent.setFlags(
                Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
        } else {
            imgCameraUri = Uri.fromFile(saveFile);
        }
        LogUtil.i(TAG, "imgUri:" + imgCameraUri.toString());
        takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, imgCameraUri);
        activity.startActivityForResult(takePictureIntent, REQ_IMG_CAMERA);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example #12
Source File: HomePresenter.java    From Muslim-Athkar-Islamic-Reminders with MIT License 6 votes vote down vote up
public Intent getShareIntent(File filePath){

        Intent shareIntent = new Intent(Intent.ACTION_SEND);
        shareIntent.putExtra(Intent.EXTRA_TEXT,
                context.getResources().getString(R.string.app_signature)+" "+context.getResources().getString(R.string.app_google_play_url)+".");

        shareIntent.setType("image/*");

        /** Build version >=24 we need to use file provider */
        if(Build.VERSION.SDK_INT>=24){
            Uri uri = FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".provider",filePath.getAbsoluteFile());
            shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
        }else{
            shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(filePath));
        }

        shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

        return shareIntent;
    }
 
Example #13
Source File: UpdateApkReceiver.java    From Yuan-WanAndroid with Apache License 2.0 6 votes vote down vote up
private void installApk(Context context,Uri uri){
    LogUtil.d(LogUtil.TAG_COMMON,"安装程序"+uri);
    File file = new File(Constant.PATH_APK_DOWNLOAD_MANAGER);
    Intent intent = new Intent("android.intent.action.VIEW");
    //适配N
    if(Build.VERSION.SDK_INT>= Build.VERSION_CODES.N){
        Uri contentUrl = FileProvider.getUriForFile(context,"com.example.yuan_wanandroid.fileProvider",file);
        Log.d(LogUtil.TAG_COMMON, "installApk: "+contentUrl);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        intent.setDataAndType(contentUrl,"application/vnd.android.package-archive");
    }else{
        intent.setDataAndType(Uri.fromFile(file),"application/vnd.android.package-archive");
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    }
    context.startActivity(intent);
}
 
Example #14
Source File: MainActivity.java    From GenerateQRCode with Apache License 2.0 6 votes vote down vote up
/**
 * 拍照
 */
private void takePhoto() {
    // 创建File对象,用于存储拍照后的图片
    File outputImage = new File(getExternalCacheDir(), "output_image.jpg");
    try {
        if (outputImage.exists()) {
            outputImage.delete();
        }
        outputImage.createNewFile();
    } catch (IOException e) {
        e.printStackTrace();
    }
    if (Build.VERSION.SDK_INT < 24) {
        imageUri = Uri.fromFile(outputImage);
    } else {
        imageUri = FileProvider.getUriForFile(MainActivity.this, "com.example.xch.generateqrcode.fileprovider", outputImage);
    }
    // 启动相机程序
    Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
    intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
    startActivityForResult(intent, TAKE_PHOTO);
}
 
Example #15
Source File: ImageUpdater.java    From TelePlus-Android with GNU General Public License v2.0 6 votes vote down vote up
public void openCamera() {
    if (parentFragment == null || parentFragment.getParentActivity() == null) {
        return;
    }
    try {
        if (Build.VERSION.SDK_INT >= 23 && parentFragment.getParentActivity().checkSelfPermission(Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
            parentFragment.getParentActivity().requestPermissions(new String[]{Manifest.permission.CAMERA}, 19);
            return;
        }
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        File image = AndroidUtilities.generatePicturePath();
        if (image != null) {
            if (Build.VERSION.SDK_INT >= 24) {
                takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, FileProvider.getUriForFile(parentFragment.getParentActivity(), BuildConfig.APPLICATION_ID + ".provider", image));
                takePictureIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
                takePictureIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            } else {
                takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(image));
            }
            currentPicturePath = image.getAbsolutePath();
        }
        parentFragment.startActivityForResult(takePictureIntent, 13);
    } catch (Exception e) {
        FileLog.e(e);
    }
}
 
Example #16
Source File: UpdateService.java    From sealrtc-android with MIT License 6 votes vote down vote up
private Intent installIntent(String path) {
    File apkFile = new File(path);
    Intent intent = new Intent(Intent.ACTION_VIEW);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        Uri contentUri =
                FileProvider.getUriForFile(
                        getBaseContext().getApplicationContext(),
                        BuildConfig.APPLICATION_ID + ".provider",
                        apkFile);
        intent.setDataAndType(contentUri, "application/vnd.android.package-archive");
    } else {
        intent.setDataAndType(Uri.fromFile(apkFile), "application/vnd.android.package-archive");
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    }
    return intent;
}
 
Example #17
Source File: DownloadFileActivity.java    From imsdk-android with MIT License 6 votes vote down vote up
private void externalShare(File file) {
    Intent share_intent = new Intent();
    share_intent.setAction(Intent.ACTION_SEND);//设置分享行为
    share_intent.setType("*/*");  //设置分享内容的类型
    Uri uri;
    if (Build.VERSION.SDK_INT >= 24) {
        share_intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        uri = FileProvider.getUriForFile(this, ProviderUtil.getFileProviderName(DownloadFileActivity.this), file);//android 7.0以上
    } else {
        uri = Uri.fromFile(file);
    }
    share_intent.putExtra(Intent.EXTRA_STREAM, uri);
    //创建分享的Dialog
    share_intent = Intent.createChooser(share_intent, "分享");
    startActivity(share_intent);
}
 
Example #18
Source File: CustomWebViewModule.java    From react-native-webview-android-file-upload with MIT License 6 votes vote down vote up
private Uri getOutputFilename(String intentType) {
    String prefix = "";
    String suffix = "";

    if (intentType == MediaStore.ACTION_IMAGE_CAPTURE) {
        prefix = "image-";
        suffix = ".jpg";
    } else if (intentType == MediaStore.ACTION_VIDEO_CAPTURE) {
        prefix = "video-";
        suffix = ".mp4";
    }

    String packageName = getReactApplicationContext().getPackageName();
    File capturedFile = null;
    try {
        capturedFile = createCapturedFile(prefix, suffix);
    } catch (IOException e) {
        Log.e("CREATE FILE", "Error occurred while creating the File", e);
        e.printStackTrace();
    }
    return FileProvider.getUriForFile(getReactApplicationContext(), packageName+".fileprovider", capturedFile);
}
 
Example #19
Source File: MediaSnippetsActivity.java    From Wrox-ProfessionalAndroid-4E with Apache License 2.0 6 votes vote down vote up
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 #20
Source File: AvatarChangeUtil.java    From landlord_client with Apache License 2.0 6 votes vote down vote up
/**
 * 判断系统及拍照
 */
public static void takePicture(Activity activity) {
    Uri pictureUri;
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    //对目标uri临时授权
    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
    File pictureFile = createImageFile(activity, null);
    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        pictureUri = FileProvider.getUriForFile(activity,
                FILEPROVIDER, pictureFile);
    } else {
        pictureUri = Uri.fromFile(pictureFile);
    }
    //去拍照
    intent.putExtra(MediaStore.EXTRA_OUTPUT, pictureUri);
    ((MainActivity) activity).setCameraUri(pictureUri);
    if(intent.resolveActivity(activity.getPackageManager()) != null) {
        activity.startActivityForResult(intent, MainActivity.REQUEST_IMAGE_CAPTURE);
    }
}
 
Example #21
Source File: VideoAdsHandler.java    From VideoOS-Android-SDK with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void onReceive(Context context, Intent intent) {
    String filePath = intent.getStringExtra("filePath");
    String fileProvider = intent.getStringExtra("fileProvider");
    // 前往安装APK页面,在此时算作开始安装
    Intent resultIntent = new Intent(Intent.ACTION_VIEW);
    resultIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

    Uri data;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        // 7.0 通过FileProvider的方式访问
        data = FileProvider.getUriForFile(context, fileProvider, new File(filePath));
        resultIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);// 赋予临时权限
    } else {
        data = Uri.fromFile(new File(filePath));
    }
    resultIntent.setDataAndType(data, "application/vnd.android.package-archive");

    context.startActivity(resultIntent);


    ObservableManager.getDefaultObserable().sendToTarget(VenvyObservableTarget.TAG_INSTALL_START);

}
 
Example #22
Source File: IntentUtils.java    From AppSmartUpdate with Apache License 2.0 6 votes vote down vote up
public static boolean installApk(Context context, String filePath) {
    try {
        File appFile = new File(filePath);
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            Uri fileUri = FileProvider.getUriForFile(context, context.getApplicationContext().getPackageName() + ".fileProvider", appFile);
            intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            intent.setDataAndType(fileUri, "application/vnd.android.package-archive");
        } else {
            intent.setDataAndType(Uri.fromFile(appFile), "application/vnd.android.package-archive");
        }
        if (context.getPackageManager().queryIntentActivities(intent, 0).size() > 0) {
            context.startActivity(intent);
        }
        return true;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return false;
}
 
Example #23
Source File: UpdateService.java    From AppSmartUpdate with Apache License 2.0 6 votes vote down vote up
/**
 * 安装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 #24
Source File: CreatePostActivity.java    From 1Rramp-Android with MIT License 6 votes vote down vote up
private void openCameraIntent() {
  leftActivityWithPurpose = true;
  Intent pictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
  if (pictureIntent.resolveActivity(getPackageManager()) != null) {
    File photoFile = null;
    try {
      photoFile = createImageFile();
    }
    catch (IOException ex) {

    }
    if (photoFile != null) {
      Uri photoURI = FileProvider.getUriForFile(this, "com.hapramp.provider", photoFile);
      pictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
        photoURI);
      startActivityForResult(pictureIntent,
        REQUEST_CAPTURE_IMAGE);
    }
  }
}
 
Example #25
Source File: ApkUtil.java    From AppUpdate with Apache License 2.0 6 votes vote down vote up
/**
 * 安装一个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 #26
Source File: NotificationUtil.java    From AppUpdate with Apache License 2.0 6 votes vote down vote up
/**
 * 显示下载完成的通知,点击进行安装
 *
 * @param context     上下文
 * @param icon        图标
 * @param title       标题
 * @param content     内容
 * @param authorities Android N 授权
 * @param apk         安装包
 */
public static void showDoneNotification(Context context, int icon, String title, String content,
                                        String authorities, File apk) {
    NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    //不知道为什么需要先取消之前的进度通知,才能显示完成的通知。
    manager.cancel(requireManagerNotNull().getNotifyId());
    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");
    PendingIntent pi = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
    NotificationCompat.Builder builder = builderNotification(context, icon, title, content)
            .setContentIntent(pi);
    Notification notification = builder.build();
    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    manager.notify(requireManagerNotNull().getNotifyId(), notification);
}
 
Example #27
Source File: MeFragment.java    From AndroidPlusJava with GNU General Public License v3.0 6 votes vote down vote up
private void navigateToCamera() {
    File file = new File(IMAGE_DIR);
    if (!file.exists()) {
        file.mkdirs();
    }
    Uri uri = null;
    mImageName = String.valueOf(System.currentTimeMillis()) + ".png";
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    //7.0适配,通过FileProvider获取图片Uri
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
        uri = FileProvider.getUriForFile(getContext(),
                "com.leon.androidplus.fileProvider", file);
    } else {
        //直接通过文件获取Uri
        uri = Uri.fromFile(new File(IMAGE_DIR + mImageName));
    }
    intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
    startActivityForResult(intent, REQUEST_CAMERA_IMAGES);
}
 
Example #28
Source File: UserDataActivity.java    From ToDoList with Apache License 2.0 6 votes vote down vote up
/**
 * 自动获取相机权限
 */
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 #29
Source File: WorkWorldBrowersingFragment.java    From imsdk-android with MIT License 6 votes vote down vote up
private void externalShare(File file){
    Intent share_intent = new Intent();
    share_intent.setAction(Intent.ACTION_SEND);//设置分享行为
    share_intent.setType("image/*");  //设置分享内容的类型
    Uri uri;
    if (Build.VERSION.SDK_INT >= 24) {
        share_intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        uri = FileProvider.getUriForFile(getActivity(), ProviderUtil.getFileProviderName(getActivity()), file);//android 7.0以上
    }else {
        uri = Uri.fromFile(file);
    }
    share_intent.putExtra(Intent.EXTRA_STREAM, uri);
    //创建分享的Dialog
    share_intent = Intent.createChooser(share_intent, "分享");
    startActivity(share_intent);
}
 
Example #30
Source File: UpdateDownloadService.java    From v9porn with MIT License 6 votes vote down vote up
private void installApk(String path) {

        File file = new File(path);
        Uri uri;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            uri = FileProvider.getUriForFile(getApplicationContext(), "com.u9porn.fileprovider", file);
        } else {
            uri = Uri.fromFile(file);
        }
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(uri, "application/vnd.android.package-archive");
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        startActivity(intent);
    }