如何将选定的 Uri 图片保存到可绘制对象?

How to save a selected Uri pic to drawable?

我正在尝试通过使用 Uri 将用户从他们的图库中选择的图片设置为应用程序的背景,但我不太明白。我尝试做的一件事是直接将背景设置为 uri,但它未能做到兼容性不匹配。我该如何做到这一点,以编程方式设置可绘制对象或任何其他方式?

这是我试过的

 protected void onActivityResult(int requestCode, int resultCode, Intent intent) {

    super.onActivityResult(requestCode, resultCode, intent);
    if (requestCode == 1) {

        if (intent != null && resultCode == RESULT_OK) {

            Uri selectedImage = intent.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 filePath = cursor.getString(columnIndex);
            cursor.close();

            if (bmp != null && !bmp.isRecycled()) {
                bmp = null;
            }

            bmp = BitmapFactory.decodeFile(filePath);
            imageView.setBackground(selectedImage);//error here

            //imageView.setBackgroundResource(0);//originally this, but this crashes also
            imageView.setImageBitmap(bmp);

        }
    }
}

看看这个 link Retrieve drawable resource from Uri

     try {
            InputStream inputStream = getContentResolver().openInputStream(yourUri);
            yourDrawable = Drawable.createFromStream(inputStream, yourUri.toString() );
            imageView.setImageDrawable(yourDrawable);
        } catch (FileNotFoundException e) {
            yourDrawable = getResources().getDrawable(R.drawable.default_image);
        }