Search code examples
javaandroidimageandroid-imagebutton

Import Image to ImageButton


I'm trying to import image to my app into ImageButton. When the user clicks on it will go to the Gallery and select the photo. But it doesn't appear after I select the image from the Gallery or external storage.

xml code

<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 code

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

I didn't forget the permissions:

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

Solution

  • Try the following:

    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>