当前位置: 首页 > news >正文

巩义市住房城乡建设局网站智库门户网站建设

巩义市住房城乡建设局网站,智库门户网站建设,有哪些网站可以推广,网站排名 各因素前言 最近线上反馈#xff0c;部分vivo手机更换头像时调用系统相册保存图片失败#xff0c;经本人测试#xff0c;确实有问题。 经修复后#xff0c;贴出这块的代码供小伙伴们参考使用。 功能 更换头像选择图片#xff1a; 调用系统相机拍照#xff0c;调用系统图片…前言 最近线上反馈部分vivo手机更换头像时调用系统相册保存图片失败经本人测试确实有问题。 经修复后贴出这块的代码供小伙伴们参考使用。 功能 更换头像选择图片 调用系统相机拍照调用系统图片裁剪并保存。调用系统相册选择照片调用系统图片裁剪并保存。 此功能需要动态申请 相机和读写外部存储的权限此处省略了请自行动态申请添加。 String[] permissionsnew String[]{Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE};1、布局文件activity_picture.xml ?xml version1.0 encodingutf-8? LinearLayout xmlns:androidhttp://schemas.android.com/apk/res/androidandroid:layout_widthmatch_parentandroid:layout_heightmatch_parentandroid:orientationverticalButtonandroid:idid/takePictureFromCameraandroid:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:text拍照 /Buttonandroid:idid/takePictureFromLibandroid:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:text从相册选取 /ImageViewandroid:idid/imgandroid:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:layout_marginTop10dp/ /LinearLayout2、PictureActivity public class PictureActivity extends AppCompatActivity {public class Const {public static final int PHOTO_GRAPH 1;// 拍照public static final int PHOTO_ZOOM 2; // 相册public static final int PHOTO_RESOULT 3;// 结果public static final String IMAGE_UNSPECIFIED image/*;}public String authority;private ImageView imageView;Overrideprotected void onCreate(Nullable Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_picture);authority getApplicationContext().getPackageName() .fileprovider;imageView findViewById(R.id.img);findViewById(R.id.takePictureFromCamera).setOnClickListener(v - openCamera(Const.PHOTO_GRAPH));findViewById(R.id.takePictureFromLib).setOnClickListener(v - openCamera(Const.PHOTO_ZOOM));}private void openCamera(int type) {Intent intent;if (type Const.PHOTO_GRAPH) {//打开相机intent new Intent(MediaStore.ACTION_IMAGE_CAPTURE);//指定调用相机拍照后照片的储存路径File photoFile new File(CoreConstants.getNurseDownloadFile(this), temp.jpg);if (!photoFile.exists()) {photoFile.getParentFile().mkdirs();}Uri uri FileUtil.getUri(this, authority, photoFile);intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);} else {//打开相册intent new Intent(Intent.ACTION_PICK, null);intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, Const.IMAGE_UNSPECIFIED);}startActivityForResult(intent, type);}/*** 调用系统的裁剪图片*/private void crop(Uri uri) {try {Intent intent new Intent(com.android.camera.action.CROP);String contentURl CoreConstants.getNurseDownloadFile(this) File.separator temp.jpg;File cropFile new File(contentURl);Uri cropUri;//在7.0以上跨文件传输uri时候需要用FileProviderif (Build.VERSION.SDK_INT Build.VERSION_CODES.N) {cropUri FileProvider.getUriForFile(this, getPackageName() .fileprovider, cropFile);intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);} else {cropUri Uri.fromFile(cropFile);}intent.putExtra(MediaStore.EXTRA_OUTPUT, cropUri);intent.setDataAndType(uri, Const.IMAGE_UNSPECIFIED);intent.putExtra(crop, true);// 裁剪框的比例11intent.putExtra(aspectX, 1);intent.putExtra(aspectY, 1);// 裁剪后输出图片的尺寸大小intent.putExtra(outputX, 200);intent.putExtra(outputY, 200);intent.putExtra(outputFormat, Bitmap.CompressFormat.JPEG.toString());// 图片格式intent.putExtra(noFaceDetection, true);// 取消人脸识别intent.putExtra(return-data, true);//是否返回裁剪后图片的Bitmapintent.putExtra(output, cropUri);//重要添加权限不然裁剪完后报 “保存时发生错误保存失败”ListResolveInfo resInfoList getPackageManager().queryIntentActivities(intent,PackageManager.MATCH_DEFAULT_ONLY);for (ResolveInfo resolveInfo : resInfoList) {String packageName resolveInfo.activityInfo.packageName;grantUriPermission(packageName, cropUri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);}ComponentName componentName intent.resolveActivity(getPackageManager());if (componentName ! null) {// 开启一个带有返回值的Activity请求码为PHOTO_REQUEST_CUTstartActivityForResult(intent, Const.PHOTO_RESOULT);}} catch (Exception e) {String s e.getMessage().toString();}}public Bitmap convertUriToBitmap(Uri uri) {ContentResolver contentResolver getContentResolver();try {// 将Uri转换为字节数组return BitmapFactory.decodeStream(contentResolver.openInputStream(uri));} catch (Exception e) {e.printStackTrace();return null;}}Overrideprotected void onActivityResult(int requestCode, int resultCode, Nullable Intent data) {super.onActivityResult(requestCode, resultCode, data);// 拍照if (requestCode Const.PHOTO_GRAPH) {// 设置文件保存路径File picture new File(CoreConstants.getNurseDownloadFile(this) File.separator temp.jpg);Uri uri FileUtil.getUri(this, authority, picture);crop(uri);}if (data null)return;//读取相册图片if (requestCode Const.PHOTO_ZOOM) {crop(data.getData());}//处理裁剪后的结果if (requestCode Const.PHOTO_RESOULT) {Bundle extras data.getExtras();Bitmap photo null;if(extras ! null) {photo extras.getParcelable(data);}if (photo null data.getData() ! null) {//部分小米手机extras是个null所以想拿到Bitmap要转下photo convertUriToBitmap(data.getData());}if (photo ! null) {//拿到Bitmap后直接显示在Image控件上imageView.setImageBitmap(photo);//将图片上传到服务器 // String fileName CommonCacheUtil.getUserId(); // final File file FileUtil.saveImgFile(this, photo, fileName); // final String fileKey UUID.randomUUID().toString().replaceAll(-, );//将file通过post上传到服务器//TODO后续自行发挥}}} }3、FileUtil 工具类 public class FileUtil {public static Uri getUri(Context context, String authority, File file) {Uri uri null;if (Build.VERSION.SDK_INT Build.VERSION_CODES.N) {uri FileProvider.getUriForFile(context, authority, file);} else {uri Uri.fromFile(file);}return uri;}public static File saveImgFile(Context context, Bitmap bitmap, String fileName) {if (fileName null) {System.out.println(saved fileName can not be null);return null;} else {fileName fileName .png;String path context.getFilesDir().getAbsolutePath();String lastFilePath path / fileName;File file new File(lastFilePath);if (file.exists()) {file.delete();}try {FileOutputStream outputStream context.openFileOutput(fileName, 0);bitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream);outputStream.flush();outputStream.close();} catch (FileNotFoundException var7) {var7.printStackTrace();} catch (IOException var8) {var8.printStackTrace();}return file;}} }4、工具类 CoreConstants public class CoreConstants {public static String getNurseDownloadFile(Context context) {return context.getExternalFilesDir().getAbsolutePath() /img;} }5、在 AndroidManifest.xml 中配置 FileProvider application....providerandroid:nameandroidx.core.content.FileProviderandroid:authorities${applicationId}.fileproviderandroid:exportedfalseandroid:grantUriPermissionstrue meta-dataandroid:nameandroid.support.FILE_PROVIDER_PATHSandroid:resourcexml/filepaths //provider/application6、filepaths.xml 文件 pathsexternal-path pathnotePadRecorder/ namenotePadRecorder /external-path namemy_images pathPictures/external-path nameexternal_files path./root-path nameroot_path path. / /paths 这部分代码在小米和vivo手机上测过是正常的。 目前线上也没有反馈在其他机型上该功能有问题如有问题后续持续更新此文章。
http://www.zqtcl.cn/news/37929/

