安卓开发之获取本地图片并设置为应用背景图片

来源:互联网 发布:mdzz什么意思网络用语 编辑:程序博客网 时间:2024/04/30 03:21

设置应用背景:

***.java中

RelativeLayout layout = (RelativeLayout)findViewById(R.id.layout1);

layout.setBackgroundResource(R.drawable.img);//图片

layout.setBackgroundColor(R.color.white);//颜色

layout.setBackground(drawable);

**.xml中

android:background="@drawable/img";

android:background="@color/white";

获取手机本地图片并设置为应用背景图片:

首先添加权限:无论是拍照还是从相册中选择图片都涉及到用户的隐私,所以我们需要声明权限,

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>

Intent intent = new Intent(Intent.ACTION_PICK);              intent.setType("image/*");              startActivityForResult(intent, 1); 


@Override      protected void onActivityResult(int requestCode, int resultCode, Intent data) {          super.onActivityResult(requestCode, resultCode, data);          if (requestCode == 1&& resultCode == Activity.RESULT_OK                  && data != null)         {              Uri selectedImage = data.getData();//返回的是uri                                     String [] filePathColumn = {MediaStore.Images.Media.DATA};              Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);              cursor.moveToFirst();              int columnIndex = cursor.getColumnIndex(filePathColumn[0]);              String path = cursor.getString(columnIndex);                            Bitmap bitmap = BitmapFactory.decodeFile(path);              RelativeLayout layout1 = (RelativeLayout)findViewById(R.id.layout1);                     Drawable drawable =new BitmapDrawable(bitmap);            layout1.setBackground(drawable);        }      }  



0 0
原创粉丝点击