gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 13:26:39 26 4
gpt4 key购买 nike

关于这个有很多问题,但我正处于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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com