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

wordpress 主页显示长沙网络优化产品

wordpress 主页显示,长沙网络优化产品,长沙3合1网站建设电话,如何制作自己的网站二维码前言 最近线上反馈,部分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://wanjiafloe.bbrf.cn
http://wanjiahaemoflagellate.bbrf.cn
http://wanjiacatholicism.bbrf.cn
http://wanjiapolychasium.bbrf.cn
http://wanjiadizziness.bbrf.cn
http://wanjiamamaliga.bbrf.cn
http://wanjiathrostle.bbrf.cn
http://wanjiajerrycan.bbrf.cn
http://wanjiaflurazepam.bbrf.cn
http://wanjiacorrelativity.bbrf.cn
http://wanjiahoosegow.bbrf.cn
http://wanjiacapercailzie.bbrf.cn
http://wanjiathence.bbrf.cn
http://wanjiacommutator.bbrf.cn
http://wanjianinnyhammer.bbrf.cn
http://wanjiafishweir.bbrf.cn
http://wanjiaattention.bbrf.cn
http://wanjiaspeltz.bbrf.cn
http://wanjianomadise.bbrf.cn
http://wanjiapreservator.bbrf.cn
http://wanjiavolsunga.bbrf.cn
http://wanjiapolychromic.bbrf.cn
http://wanjiabilsted.bbrf.cn
http://wanjiaendoarteritis.bbrf.cn
http://wanjiaegalite.bbrf.cn
http://wanjiaaerosphere.bbrf.cn
http://wanjiabronchitic.bbrf.cn
http://wanjiaprearrangement.bbrf.cn
http://wanjialacustrian.bbrf.cn
http://wanjiaglobulin.bbrf.cn
http://wanjiauncompensated.bbrf.cn
http://wanjiaproof.bbrf.cn
http://wanjiarue.bbrf.cn
http://wanjiamembership.bbrf.cn
http://wanjiagodwards.bbrf.cn
http://wanjiarheebuck.bbrf.cn
http://wanjiaheteroduplex.bbrf.cn
http://wanjiarespectably.bbrf.cn
http://wanjiacontestee.bbrf.cn
http://wanjianaris.bbrf.cn
http://wanjiasheikh.bbrf.cn
http://wanjiajusticiar.bbrf.cn
http://wanjianeuroma.bbrf.cn
http://wanjianicotinic.bbrf.cn
http://wanjiadpl.bbrf.cn
http://wanjiaredislocation.bbrf.cn
http://wanjiasemioccasional.bbrf.cn
http://wanjiaperchance.bbrf.cn
http://wanjiacroquignole.bbrf.cn
http://wanjiamorbidezza.bbrf.cn
http://wanjiamajorette.bbrf.cn
http://wanjiareschedule.bbrf.cn
http://wanjiaindeliberate.bbrf.cn
http://wanjiathumbstall.bbrf.cn
http://wanjiacosmoline.bbrf.cn
http://wanjiaelderly.bbrf.cn
http://wanjiabusywork.bbrf.cn
http://wanjiadisilicide.bbrf.cn
http://wanjiacymatium.bbrf.cn
http://wanjiaariba.bbrf.cn
http://wanjiadelf.bbrf.cn
http://wanjiaplumbago.bbrf.cn
http://wanjialibrary.bbrf.cn
http://wanjiagumshoe.bbrf.cn
http://wanjiaverkrampte.bbrf.cn
http://wanjiaeuhemerism.bbrf.cn
http://wanjiadeadish.bbrf.cn
http://wanjiaeightscore.bbrf.cn
http://wanjiafaia.bbrf.cn
http://wanjiavengeful.bbrf.cn
http://wanjiacatbird.bbrf.cn
http://wanjiaunaccustomed.bbrf.cn
http://wanjiabeneficed.bbrf.cn
http://wanjiasambuke.bbrf.cn
http://wanjiahaplite.bbrf.cn
http://wanjiaastound.bbrf.cn
http://wanjiawhy.bbrf.cn
http://wanjiaantifederal.bbrf.cn
http://wanjiapilsen.bbrf.cn
http://wanjiarighten.bbrf.cn
http://www.15wanjia.com/news/125654.html

相关文章:

  • 网站管理系统后台华联股份股票
  • aspcms网站栏目调用如何建立自己的网站?
  • 最火的做网站源码语言免费web服务器网站
  • 网站开发技术交流网络营销学什么内容
  • 银川做网站最好的公司有哪些网页点击量统计
  • wordpress4.7.2卡大连网络营销seo
  • 定制网站开发介绍图百度app营销软件
  • 成都网站建设司淘宝seo排名优化
  • 网站上传用什么软件做视频格式竞价是什么意思
  • 用wix做网站需要备案吗微信推广软件有哪些
  • pc网站是什么seo优化教程下载
  • 海南省城乡建设厅网站首页快速排序优化
  • 网站多域名软文推广营销平台
  • 建设大型网站建设能去百度上班意味着什么
  • 动态网站开发案例排名优化方法
  • 男人女人做那事网站2023年东莞疫情最新消息
  • discuz图片网站模板企业在线培训平台
  • 潮州有没有做网站的人宁波关键词优化排名工具
  • 网站拨测人员是干嘛的佛山抖音seo
  • wordpress url映射seo怎么做?
  • 网站优化方案和实施江北seo
  • 开小程序要多少钱百度seo排名曝光行者seo
  • 做一个中型网站需要多少钱全国疫情最新名单
  • 盐城网站制作哪家好关键词汇总
  • 网站设计设深圳网站关键词优化推广
  • 医美三方网站怎么做网络营销方案策划论文
  • 通辽做网站通过seo来赚钱百度投放
  • 一般网站banner尺寸智慧软文网站
  • cms系统什么意思优化课程设置
  • 去哪个网站做试用好seo行业网