android - 从图库或相机中选择图像并用所选图像替换按钮

标签 android

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

我在以下方面遇到错误: btnOpenGalery.setBackground(BitmapFactory.decodeFile(picturePath));

错误信息:

The method setBackground(Drawable) in the type View is not applicable for the arguments (Bitmap)

当前调试代码:

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" />

最佳答案

在您的Button 上调用setImageBitmap

myButton.setImageBitmap(myBitmap);

关于android - 从图库或相机中选择图像并用所选图像替换按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20618766/

相关文章:

android - Proguard 规则 -keep vs -keepclassmembers vs -keepclasseswithmembers

java - 修复 ImageLoader : "Try to initialize ImageLoader which had already been initialized before" 的警告

java - 为什么在 Google Playstore 上发布应用程序后,数据无法从 Firebase 加载到应用程序中?

android - 嵌套坐标布局和fitsSystemWindows重叠状态栏

带有 DOM 的 Android 解析 xml - 相同命名的标签

android - 如何以 MVP 模式将上下文传递给存储库

java - ImageView.setImageDrawable 在 LANDSCAPE 模式下不起作用

java - 如何在 TextView 中最后一次更改后的某个时间调用函数?

android - "Cannot resolve class android.support.v4.widget.DrawerLayout"即使在 gradle 中添加依赖项之后

android - 为什么在调用 sendEmailVerification() 方法(Firebase Auth 的方法)时对象没有插入到 firebase 数据库中?