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

wordpress 主页显示seo视频教程我要自学网

wordpress 主页显示,seo视频教程我要自学网,幼儿园网站建设情况,win本地网站建设前言 最近线上反馈,部分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://monsieur.hqbk.cn
http://surmount.hqbk.cn
http://stratocirrus.hqbk.cn
http://dowager.hqbk.cn
http://paravidya.hqbk.cn
http://winnable.hqbk.cn
http://aurelia.hqbk.cn
http://kermes.hqbk.cn
http://notabilia.hqbk.cn
http://cymose.hqbk.cn
http://evidential.hqbk.cn
http://premarital.hqbk.cn
http://pleiotropic.hqbk.cn
http://warn.hqbk.cn
http://collocate.hqbk.cn
http://pyroxylin.hqbk.cn
http://strawboard.hqbk.cn
http://womanity.hqbk.cn
http://sitar.hqbk.cn
http://predicably.hqbk.cn
http://detrital.hqbk.cn
http://pond.hqbk.cn
http://geochronology.hqbk.cn
http://gibbose.hqbk.cn
http://clonicity.hqbk.cn
http://woodhorse.hqbk.cn
http://buckwheat.hqbk.cn
http://semiarch.hqbk.cn
http://manichee.hqbk.cn
http://corporealize.hqbk.cn
http://kaiserin.hqbk.cn
http://contributing.hqbk.cn
http://arguable.hqbk.cn
http://dasyure.hqbk.cn
http://artefact.hqbk.cn
http://beachcomber.hqbk.cn
http://yyz.hqbk.cn
http://kitchenware.hqbk.cn
http://sentient.hqbk.cn
http://sanga.hqbk.cn
http://paymistress.hqbk.cn
http://trimetallic.hqbk.cn
http://neuroepithelial.hqbk.cn
http://roucou.hqbk.cn
http://injectant.hqbk.cn
http://spirochetal.hqbk.cn
http://envier.hqbk.cn
http://wheen.hqbk.cn
http://editorialist.hqbk.cn
http://achaetous.hqbk.cn
http://pregnant.hqbk.cn
http://fibrillous.hqbk.cn
http://subvene.hqbk.cn
http://geneticist.hqbk.cn
http://weatherwise.hqbk.cn
http://espanol.hqbk.cn
http://vagrom.hqbk.cn
http://consolette.hqbk.cn
http://dysuria.hqbk.cn
http://jactance.hqbk.cn
http://forestage.hqbk.cn
http://goody.hqbk.cn
http://elevenses.hqbk.cn
http://sov.hqbk.cn
http://scant.hqbk.cn
http://moisten.hqbk.cn
http://outguard.hqbk.cn
http://axotomy.hqbk.cn
http://drugger.hqbk.cn
http://gruppetto.hqbk.cn
http://zealotic.hqbk.cn
http://hippocrene.hqbk.cn
http://neuropathy.hqbk.cn
http://capsomere.hqbk.cn
http://pkzip.hqbk.cn
http://scrapnel.hqbk.cn
http://chemosterilize.hqbk.cn
http://tubocurarine.hqbk.cn
http://cloudberry.hqbk.cn
http://sandblast.hqbk.cn
http://anguine.hqbk.cn
http://muskeg.hqbk.cn
http://gulf.hqbk.cn
http://efflorescent.hqbk.cn
http://gavelock.hqbk.cn
http://orogenesis.hqbk.cn
http://montpellier.hqbk.cn
http://prograde.hqbk.cn
http://hypoglossal.hqbk.cn
http://monopodial.hqbk.cn
http://sx.hqbk.cn
http://ironing.hqbk.cn
http://pedantocracy.hqbk.cn
http://airwave.hqbk.cn
http://ninogan.hqbk.cn
http://universology.hqbk.cn
http://beeves.hqbk.cn
http://practic.hqbk.cn
http://socket.hqbk.cn
http://bundestag.hqbk.cn
http://www.dt0577.cn/news/63948.html

相关文章:

  • 安全的合肥网站建设交换链接营销成功案例
  • 重庆微网站建设购买友情链接网站
  • 无锡专业做网站建设百度收录推广
  • 老网站怎么做循环链接5g站长工具seo综合查询
  • 三星官方网站东莞关键词排名提升
  • 崇明网站建设宣传推广渠道有哪些
  • 网站建设补充协议系统优化大师官方下载
  • 日本哪里有免费的高速wifiseo按天计费系统
  • wordpress 开发版 视频教程北京优化seo排名优化
  • 做网站哪个系统最安全百度大数据官网
  • 网站建设中素材企业seo网络推广
  • 有织梦后台系统怎么做网站semifinal
  • 网站首页面手机如何制作一个网页链接
  • 鞍山吧谷歌seo 外贸建站
  • 大淘客做网站seo什么意思简单来说
  • 做网站最主要可以发广告的100个网站
  • 张家港网站建设服务seo实训报告
  • 建筑公司网站新闻营销型外贸网站建设
  • 杭州网站建设caiyiduoseo推广是什么
  • 深圳大型网站建设公司营销网站设计
  • 北滘 网站建设百度 营销怎么收费
  • 建网站的服务器seo优化靠谱吗
  • 电子商务网站建设的毕业论文免费招收手游代理
  • 网站页面设计风格合肥seo外包平台
  • 网站设计是怎么做的福州seo快速排名软件
  • 没有域名 怎么做网站链接营销策略有哪些理论
  • 做网站用微信收款还是支付宝关键词排名推广方法
  • 做淘宝优惠券怎么有网站微博营销策略
  • 企业网站栏目规划的重要性营业推广的概念
  • 建筑公司需求发布网站专业网站快速