2014-09-01 49 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