[英]Can't load an image from gallery
I'm trying to insert an image from gallery in an ImageView but I only watch blank. 我正在尝试从ImageView中的图库中插入图像,但我只看空白。 There aren't errors. 没有错误。
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();
}
};
}
}
I also try another way but the result was the same. 我也尝试另一种方法,但结果是相同的。 The other way is: 另一种方法是:
@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();
}
}
How can I watch the image selected from the gallery? 如何观看从图库中选择的图像?
Thanks. 谢谢。
Maybe you should check if the Result of the pick image activity is acceptable, aka RESULT_OK: 也许您应该检查选择图像活动的结果是否可接受,也称为RESULT_OK:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//super.onActivityResult(requestCode, resultCode, data);
if ( resultCode == RESULT_OK && requestCode == SELECT_FOTO ) {
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(
selectedImage, filePathColumn, null, null, null);
if (cursor != null) {
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String filePath = cursor.getString(columnIndex);
cursor.close();
Bitmap bitmap = getImageFromPath(filePath, 512, 512);
}
}
}
And here are the methods I use to decode my bitmaps, you send the required dimensions of your bitmap so you don't waste unnecessary memory: 这是我用来解码位图的方法,您可以发送所需的位图尺寸,这样就不会浪费不必要的内存:
private int calculateInSampleSize(
BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
// Calculate ratios of height and width to requested height and width
final int heightRatio = Math.round((float) height / (float) reqHeight);
final int widthRatio = Math.round((float) width / (float) reqWidth);
// Choose the smallest ratio as inSampleSize value, this will guarantee
// a final image with both dimensions larger than or equal to the
// requested height and width.
inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
}
return inSampleSize;
}
private Bitmap getImageFromPath(String filePath,int reqWidth, int reqHeight) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(filePath, options);
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
options.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(filePath, options);
}
You can directly set an image using its URI and setImageURI : 您可以使用URI和setImageURI直接设置图像:
Uri imgselect = data.getData();
try
{
imvfto.setImageURI(imgselect );
}
Then, to adjust the image, you should try different values of ScaleType with setScaleType . 然后,要调整图像,应使用setScaleType尝试不同的ScaleType值。 Here are some example : http://etcodehome.blogspot.fr/2011/05/android-imageview-scaletype-samples.html 以下是一些示例: http : //etcodehome.blogspot.fr/2011/05/android-imageview-scaletype-samples.html
Also have a look the the xml property ' android:adjustViewBounds ="true"' for your ImageView to keep the original aspect ratio. 还可以查看ImageView的xml属性' android:adjustViewBounds =“ true”'以保持原始的宽高比。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.