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

上海网站建设小程序开发企业邮箱怎么申请

上海网站建设小程序开发,企业邮箱怎么申请,做项目的招聘网站,杭州 做网站前言 最近线上反馈,部分vivo手机更换头像时调用系统相册保存图片失败,经本人测试,确实有问题。 经修复后,贴出这块的代码供小伙伴们参考使用。 功能 更换头像选择图片: 调用系统相机拍照,调用系统图片…

前言

最近线上反馈,部分vivo手机更换头像时调用系统相册保存图片失败,经本人测试,确实有问题。

经修复后,贴出这块的代码供小伙伴们参考使用。

功能

更换头像选择图片:

  • 调用系统相机拍照,调用系统图片裁剪并保存。
  • 调用系统相册选择照片,调用系统图片裁剪并保存。

此功能需要动态申请 相机和读写外部存储的权限,此处省略了,请自行动态申请添加。

String[] permissions=new String[]{Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE};

1、布局文件activity_picture.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><Buttonandroid:id="@+id/takePictureFromCamera"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="拍照" /><Buttonandroid:id="@+id/takePictureFromLib"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="从相册选取" /><ImageViewandroid:id="@+id/img"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="10dp"/>
</LinearLayout>

2、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");// 裁剪框的比例,1:1intent.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);//重要!!!添加权限,不然裁剪完后报 “保存时发生错误,保存失败”List<ResolveInfo> 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:name="androidx.core.content.FileProvider"android:authorities="${applicationId}.fileprovider"android:exported="false"android:grantUriPermissions="true" ><meta-dataandroid:name="android.support.FILE_PROVIDER_PATHS"android:resource="@xml/filepaths" /></provider></application>

6、filepaths.xml 文件:

<paths><external-path path="notePadRecorder/" name="notePadRecorder" /><external-path name="my_images" path="Pictures"/><external-path name="external_files" path="."/><root-path name="root_path" path="." />
</paths>

这部分代码在小米和vivo手机上测过,是正常的。

目前线上也没有反馈在其他机型上该功能有问题,如有问题,后续持续更新此文章。

http://www.hkea.cn/news/268924/

相关文章:

  • 黄页88企业名录seo怎么优化武汉厂商
  • 触摸屏网站如何做泰州seo网络公司
  • 银川app购物网站制作公司搜狗收录入口
  • 做单页网站要多少钱wordpress免费网站
  • 网站建设性价比高优化设计官网
  • 电脑手机网站相互跳转西安seo关键词排名优化
  • 一般做网站用什么字体比较合适搜索引擎营销是什么
  • 去什么网站发贴做推广seo及网络推广招聘
  • 如何批量建站什么是互联网营销
  • 哈尔滨网站建设如何搭建一个网站平台
  • 哪些网站可以做任务网络营销推广的基本手段
  • 互联网舆情报告什么是seo营销
  • 餐饮商家做网站的好处沈阳关键词优化费用
  • 如何把网站的文字编辑网站页面优化内容包括哪些
  • 安徽做公司网站哪家好销售策略和营销策略
  • 做游戏都需要什么网站吗域名注册腾讯云
  • 北京建设厅网站谷歌关键词搜索
  • 如何识别一个网站是否做的好谷歌关键词挖掘工具
  • 网站建设专家网店营销推广
  • 做试玩网站搜索引擎优化答案
  • 外贸家具网站百度引擎搜索网址
  • 公司网站的栏目设置肇庆seo优化
  • 如何制作一个论坛网站网络服务器配置与管理
  • 北京中国建设部网站有什么平台可以推广
  • flash网站优缺点厦门百度seo
  • 贵阳利于优化的网站百度搜索引擎推广步骤
  • 金色 网站 模板外链是什么
  • 网站有多难做如何做推广引流赚钱
  • 建设企业网站怎么样百度首页 百度
  • 热烈祝贺网站上线泉州seo代理计费