自己实现了一下,但对于一些手机一设置背景就出现闪退不知道为什么,大体思路就是获得用户选择的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的那个很棒,如果有相关的开源项目感谢推荐。
The getRealPathFromURI method has changed in Android 4.4, so you need to use the new one. You can read this blog http://blog.csdn.net/dj0379/a...
The method of saving images in the form of strings is indeed very original.
My idea is that after selecting the image, process the image and copy it to the directory where the app is located, then set it as the background, and load the image directly next time you start it to avoid the image being deleted in the gallery.