关于android获取相机拍照文件

android获取相机拍照文件可分为7.0以上和7.0一下两种情况,7.0以上Google认为直接使用本地的根目录即file:// URI是不安全的操作,直接访问会抛出FileUriExposedExCeption异常。所以要通过一个叫FileProvider的类对访问路径加以临时的访问权限具体如下:

在清单文件下声明:

<provider
    android:name="android.support.v4.content.FileProvider"
    android:authorities="zhuyekeji.zhengzhou.diantiwuye"
    android:exported="false"
    android:grantUriPermissions="true">
    <!-- 元数据 -->
    <meta-data
        android:name="android.support.FILE_PROVIDER_PATHS"
        android:resource="@xml/file_paths" />
</provider>

然后在res目录下建立xml文件夹建立file_paths文件,文件内容如下:

<paths>
    <external-path
        name="files_root"
        path="Android/data/zhuyekeji.zhengzhou.diantiwuye/" />
    <external-path
        name="external_storage_root"
        path="." />
</paths>

其中path的值必须和清单文件中的authorities一样,name可以任意写。然后就是权限了6.0一下只需在清单文件加上:

<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

打开系统相机和往sd卡写数据的权限。但是6.0之后加了动态权限,需要动态申请权限具体如下

if (!RequestPermissionUtils.getPermission(UPBaoGaActivity.this,
        Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA))
{
    RequestPermissionUtils.grequest(UPBaoGaActivity.this,
            Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA);
} else
{
    chooseType();//表示此程序已经获得权限
}
如果没有此权限会动态申请权限然后在activity的onRequestPermissionsResult方法判断是否获得该权限。
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults)
{
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);

    switch (requestCode)
    {
        case 1:
            for (int i = 0; i < grantResults.length; i++)
            {
                if (grantResults[i] == PackageManager.PERMISSION_GRANTED)
                {
                  //已获得权限
                } else
                {
                   //拒绝权限申请
                }
            }

            break;
    }

}

然后在Activity中初始化声明的临时访问路径

name = System.currentTimeMillis();
file = new File(FileUtil.getCachePath(UPBaoGaActivity.this), name + ".jpg");
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N)
{
    uri = Uri.fromFile(file);
} else
{
    //通过FileProvider创建一个content类型的Uri(android 7.0需要这样的方法跨应用访问)
    uri = FileProvider.getUriForFile(App.getInstance(), "zhuyekeji.zhengzhou.diantiwuye", file);
}

然后就可以拍照了。但是问题又来了,每次拍完照后之前的照片就会被覆盖。我是这么解决的,每次拍完照后将拍的照片复制一份将名字改了,这样就不会覆盖之前的照片了。具体如下:

File f= new File(FileUtil.getCachePath(UPBaoGaActivity.this), System.currentTimeMillis() + ".jpg");
Uri uri=FileProvider.getUriForFile(App.getInstance(), "zhuyekeji.zhengzhou.diantiwuye", f);
copyFile(Uri.fromFile(filee).toString(),uri.toString());
public void copyFile(String oldPath, String newPath) {
    try {
        int bytesum = 0;
        int byteread = 0;
        File oldfile = new File(oldPath);
        if (oldfile.exists()) { //文件存在时
            InputStream inStream = new FileInputStream(oldPath); //读入原文件
            FileOutputStream fs = new FileOutputStream(newPath);
            byte[] buffer = new byte[1444];
            int length;
            while ( (byteread = inStream.read(buffer)) != -1) {
                bytesum += byteread; //字节数 文件大小
                System.out.println(bytesum);
                fs.write(buffer, 0, byteread);
            }
            inStream.close();
        }
    }
    catch (Exception e) {
        System.out.println("复制单个文件操作出错");
        e.printStackTrace();

    }

}
private void chooseType()
{
   
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    intent.putExtra(MediaStore.Images.Media.ORIENTATION, 0);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
    startActivityForResult(intent, REQUEST_CODE_TAKE_PHOTO);
} 害羞第一次发,鼓励一下吧。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值