av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

您的位置:首頁技術(shù)文章
文章詳情頁

android - 安卓實(shí)現(xiàn)類似QQ剛換聊天背景的功能

瀏覽:115日期:2022-10-01 16:41:23

問題描述

自己實(shí)現(xiàn)了一下,但對(duì)于一些手機(jī)一設(shè)置背景就出現(xiàn)閃退不知道為什么,大體思路就是獲得用戶選擇的uri,然后如果屏幕分辨率小于圖片的分辨率就對(duì)圖片進(jìn)行下處理,防止OOM。但現(xiàn)在不知道哪里還有問題

啟動(dòng)系統(tǒng)的圖片選擇

Intent intent = new Intent(Intent.ACTION_GET_CONTENT);intent.setType('image/*');CourseFragment.getInstannce().startActivityForResult(intent,1);

里邊有檢查權(quán)限,安卓6.0權(quán)限得經(jīng)用戶同意讀內(nèi)存,還有就是我把圖片以字符串的形式保存下來了,使得下次啟動(dòng)直接顯示保存的圖片(不知道有沒有更好的方法,感覺這個(gè)方法很不正規(guī))。

@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('請(qǐng)檢查存儲(chǔ)權(quán)限是否開啟');return; } BitmapDrawable bd=new BitmapDrawable(getResources(),photo); if(bd==null) {OwnToast.Long('請(qǐng)檢查存儲(chǔ)權(quán)限是否開啟');return; } ByteArrayOutputStream stream = new ByteArrayOutputStream(); photo.compress(Bitmap.CompressFormat.JPEG, 60, stream); byte[] b = stream.toByteArray(); // 將圖片流以字符串形式存儲(chǔ)下來 String tp = new String(Base64Encoder.encode(b)); InformationShared.setString('course_background', tp); CourseBackground.setBackground(bd);} }

根據(jù)URI去返回真實(shí)路徑,網(wǎng)上找的代碼,似乎這里有問題,在錯(cuò)誤統(tǒng)計(jì)里看到這里的報(bào)錯(cuò)

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)

然后是得到壓縮的圖片函數(shù)

public Bitmap getSmallBitmap(String filepath, int reqWidth, int reqHeight) {// 第一次解析將inJustDecodeBounds設(shè)置為true,來獲取圖片大小final BitmapFactory.Options options = new BitmapFactory.Options();options.inJustDecodeBounds = true;BitmapFactory.decodeFile(filepath,options);// 調(diào)用上面定義的方法計(jì)算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; }

不知道哪里有問題,請(qǐng)大家?guī)兔纯矗惺裁唇ㄗh告訴我,就是一個(gè)LinearLayout然后調(diào)用setBackground設(shè)置背景圖。感覺QQ的那個(gè)很棒,如果有相關(guān)的開源項(xiàng)目感謝推薦。

問題解答

回答1:

getRealPathFromURI這個(gè)方法,在Android4.4有變化,所以你需要是用新的.你可以看一下這篇博客 http://blog.csdn.net/dj0379/a...

回答2:

把圖片以字符串的形式保存下來的方法確實(shí)很獨(dú)出心裁啊。我的想法是在選擇圖片之后,將圖片處理后復(fù)制一份到App所在的目錄,然后設(shè)置其為背景,下次啟動(dòng)直接加載圖片,免得此圖片在圖庫中被刪除。

標(biāo)簽: qq
相關(guān)文章:
主站蜘蛛池模板: 一级a性色生活片久久毛片 一级特黄a大片 | 日本在线网址 | 国产精品明星裸体写真集 | 91久久久久久 | 欧美综合国产精品久久丁香 | 超碰人人做 | 瑞克和莫蒂第五季在线观看 | 日韩久久久久 | 亚洲欧美视频 | 国产精品亚洲综合 | 久久国产区 | 91社区在线观看播放 | 久草福利 | 欧美日韩一区在线 | 武道仙尊动漫在线观看 | 日韩精品网站 | 中文字幕一区在线观看视频 | 日日摸日日碰夜夜爽2015电影 | 黄色一级视频 | 97视频在线观看网站 | 久久成人高清视频 | 日韩精品1区2区3区 成人黄页在线观看 | 精品国产一二三区 | 日韩中文字幕在线观看 | 日韩高清在线观看 | 欧美白人做受xxxx视频 | 久久夜视频 | 国产精品久久久久久久久久妞妞 | 久久久久亚洲精品中文字幕 | 91精品一区| 自拍偷拍亚洲欧美 | 国产一在线 | 国产分类视频 | 成人不卡一区二区 | 91精品国产综合久久精品 | 欧美一级三级在线观看 | 亚洲喷水 | 欧美福利专区 | 免费的av网站 | 成人在线免费网站 | 国产九九精品 |