gpt4 book ai didi

java - 在 Android 中使用图库图像更改图像按钮

转载 作者:太空宇宙 更新时间:2023-11-04 14:35:41 25 4
gpt4 key购买 nike

我已经四处寻找解决我遇到的以下问题的方法,但没有找到任何东西。在应用程序中,我有一个带有预定义可绘制对象的 ImageButton。我的目的是让用户能够单击此图像按钮,从手机的图像库中选择一张图片并显示该图片,直到用户再次更改它。到目前为止,我设法显示 ImageGallery 并能够选择图片,但我无法执行以下操作:

1. Show the Picture from the Picture Gallery in the shape of the original drawable -- it is a circle
2. When I change activity and come back to this activity, the picture chosen is not there anymore.

我的代码如下所示:ImageButton 的 XML 位于 LinearLayout 内

        <ImageButton 
android:id="@+id/AddPic"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.4"
android:background="#00FFFFFF"
android:contentDescription="@string/MyInformation"
android:gravity="left"
android:onClick="AddPic"
android:src="@drawable/dp_holder_large"
/>

我的 Java 代码如下所示:

    public class MyInformation extends Activity{

ImageButton imgButton;
public static int RESULT_LOAD_IMAGE = 1;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.myinformation);


//Adding the picture bit

imgButton = (ImageButton) findViewById(R.id.AddPic);
imgButton.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);
imgButton.setImageBitmap(BitmapFactory.decodeFile(picturePath));
Toast.makeText(getApplicationContext(), picturePath, Toast.LENGTH_SHORT).show();

}

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}

谢谢!

最佳答案

问题 1 的解决方案:

删除背景并制作带有圆圈的自定义背景。

不要使用android:src="@drawable/dp_holder_large"

因为一旦你给它设置了图像,那么圆圈就会消失

问题2的解决方案:

您需要使用startActivityForResult()方法调用新 Activity

Intent newIntent = new Intent(CurrentActivity.this, NextActivity.class);
startActivityForResult(newIntent, 2);

并在您的 onActivityResult() 方法中执行

if(resultCode==2){
imgButton.setImageBitmap(BitmapFactory.decodeFile(picturePath));//where picturePath is the path
}

关于java - 在 Android 中使用图库图像更改图像按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25610507/

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