我想知道是否可以从图库中的ImageButton中插入图像。我知道可以使用ImageView来实现它,但我认为也可以使用ImageButton。
代码:
imgbutton = (ImageButton) findViewById(R.id.imgButton);
EditText titlugar;
EditText desclugar;
titlugar = (EditText) findViewById(R.id.edit_titulo_lugar);
String tlugar = titlugar.getText().toString();
desclugar = (EditText) findViewById(R.id.edit_descripcion_lugar);
String dlugar = desclugar.getText().toString();
imgbutton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
final String[] items = {"Cámara", "Galería"};
AlertDialog.Builder builder = new AlertDialog.Builder(Insertarlugar.this);
builder.setTitle("Foto");
builder.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
int cdg = 0;
Intent itn = null;
switch (item)
{
case 1:
{
itn = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
cdg = SELECT_FOTO;
break;
}
case 2:
{
itn = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
cdg = CAPTURA_FOTO;
fnombre = Environment.getExternalStorageDirectory() + "/Foto.jpg";
Uri fichero = Uri.fromFile(new File(fnombre));
itn.putExtra(MediaStore.EXTRA_OUTPUT, fichero);
break;
}
}
startActivityForResult(itn, cdg);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == CAPTURA_FOTO)
{
imgbutton.setImageBitmap((Bitmap) data.getParcelableExtra("data"));
}
if(requestCode == SELECT_FOTO)
{
imgbutton.setImageBitmap(BitmapFactory.decodeFile(fnombre));
new MediaScannerConnectionClient() {
private MediaScannerConnection msc = null; {
msc = new MediaScannerConnection(getApplicationContext(), this);
msc.connect();
}
public void onMediaScannerConnected() {
msc.scanFile(fnombre, null);
}
public void onScanCompleted(String path, Uri uri) {
msc.disconnect();
}
};
}
}
当我运行它时,我可以从图库中选择图像,但在Imagebutton的位置我会看到一个小矩形。
如何插入图像?
感谢。