我正在尝试将图片导入到我的应用中ImageButton
。当用户点击它时,将转到图库并选择照片。但是,在从图库或外部存储中选择图像后,它不会出现。
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"/>
答案 0 :(得分:0)
我认为您提到的按钮是您在上面显示的图像按钮。如果是这种情况,则需要为该按钮实现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);
如果您有任何其他问题,请告诉我。
答案 1 :(得分:0)
尝试以下方法:
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>