相关文章:

  • 哪些网站开业做简单海报上海建立公司网站
  • 投标网站建设服务承诺昆明网签备案查询系统
  • 网站开发设计公司简介产品设计需要学的软件
  • 精品网站建设比较好免费素材网站 可商用
  • 上海交通网站建设建设银行网站上怎么查看账户
  • 国产99做视频网站网站规划有哪些内容
  • 帮传销做网站网站建设制作设计推广
  • 宁波环保营销型网站建设创意文字设计
  • 外留网站建设做电商网站有什么用
  • 有哪些网站结构是不合理的wordpress 微信 登陆
  • 杭州网站建设 网络服务苏州网站制作排名优化
  • 河北网站建设团队企业邮箱的登录方式
  • 云服务器搭建个人网站河南建设网站公司哪家好
  • 超酷的网站设计360网站制作
  • 外贸网站建设高端的商城类网站备案
  • 济南制作网站公司哪家好做电影网站赚钱的方法
  • 网站建设冷色调东莞营销型网站建设
  • 山东住房和城乡建设厅网站主页数据库设计对网站开发的影响
  • 深圳哪家网站建设公司好信息网站有哪些
  • 建大型网站公司网站开发与维护价格
  • 校园云网站建设wordpress按需求开发
  • 网站建设自查情况全球做的比较好的网站
  • 网站哪家做的比较好移动手机导航下载
  • 怎样做网站卖自己的产品链接检测工具
  • 网站备案 拍照2345网址导航浏览器
  • 首钢建设二建设公司网站网站模板与网站定制版的区别
  • 网站备案名称怎么修改电子商务网站建设教程试卷
  • 猎头公司网站建设方案制作wordpress静态首页
  • 怎样建立自己的网站辽宁建设工程信息网新网址
  • 意识形态 加强网站建设外国产品设计网站