首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么我不能在这里将ImageBitmap设置为RelativeLayout?

为什么我不能在这里将ImageBitmap设置为RelativeLayout?
EN

Stack Overflow用户
提问于 2015-05-17 16:16:32
回答 2查看 76关注 0票数 0

我正在尝试从图片库中选择一个背景图像,并将其设置为Activity背景。但是问题就在这里,当我设置RelativeLayout而不是ImageView时,它给了我关于setImageBitmap的错误,原因是什么?下面是我的代码:我参考了本教程:http://viralpatel.net/blogs/pick-image-from-galary-android-app/,谢谢!

代码语言:javascript
复制
@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == RESULT_LOAD_IMAGE && 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();

            ImageView imageView = (ImageView) findViewById(R.id.background);
            imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));

        }
    }
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-05-17 16:27:59

这是因为RelativeLayout没有一个名为setImageBitmap的方法。引用此link,您可以使用它将其设置为相对布局:

代码语言:javascript
复制
File f = new File(getRealPathFromURI(path));  
Drawable d = Drawable.createFromPath(f.getAbsolutePath());
mRelativeLayout.setBackground(d);

private String getRealPathFromURI(Uri contentURI) {
        Cursor cursor = getContentResolver().query(contentURI, null, null, null, null);
        if (cursor == null) { // Source is Dropbox or other similar local file path
            return contentURI.getPath();
        } else { 
            cursor.moveToFirst(); 
            int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA); 
            return cursor.getString(idx); 
        }
    }
票数 1
EN

Stack Overflow用户

发布于 2015-05-17 16:25:00

你不能在setImageBitmap上做RelativeLayout。我认为您想要做的是setBackground,它接受一个可绘制的参数。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30289103

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档