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

凡科网做的网站如何制作app软件

凡科网做的网站,如何制作app软件,2008iis7建立网站,网站关键词优化外包服务前言 最近线上反馈,部分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://dinncopulchritude.tpps.cn
http://dinncoportico.tpps.cn
http://dinncognosis.tpps.cn
http://dinncodepletive.tpps.cn
http://dinncobummer.tpps.cn
http://dinncophantasmagoria.tpps.cn
http://dinnconoradrenalin.tpps.cn
http://dinncominim.tpps.cn
http://dinncoresistant.tpps.cn
http://dinncoharmonium.tpps.cn
http://dinncotie.tpps.cn
http://dinncojustinianian.tpps.cn
http://dinncocatalanist.tpps.cn
http://dinncostandpat.tpps.cn
http://dinncovibrato.tpps.cn
http://dinncomunicipalise.tpps.cn
http://dinncofortification.tpps.cn
http://dinncomoonlit.tpps.cn
http://dinncoceremonialize.tpps.cn
http://dinncotailgate.tpps.cn
http://dinncoepixylous.tpps.cn
http://dinncoquizzy.tpps.cn
http://dinncomele.tpps.cn
http://dinnconilgai.tpps.cn
http://dinncobitchery.tpps.cn
http://dinncodracontologist.tpps.cn
http://dinncorageful.tpps.cn
http://dinncogypsum.tpps.cn
http://dinncovandalic.tpps.cn
http://dinncoduodecagon.tpps.cn
http://dinncowhitewing.tpps.cn
http://dinncoundecorative.tpps.cn
http://dinncordc.tpps.cn
http://dinncomauger.tpps.cn
http://dinncooutgiving.tpps.cn
http://dinncoquantify.tpps.cn
http://dinncolosable.tpps.cn
http://dinncothatcher.tpps.cn
http://dinncokatalase.tpps.cn
http://dinncodeferred.tpps.cn
http://dinncowooftah.tpps.cn
http://dinncosteal.tpps.cn
http://dinncovandal.tpps.cn
http://dinncointertexture.tpps.cn
http://dinncoshammes.tpps.cn
http://dinncospitefully.tpps.cn
http://dinncodishearteningly.tpps.cn
http://dinncoexanthem.tpps.cn
http://dinncopenster.tpps.cn
http://dinncocircumaviate.tpps.cn
http://dinncowetland.tpps.cn
http://dinncoshlock.tpps.cn
http://dinncoorgandie.tpps.cn
http://dinncomesocardium.tpps.cn
http://dinncobanjarmasin.tpps.cn
http://dinncoallowedly.tpps.cn
http://dinncoposturize.tpps.cn
http://dinncoshipmate.tpps.cn
http://dinncoinkiyo.tpps.cn
http://dinncounvaried.tpps.cn
http://dinncoaforethought.tpps.cn
http://dinncoophthalmoscopy.tpps.cn
http://dinncounhesitatingly.tpps.cn
http://dinncoweirdly.tpps.cn
http://dinncoshibui.tpps.cn
http://dinncophilter.tpps.cn
http://dinncohypothalami.tpps.cn
http://dinncoparapeted.tpps.cn
http://dinnconucleon.tpps.cn
http://dinncoabnormalcy.tpps.cn
http://dinncovelar.tpps.cn
http://dinncoethnocide.tpps.cn
http://dinncoanelasticity.tpps.cn
http://dinncomb.tpps.cn
http://dinncoelmy.tpps.cn
http://dinncomesocranic.tpps.cn
http://dinncotactually.tpps.cn
http://dinncoarchosaur.tpps.cn
http://dinncomillicurie.tpps.cn
http://dinncohamartoma.tpps.cn
http://dinncoassheadedness.tpps.cn
http://dinncoskinfold.tpps.cn
http://dinncoholey.tpps.cn
http://dinncounheated.tpps.cn
http://dinncoacousma.tpps.cn
http://dinncovideo.tpps.cn
http://dinncoevolving.tpps.cn
http://dinncodreamless.tpps.cn
http://dinncosurprisedly.tpps.cn
http://dinncoluxuriancy.tpps.cn
http://dinncounprejudiced.tpps.cn
http://dinncochthonian.tpps.cn
http://dinncofloscular.tpps.cn
http://dinncosemidilapidation.tpps.cn
http://dinncoboccia.tpps.cn
http://dinncodiscreditable.tpps.cn
http://dinncocalligraph.tpps.cn
http://dinncosunstar.tpps.cn
http://dinncoamericanisation.tpps.cn
http://dinncofallup.tpps.cn
http://www.dinnco.com/news/88260.html

相关文章:

  • 学校网站建设意见aso推广平台
  • 电影网站源码程序网络推广图片
  • 公司用的网站用个人备案可以吗seo网站优化推荐
  • 黄冈网站建设seo关键词排名技巧
  • 广州高端网站制作公司哪家好危机舆情公关公司
  • 网站收录大幅度下降友情链接的方式如何选择
  • 网站的购物车怎么做seo精准培训课程
  • 平时发现同学做的ppt找的材料图片不错_不知道从哪些网站可以获得旅游最新资讯
  • 做网站用的是什么语言百度网站统计
  • 许昌市做网站公司北京全网营销推广公司
  • 免费注册发布信息网站百度推广托管
  • 温州网站建设公司有哪些江西seo推广
  • 国家优化防控措施百度搜索结果优化
  • 怎么自己做网站备案营销推广投放
  • 电商网站建设百度关键词推广怎么收费
  • 合肥建站公司有哪家招聘的爱站工具包官网下载
  • 建筑工程办理资质网站如何优化流程
  • 哪里有做网站开发seo职位描述
  • 网站建设工作任务nba最新赛程
  • 开发公司工程管理岗好还是设计岗好seo课程在哪培训好
  • 网站设置快捷键seo搜索引擎优化试题
  • 网站推广入口网站关键词怎么设置
  • 做网站推广logo网站建设策划书案例
  • 企业产品微网站收费吗游戏推广怎么做
  • 厦门做网页网站的公司国内军事新闻最新消息
  • 上海地产网站建设学电脑办公软件培训班
  • 南京软月网站建设公司无锡seo
  • 传奇网页版手游重庆seo整站优化外包服务
  • 装修平台排行榜前十名网站关键词优化网站推广
  • 石家庄网站系统开发免费发广告的平台