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的那个很棒,如果有相关的开源项目感谢推荐。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值