/**
* 選擇圖片,2種實(shí)現(xiàn)方式
*/
public void pickBackgroundPicture() {
Logutil.e("pickBackgroundPicture");
// 只顯示拍的照片和擴(kuò)展存儲(chǔ)根目錄下的圖片
Intent intent = new Intent(Intent.ACTION_PICK,
MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, REQUESTCODE_LOAD_IMAGE);
// 可顯示全部圖片,但可能會(huì)彈出選擇瀏覽圖片的應(yīng)用選擇
/*Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent, RESULTCODE_LOAD_IMAGE);
*/
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case REQUESTCODE_LOAD_IMAGE:
if (resultCode == RESULT_OK && null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
Bitmap _Bitmap = null;
// Bitmap _Bitmap = BitmapFactory.decodeFile(picturePath);
try {
_Bitmap = MyUtility.getBitmapByFile(new File(picturePath));
} catch (Exception e){
e.printStackTrace();
}
if (_Bitmap == null) {
Log.d(TAG, "picturePath=" + picturePath);
break;
}
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) {
mMainlayout.setBackground(new BitmapDrawable(getResources(),
_Bitmap));
} else {
mMainlayout.setBackgroundDrawable(new BitmapDrawable(
getResources(), _Bitmap));
}
// setBackground(-1, _Bitmap, null, 2);
SharedPreferences.Editor _Editor = mSharedPreferences.edit();
_Editor.putString(KEY_SAVE_BACKGROUND, picturePath);
boolean _boolean = _Editor.commit();
if (false == _boolean) {
Log.d(TAG, "save background picture failed");
}
// cancelAlarm();
}
break;
}
}
android選擇圖片
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
相關(guān)閱讀更多精彩內(nèi)容
- 最近深入了解了關(guān)于圖片的選擇和裁剪部分的內(nèi)容,發(fā)現(xiàn)還是有很多需要注意的點(diǎn),需要及時(shí)的記錄一下。根據(jù)使用場(chǎng)景的不同,...
- 本文出自 “阿敏其人” 簡(jiǎn)書博客,轉(zhuǎn)載或引用請(qǐng)注明出處。 我們要做的是從類似qq更換頭像的功能。按下頭像,彈出po...
- 最近在做應(yīng)用的時(shí)候,遇到了需要選擇圖片上傳頭像的需求。本著代碼搬運(yùn)工的精神,在網(wǎng)上瀏覽了一圈,發(fā)現(xiàn)解決辦法都大致如...
- 膜拜大神:https://github.com/crazycodeboy/TakePhoto 安裝說明 演示 使用...