2014-09-01 90 views
1

我環顧四周尋找解決方案,以解決我遇到的以下問題,但沒有找到任何東西。 在應用程序中,我有一個帶有預定義繪圖的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; 
    } 

} 

謝謝!

+0

可以解決你的第二個問題。但無法理解你在第一個問題上的含義? – Nabin 2014-09-01 17:24:55

+0

啊!好的,道歉不清楚。在用戶選擇圖片之前顯示的可繪圖是黑色圓圈。我希望用戶選擇的圖片適合這個圈子......這是否更好地解釋了它?謝謝 – user3079872 2014-09-01 17:26:58

+0

請參考下面的答案,如果您有任何問題,請告訴我。祝您好運 – Nabin 2014-09-01 17:38:18

回答

0

解決方案問題1:

移除背景,並與圈自定義背景。

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

因爲一旦你設置圖像,然後圓圈就會消失

求解問題2:

你需要調用新的活動,startActivityForResult()方法

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 
} 
+0

感謝您的快速回復。我拿出了android:src行並用作自定義圓圈的背景。不幸的是,它沒有任何區別,因爲所選圖像顯示爲矩形。在問題2的解決方案,我不知道該怎麼辦... – user3079872 2014-09-01 19:46:44

+0

感謝您的更新代碼。問題不在於我無法選擇圖片。我可以選擇圖片並出現在圖片按鈕中,但它不會保存,所以如果我關閉應用程序或我移動到新的活動並回來,圖片就不會出現。我想我可以在SharedPreferences中保存圖像的路徑,並在調用活動時調用它,但我不知道如何使用圖像按鈕來完成此操作。這是否更好解釋?再次感謝。 – user3079872 2014-09-02 08:46:34

+0

因此,如果我幫助您在SharedPreference中保存** picturePath **並在需要時取回,您將能夠解決您的問題? – Nabin 2014-09-03 00:09:48