亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

android - 安卓實現類似QQ剛換聊天背景的功能

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

問題描述

自己實現了一下,但對于一些手機一設置背景就出現閃退不知道為什么,大體思路就是獲得用戶選擇的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所在的目錄,然后設置其為背景,下次啟動直接加載圖片,免得此圖片在圖庫中被刪除。

標簽: qq
相關文章:
主站蜘蛛池模板: 免费看欧美一级特黄a毛片 免费看欧美一级特黄α大片 | 成人午夜精品久久不卡 | 国产无套在线播放 | 精品哟啊呦v视频在线观看 精品哟哟国产在线观看 | 中国一级毛片欧美一级毛片 | 国产欧美一区二区三区视频 | 91精品乱码一区二区三区 | 麻豆国产精品入口免费观看 | 免费a级在线观看完整片 | 国产精品久久精品 | 一级一片在线播放在线观看 | 日本特交大片免费观看 | 亚洲综合色丁香麻豆 | 最新国产精品自拍 | 爱爱动态视频免费 | 精品牛牛影视久久精品 | 国产精品你懂的在线播放 | a成人在线| 欧美aaa级片 | 欧美亚洲黄色 | 久久97久久99久久综合 | 久久精品国产久精国产果冻传媒 | 国产精品亚洲国产三区 | 免费黄色大片网站 | 日韩欧美在线视频 | 免费在线观看一级片 | 国产拍视频 | 国产亚洲欧美另类一区二区三区 | 欧美人妖xxx| 国产成人精品美女在线 | 欧美成人免费毛片 | 欧美一级黄色录像片 | 国产无套乱子伦精彩是白视频 | 91精品国产综合久久欧美 | 亚洲国产成人在线观看 | 看黄子片免费 | 精品亚洲视频在线 | 国产合集福利视频在线视频 | 一区二区三区免费 | a一级免费| 免费在线观看黄色小视频 |