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. html5 - iframe src可以引入其他域名或者IP吧iframe src可以是其他域名过IP吧2. javascript - 给js写的盒子添加css样式,css样式没起作用。3. javascript - 如何将psd裁切下来的图片非常清晰的宣示出来4. javascript - vuex中action应该怎么分发事件5. html5 - 请问一下写H5的时候 你们都是兼容那些手机6. html5 - ElementUI table中el-table-column怎么设置百分比显示。7. mysqli函数8. web前端页面中实现表格效果,这个表格是可编辑的9. javascript - h5页面中iframe无法唤起iOS应用?10. 数据挖掘 - 如何用python实现《多社交网络的影响力最大化问题分析》中的算法?
![php-_server-php_self - nginx $_SERVER[’PHP_SELF’] 得到重复路径是什么原因?](http://www.haobala.com/attached/image/news/202309/1123091c0c.png)
网公网安备