【发布时间】:2015-04-13 09:40:56
【问题描述】:
我正在尝试通过使用 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);
}
}
}
【问题讨论】:
标签: android uri background-image drawable image-gallery