美文网首页
android选择图片

android选择图片

作者: MacLi | 来源:发表于2020-04-24 10:52 被阅读0次
    /**
     * 选择图片,2种实现方式
     */
      public void pickBackgroundPicture() {
          Logutil.e("pickBackgroundPicture");
          // 只显示拍的照片和扩展存储根目录下的图片
          Intent intent = new Intent(Intent.ACTION_PICK,
                  MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
          startActivityForResult(intent, REQUESTCODE_LOAD_IMAGE);
    
          // 可显示全部图片,但可能会弹出选择浏览图片的应用选择
    /*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选择图片

          本文链接:https://www.haomeiwen.com/subject/ammxwhtx.html