android - 安卓实现类似QQ刚换聊天背景的功能
问题描述
自己实现了一下,但对于一些手机一设置背景就出现闪退不知道为什么,大体思路就是获得用户选择的uri,然后如果屏幕分辨率小于图片的分辨率就对图片进行下处理,防止OOM。但现在不知道哪里还有问题
启动系统的图片选择
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);intent.setType('image/*');CourseFragment.getInstannce().startActivityForResult(intent,1);
里边有检查权限,安卓6.0权限得经用户同意读内存,还有就是我把图片以字符串的形式保存下来了,使得下次启动直接显示保存的图片(不知道有没有更好的方法,感觉这个方法很不正规)。
@Override public void onActivityResult(int requestCode, int resultCode, Intent data) {super.onActivityResult(requestCode, resultCode, data);if(requestCode==1&&resultCode==RESULT_OK&&data!=null) { Uri uri = data.getData(); int weight= CourseBackground.getHeight(); int height=CourseBackground.getWidth(); Bitmap photo=getSmallBitmap(getRealPathFromURI(uri),weight,height); if(photo==null) {OwnToast.Long('请检查存储权限是否开启');return; } BitmapDrawable bd=new BitmapDrawable(getResources(),photo); if(bd==null) {OwnToast.Long('请检查存储权限是否开启');return; } ByteArrayOutputStream stream = new ByteArrayOutputStream(); photo.compress(Bitmap.CompressFormat.JPEG, 60, stream); byte[] b = stream.toByteArray(); // 将图片流以字符串形式存储下来 String tp = new String(Base64Encoder.encode(b)); InformationShared.setString('course_background', tp); CourseBackground.setBackground(bd);} }
根据URI去返回真实路径,网上找的代码,似乎这里有问题,在错误统计里看到这里的报错
private String getRealPathFromURI(Uri contentURI) {String result;Cursor cursor = getActivity().getContentResolver().query(contentURI, null, null, null, null);if (cursor == null) { // Source is Dropbox or other similar local file path result = contentURI.getPath();} else { cursor.moveToFirst(); int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA); result = cursor.getString(idx); cursor.close();}return result; }
1273 return result那一行
org.pointstone.cugapp.fragments.CourseFragment.getRealPathFromURI(CourseFragment.java:1273)
然后是得到压缩的图片函数
public Bitmap getSmallBitmap(String filepath, int reqWidth, int reqHeight) {// 第一次解析将inJustDecodeBounds设置为true,来获取图片大小final BitmapFactory.Options options = new BitmapFactory.Options();options.inJustDecodeBounds = true;BitmapFactory.decodeFile(filepath,options);// 调用上面定义的方法计算inSampleSize值options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);// 使用获取到的inSampleSize值再次解析图片options.inJustDecodeBounds = false;return BitmapFactory.decodeFile(filepath, options); } private String getRealPathFromURI(Uri contentURI) {String result;Cursor cursor = getActivity().getContentResolver().query(contentURI, null, null, null, null);if (cursor == null) { // Source is Dropbox or other similar local file path result = contentURI.getPath();} else { cursor.moveToFirst(); int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA); result = cursor.getString(idx); cursor.close();}return result; }
不知道哪里有问题,请大家帮忙看看,有什么建议告诉我,就是一个LinearLayout然后调用setBackground设置背景图。感觉QQ的那个很棒,如果有相关的开源项目感谢推荐。
问题解答
回答1:getRealPathFromURI这个方法,在Android4.4有变化,所以你需要是用新的.你可以看一下这篇博客 http://blog.csdn.net/dj0379/a...
回答2:把图片以字符串的形式保存下来的方法确实很独出心裁啊。我的想法是在选择图片之后,将图片处理后复制一份到App所在的目录,然后设置其为背景,下次启动直接加载图片,免得此图片在图库中被删除。
相关文章:
1. android - 安卓微信刷卡支付的DEMO2. Linux下用Vim写Python程序时「查询Python的帮助内容,即Python-doc」能做到如「vim-doc」那样吗?3. javascript - js对象 属性的访问和创建4. update方法不能更新字段值为0的数据5. javascript - 安装vue-cli一直卡在这个状态该怎么解决呢6. javascript - 关于定时器 与 防止连续点击 问题7. java - Win10下 idea的编码设置成了UTF-8,但是返回时乱码显示,哪里有问题??8. python爬虫如何批量爬取糗事百科段子9. html - css 英文段落换行,单词截断10. python - (初学者)代码运行不起来,求指导,谢谢!

网公网安备