发布网友 发布时间:2022-04-25 04:04
共1个回答
热心网友 时间:2023-10-24 18:49
// 从本地选择图片(比如按钮触发该事件,调取系统相册) if (Environment.getExternalStorageState().equals( Environment.MEDIA_MOUNTED)) { Intent getImageIntent = new Intent(Intent.ACTION_GET_CONTENT); getImageIntent.addCategory(Intent.CATEGORY_OPENABLE); getImageIntent.setType("image/jpeg"); startActivityForResult(getImageIntent, 4); } else { Toast.makeText(getApplicationContext(), "SD卡不可用", 0).show(); } // 本地选择 结果在activity的onactivityresult()中写:if (data != null) {//判空 Uri uri = data.getData(); uri.getPath().toString(); ContentResolver cr = this.getContentResolver(); try { Bitmap bmp;//压缩 BitmapFactory.Options option = new BitmapFactory.Options(); option.inJustDecodeBounds = true; option.inPurgeable = true; option.inInputShareable = true; option.inSampleSize = 2; option.inJustDecodeBounds = false; bmp = BitmapFactory.decodeStream(cr.openInputStream(uri), null, option);//imageView_userLogo(view控件设置背景) imageView_userLogo.setImageBitmap(BmpToRound .toRoundBitmap(bmp)); File file = SaveBitmapToSdCard.saveMyBitmap( getApplicationContext(), "" + System.currentTimeMillis(), bmp); startUpLoad(file); } catch (FileNotFoundException e) { e.printStackTrace(); } }