从图库或相机中选择图像,然后用所选图像替换按钮

时间:2013-12-16 19:03:50

标签: android

关于这个有几十个问题,但我处于android开发的学习阶段,似乎无法将这个功能实现到我的项目中。我想点击我的按钮图像,从图库/相机中选择一个图像,然后用所选图像替换按钮图像。有人可以给我一个模板或指出我正确的方向来完成这个,谢谢。

我收到错误:     btnOpenGalery.setBackground(BitmapFactory.decodeFile(picturePath));

错误讯息:

  

View类型中的方法setBackground(Drawable)不适用   参数(位图)

debuging的当前代码:

Button btnOpenGalery;

public static int RESULT_LOAD_IMAGE = 1;

 @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
   setContentView(R.layout.register);

/**
 * Defining all layout items
 **/
    inputFirstName = (EditText) findViewById(R.id.fname);
    inputLastName = (EditText) findViewById(R.id.lname);
    inputUsername = (EditText) findViewById(R.id.uname);
    inputEmail = (EditText) findViewById(R.id.email);
    inputPassword = (EditText) findViewById(R.id.pword);
    btnRegister = (Button) findViewById(R.id.register);
    registerErrorMsg = (TextView) findViewById(R.id.register_error);



/**
* Button which Switches back to the login screen on clicked
**/

    Button login = (Button) findViewById(R.id.bktologin);
    login.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent myIntent = new Intent(view.getContext(), Login.class);
            startActivityForResult(myIntent, 0);
            finish();
        }

    });




    btnOpenGalery = (Button) findViewById(R.id.profilepic);
    btnOpenGalery.setOnClickListener(new OnClickListener(){
        @Override
        public void onClick(View v) {
            Intent GaleryIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            startActivityForResult(GaleryIntent, RESULT_LOAD_IMAGE);
        }
    });
}

@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 SelectedCursor = getContentResolver().query(SelectedImage, FilePathColumn, null, null, null);
            SelectedCursor.moveToFirst();

            int columnIndex = SelectedCursor.getColumnIndex(FilePathColumn[0]);
            String picturePath = SelectedCursor.getString(columnIndex);
            SelectedCursor.close();

          //  Drawable d = new BitmapDrawable(getResources(),BitmapFactory.decodeFile(picturePath)); 
           // btnOpenGalery .setImageBitmap(d);
          btnOpenGalery.setBackground(BitmapFactory.decodeFile(picturePath));
            Toast.makeText(getApplicationContext(), picturePath, Toast.LENGTH_SHORT).show();

        }

XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/android"
android:orientation="vertical"
tools:ignore="HardcodedText,SpUsage" >


<ImageButton
    android:id="@+id/profilepic"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="36dp"
    android:background="@drawable/custombutton"
    android:contentDescription="profile picture" />

1 个答案:

答案 0 :(得分:2)

致电setImageBitmap上的Button

myButton.setImageBitmap(myBitmap);