将图像导入 ImageButton

Import Image to ImageButton

我正在尝试将图像导入我的应用程序 ImageButton。当用户单击它时,将转到图库和 select 照片。但是在我 select 来自图库或外部存储的图像后它没有出现。

xml代码

<ImageButton
    android:id="@+id/foodimbutton"
    android:layout_width="match_parent"
    android:layout_height="300dp"
    android:onClick="ibC"
/>


<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/itemName"
    android:hint="enter the item Name"
 />

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/itemDesc"
    android:hint="enter the item Descrption"
/>

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/itemPrice"
    android:hint="enter the item Price"
/>

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Add the Item"
    android:onClick="AddtheItemButtonClcied"
/>

java代码

public class AddFood extends AppCompatActivity {

    ImageButton imageButton;
    Intent galleryIntent;
    private static final int GALLREQ = 1;
    private EditText name,desc,price;
    private Uri uri = null;

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

        name = (EditText)findViewById(R.id.itemName);
        desc = (EditText)findViewById(R.id.itemDesc);
        price = (EditText)findViewById(R.id.itemPrice);
    }

    public void ibC (View view) {
        galleryIntent = new Intent(Intent.ACTION_GET_CONTENT);
        galleryIntent.setType("Image/*");
        startActivityForResult(galleryIntent,GALLREQ);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data){
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == GALLREQ && resultCode == RESULT_OK ) {
            uri = data.getData();
            imageButton = (ImageButton)findViewById(R.id.foodimbutton);
            imageButton.setImageURI(uri);
        }
    }
}

AndroidManifest

我没有忘记权限:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

我想你提到的按钮就是你上面显示的图像按钮。如果是这种情况,您需要为该按钮实现 onClickListener()。那就是您通过传递该视图来调用您的方法的地方。网上有很多关于如何实现它的例子。这是一个示例,

// Create an anonymous implementation of OnClickListener
private OnClickListener myListener = new OnClickListener() {
    public void onClick(View v) {
       ibC(v);
    }
};
//call this inside your onCreate() method
imageButton.setOnClickListener(myListener);

如果您有任何其他问题,请告诉我。

尝试以下操作:

public class Demo2 extends AppCompatActivity {

private ImageButton ib;
private final int GALLREQ = 1;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.demo4);
}

public void ibC (View view) {

    Intent galleryIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    startActivityForResult(galleryIntent , GALLREQ);

}


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == GALLREQ && resultCode == RESULT_OK) {
        Uri uri = data.getData();
        ib = (ImageButton) findViewById(R.id.ib);
        Log.e("Uri", "*******" + uri.getPath());
        ib.setImageURI(uri);
    }

}
}

demo4.xml:------

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageButton
    android:layout_width="match_parent"
    android:layout_height="400dp"
    android:id="@+id/ib"
    android:onClick="ibC"/>

</android.support.constraint.ConstraintLayout>