gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 15:51:45 25 4
gpt4 key购买 nike

我正在尝试通过使用 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);

}
}
}

最佳答案

查看此链接 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);
}

关于android - 如何将选定的 Uri 图片保存到可绘制对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29602216/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com