Android 7.0
1.0 项目中调用 照相机拍照/录像
FileProvider 适配
1. 第一步:在manifest清单文件中注册provider
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.jiangzqts.fileprovider"
android:grantUriPermissions="true"
android:exported="false">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
exported:要求必须为false,为true则会报安全异常。grantUriPermissions:true,表示授予 URI 临时访问权限。
2.第二步:指定共享的目录
为了指定共享的目录我们需要在资源(res)目录下创建一个xml目录,然后创建一个名为“file_paths”(名字可以随便起,只要和在manifest注册的provider所引用的resource保持一致即可)的资源文件,内容如下:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<paths>
<external-path path="" name="camera_photos" />
</paths>
</resources>
- 代表的根目录: Context.getFilesDir()
- 代表的根目录: Environment.getExternalStorageDirectory()
代表的根目录: getCacheDir()
上述代码中path="",是有特殊意义的,它代码根目录,也就是说你可以向其它的应用共享根目录及其子目录下任何一个文件了,如果你将path设为path="pictures",
那么它代表着根目录下的pictures目录(eg:/storage/emulated/0/pictures),如果你向其它应用分享pictures目录范围之外的文件是不行的。
。
3.使用FileProvider
File file=new File(Environment.getExternalStorageDirectory(), "/temp/"+System.currentTimeMillis() + ".jpg");
Uri imageUri = FileProvider.getUriForFile(context, "com.jiangzqts.fileprovider", file);//通过FileProvider创建一个content类型的Uri
Intent intent = new Intent();
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); //添加这一句表示对目标应用临时授权该Uri所代表的文件
intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);//设置Action为拍照
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);//将拍取的照片保存到指定URI
startActivityForResult(intent,1006);
1.Uri imageUri = FileProvider.getUriForFile(context, "", file);//通过FileProvider创建一个content类型的Uri
2.intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); //添加这一句表示对目标应用临时授权该Uri所代表的文件
2.0 裁剪图片
注意 inputUri 需要写成FileProvider.getUriForFile ,而如果把outUri 写成FileProvider.getUriForFile 就会有问题?仍然使用的是 Uri.fromFile() 方法获取的 File URI 类型,事实上,使用这种方式调用系统裁剪功能本身就是有问题的!常见问题如:在部分机型上,调用系统裁剪并返回前一个页面时,在 onActivityResult() 方法中得到的 resultCode 值不等于 RESULT_OK。
outUri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(), "crop_head_img.jpg"));
Intent intent = new Intent("com.android.camera.action.CROP");
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setDataAndType(inputUri, "image/*");
// 下面这个crop=true是设置在开启的Intent中设置显示的VIEW可裁剪
intent.putExtra("crop", "true");
intent.putExtra("scale", true);// 保存比例
intent.putExtra("scaleUpIfNeeded", true);// 去黑边
intent.putExtra(MediaStore.EXTRA_OUTPUT, outUri);
3.0 安装APK
同上,创建URI的方式不同
public static void installApk(Context context, String apkPath) {
File file = new File(apkPath);
if (file.exists()) {
Intent intent = new Intent();
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction(Intent.ACTION_VIEW);
String type = "application/vnd.android.package-archive";
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
intent.setDataAndType(getUriForFile(context, context.getPackageName(), file), type);
} else {
intent.setDataAndType(Uri.fromFile(file), type);
}
context.startActivity(intent);
} else {
Toast.makeText(context, "文件不存在", Toast.LENGTH_SHORT).show();
}
}
4.0 PopupWindow弹出位置的适配问题
/**
* 排序弹框View
*
* @return
*/
private View getSelectPopView() {
// 一个自定义的布局,作为显示的内容
View contentView = LayoutInflater.from(getApplicationContext()).inflate(
R.layout.select_popupwindow, null);
popupWindow = new PopupWindow(contentView,
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, true) {
@Override
public void showAsDropDown(View anchor) {
if (Build.VERSION.SDK_INT >= 24) {
Rect rect = new Rect();
anchor.getGlobalVisibleRect(rect);
int h = anchor.getResources().getDisplayMetrics().heightPixels - rect.bottom;
setHeight(h);
}
super.showAsDropDown(anchor);
}
};
popupWindow.setTouchable(true);
popupWindow.setFocusable(true);
popupWindow.setBackgroundDrawable(new BitmapDrawable());
popupWindow.setOutsideTouchable(true);
popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
@Override
public void onDismiss() {
tv_select_pop_content.setTextColor(Color.parseColor("#565D66"));
iv_select_pop_img.setImageResource(R.mipmap.select_pop_default_down);
}
});
return contentView;
}