我正在尝试在 ImageView 中插入图库中的图像,但我只看空白。没有错误。
protected static final int RESULT_LOAD_IMAGE = 1;
ImageView imgfto;
private static int CAPTURA_FOTO = 2;
private static int SELECT_FOTO = 1;
private String fnombre = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_insertarlugar);
imgfto = (ImageView) findViewById(R.id.imgFoto);
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();
imgfto.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);
Toast.makeText(Insertarlugar.this, "Click\n" + item,
Toast.LENGTH_SHORT).show();
}
});
AlertDialog alert = builder.create();
alert.show();
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == CAPTURA_FOTO)
{
Uri imgselect = data.getData();
String imgpath = imgselect.getPath();
File f = new File (imgpath);
Bitmap bm = BitmapFactory.decodeFile(f.getAbsolutePath());
ImageView imvfto = (ImageView) findViewById(R.id.imgFoto);
imvfto.setImageBitmap(bm);
}
if(requestCode == SELECT_FOTO)
{
imgfto.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();
}
};
}
}
我也尝试了另一种方法,但结果是一样的。另一种方式是:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == CAPTURA_FOTO)
{
Uri imgselect = data.getData();
InputStream is;
try
{
is = getContentResolver().openInputStream(imgselect);
BufferedInputStream bis = new BufferedInputStream(is);
Bitmap bmap = BitmapFactory.decodeStream(bis);
ImageView imvfto = (ImageView)findViewById(R.id.imgFoto);
imvfto.setImageBitmap(bmap);
}
catch (FileNotFoundException e)
{
Toast.makeText(Insertarlugar.this, "Error al cargar la
imagen", Toast.LENGTH_SHORT).show();
}
}
如何观看从图库中选择的图像?
谢谢。