我環顧四周尋找解決方案,以解決我遇到的以下問題,但沒有找到任何東西。 在應用程序中,我有一個帶有預定義繪圖的ImageButton。我的所能是用戶點擊這個ImageButton的能力,從手機的圖像庫中選擇一張圖片並顯示該圖片,直到用戶再次更改它爲止。 到目前爲止,我管理,以獲得ImageGallery顯示,並且能夠選擇一個圖片,但我沒能做到以下幾點:在Android中更改圖像按鈕與圖庫圖像
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.
我的代碼看起來是這樣的: XML的ImageButton的是一個的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;
}
}
謝謝!
可以解決你的第二個問題。但無法理解你在第一個問題上的含義? – Nabin 2014-09-01 17:24:55
啊!好的,道歉不清楚。在用戶選擇圖片之前顯示的可繪圖是黑色圓圈。我希望用戶選擇的圖片適合這個圈子......這是否更好地解釋了它?謝謝 – user3079872 2014-09-01 17:26:58
請參考下面的答案,如果您有任何問題,請告訴我。祝您好運 – Nabin 2014-09-01 17:38:18