从图库中选择图像并在imageview中设置相同的大小

时间:2012-11-25 08:43:12

标签: android android-gallery

我想从图库中选择图像,并在活动中的imageview中设置相同大小的图像,但我编写的代码却没有在imageview中以相同的大小显示。它显示小。如何达到相同的尺寸。

这里是imageview的设置大小

mainImageView.setLayoutParams(new LayoutParams(VirtualMirrorActivity.convertDpToPixel(270,this),VirtualMirrorActivity.convertDpToPixel(280, this)));

mainImageView.setBackgroundResource(R.layout.border);
LinearLayout linearLayout=(LinearLayout)findViewById(R.id.linearLayout);
linearLayout.addView(mainImageView);

意图从画廊中挑选图片。

Intent intent = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

startActivityForResult(Intent.createChooser(intent, "Select Picture"),10);

if (requestCode == 10 && resultCode == Activity.RESULT_OK) 
            {             
                Uri contentUri = data.getData();          
                String[] proj = { MediaStore.Images.Media.DATA };         
                Cursor cursor = managedQuery(contentUri, proj, null, null, null);         
                int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);         
                cursor.moveToFirst();         
                imageUri = Uri.parse(cursor.getString(column_index));

                InputStream stream = getContentResolver().openInputStream(data.getData());
                tempBitmap = BitmapFactory.decodeStream(stream);
                mainImageView.setImageBitmap(tempBitmap);

                if(tempBitmap!=null)
                {
                    isModel = false;
                    VMAdjustFrame.issaved = false;
                    isPhotofromAccount = false;
                    isPhotoChanged = true;
                }
            }
        }

1 个答案:

答案 0 :(得分:1)

mainImageView.setLayoutParams().height = 270 mainImageView.setLayoutParams().width= 270

图片大小

删除此内容并尝试上面的代码 mainImageView.setLayoutParams(new LayoutParams(VirtualMirrorActivity.convertDpToPixel(270,this),VirtualMirrorActivity.convertDpToPixel(280, this)));