android - 安卓实现类似QQ刚换聊天背景的功能

查看:86
本文介绍了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的那个很棒,如果有相关的开源项目感谢推荐。

解决方案

getRealPathFromURI这个方法,在Android4.4有变化,所以你需要是用新的.你可以看一下这篇博客 http://blog.csdn.net/dj0379/a...

这篇关于android - 安卓实现类似QQ刚换聊天背景的功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