【问题标题】:Changing Image Button with a Gallery Image in Android在 Android 中使用图库图像更改图像按钮
【发布时间】:2014-09-01 17:18:53
【问题描述】:

我已经四处寻找解决我遇到的以下问题的方法,但没有找到任何东西。 在应用程序中,我有一个带有预定义可绘制对象的 ImageButton。我的功能是让用户单击此 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;
     }

}

谢谢!

【问题讨论】:

  • 可以解决你的第二个问题。但无法理解您在第一个问题中的意思?
  • 啊!好的,抱歉,不清楚。在用户选择图片之前显示的可绘制对象是一个黑色圆圈。我希望用户选择的图片“适合”这个圈子......这能更好地解释它吗?谢谢
  • 请参考下面我的回答,如果您有任何问题,请告诉我。祝你好运

标签: java android image imagebutton android-imagebutton


【解决方案1】:

问题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
}

【讨论】:

  • 感谢您的快速回复。我取出了 android:src 线并用作背景自定义圆圈。不幸的是,它没有任何区别,因为选择的图像显示为矩形。关于问题2的解决方案,我不知道该怎么办......
  • 感谢更新代码。问题不在于我无法选择图片。我可以选择图片并显示在图像按钮中,但它不会保存,因此如果我关闭应用程序或移动到新活动并返回,图像不会出现。我想我可以将图像的路径保存在 SharedPreferences 中,然后在调用活动时调用它,但我不知道如何使用图像按钮执行此操作。这解释得更好吗?再次感谢。
  • 那么如果我帮助您将 picturePath 保存在 SharedPreference 中并在需要时取回,您将能够解决您的问题吗?
  • 嗨,我实际上自己解决了第一个问题,我很高兴,因为我对此很陌生。我读到最好将路径存储在共享首选项中,然后检索路径。这行代码确保我检索到了路径:'File imgFile = new File(sharedpreferences.getString(Path, LOCATION_SERVICE));'
猜你喜欢
  • 2011-06-23
  • 1970-01-01
  • 1970-01-01
  • 2013-09-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-25
  • 1970-01-01
相关资源
最近更新 更多