Android 7.0 自动安装APK及拍照崩溃问题

解决方法

1,在AndroidManifest中添加

       <provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="${applicationId}.fileProvider"
            android:grantUriPermissions="true"
            android:exported="false">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths" />
        </provider>

2,在res目录下新建xml文件夹,添加file_paths.xml文件,内容如下:

        <?xml version="1.0" encoding="utf-8"?>
        <paths>
            <external-path path="Android/data/com.heyikun.hehemall/" name="files_root" />
            <external-path path="." name="external_storage_root" />

            <external-path name="external_storage_root" path="."/>
            <files-path name="files" path="."/>
        </paths>

3,安装APP

    private void installApk(File file) {
        Intent install = new Intent(Intent.ACTION_VIEW);
        //判断是否是AndroidN以及更高的版本
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            install.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            Uri contentUri = FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".fileProvider", file);
            install.setDataAndType(contentUri, "application/vnd.android.package-archive");
        } else {
            install.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
            install.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        mContext.startActivity(install);
    }

4,拍照

  public Intent dispatchTakePictureIntent() throws IOException {
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    if (takePictureIntent.resolveActivity(mContext.getPackageManager()) != null) {
      File file = createImageFile();
      Uri photoFile;
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        String authority = mContext.getApplicationInfo().packageName + ".fileProvider";
        photoFile = FileProvider.getUriForFile(this.mContext.getApplicationContext(), authority, file);
      } else {
        photoFile = Uri.fromFile(file);
      }
      if (photoFile != null) {
        takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoFile);
      }
    }
    return takePictureIntent;
  }





    private void openCamera() {
        try {
            Intent intent = captureManager.dispatchTakePictureIntent();
            startActivityForResult(intent, ImageCaptureManager.REQUEST_TAKE_PHOTO);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ActivityNotFoundException e) {
            // TODO No Activity Found to handle Intent
            e.printStackTrace();
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值