我是 android 的新手,我正在开发一个使用图库和从图库中挑选图像的应用程序,但是我遇到了一个错误,我试图以不同的方式解决这个错误,因为
当我选择水平图像(宽度大于高度)时,我正在使用位于 SD 卡上的图库,它可以很好地在 ImageView 中加载图像,但问题在于垂直图像(高度大于宽度),它显示图像在左侧旋转的 ImageView 中(或从原始位置说 -90 度)。我已经打印了两个图像(水平和垂直)的宽度和高度,两个图像的显示宽度 = 2592 和高度 = 1936,
我真的很震惊,大约 3 周都无法解决这个问题,请任何人帮助我。
提前致谢
这是我的代码:
用于启动图库 Intent
Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
startActivityForResult(i, ACTIVITY_SELECT_IMAGE);
选择图片后:
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent)
{
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
try{ switch(requestCode) { case REQ_CODE_PICK_IMAGE: if(resultCode == RESULT_OK)
{
Uri selectedImage = imageReturnedIntent.getData(); String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
filePath = cursor.getString(columnIndex);
cursor.close();
File img = new File(filePath); Bitmap yourSelectedImage = BitmapFactory.decodeStream(new FileInputStream(img));
int imgHeight = yourSelectedImage.getHeight(); int imgWidth = yourSelectedImage.getWidth();
Toast.makeText(getApplicationContext(), "Original size: Width ="+imgWidth+" Height="+imgHeight, Toast.LENGTH_LONG).show();
float myScalingFactor = MyScaleFactor(imgHeight,imgWidth);
int destW = (int)(imgWidth*myScalingFactor);
int destH = (int)(imgHeight*myScalingFactor);
Toast.makeText(getApplicationContext(), "Scaled size: Width ="+destW+" Height="+destH + "Scale" +myScalingFactor, Toast.LENGTH_LONG).show();
yourSelectedImage = Bitmap.createScaledBitmap(yourSelectedImage, destW, destH, true);
picUpload.setImageBitmap(yourSelectedImage);
}
最佳答案
我写了一个 article对这个。它取决于我制作的库,但您可以使用文章本身中的代码获得大部分功能。这是一个总结...
启动 Activity 的正确方式。基本上,如果有人在从图库中挑选时退出应用程序,然后在 2 小时后通过启动器图标返回到应用程序。将他们带回画廊会让人迷失方向,因为他们不会理解或记得他们为什么在那里。
final Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
activity.startActivityForResult(intent, requestCode);
检索结果...
final InputStream is = context.getContentResolver().openInputStream(intent.getData());
final Bitmap imageData = BitmapFactory.decodeStream(is, null, options);
is.close();
此代码包装处理的情况比数据库查找内容处理的更多。它也更清洁。
这将使您物有所值,甚至可能足以用于生产代码。我详细介绍了加载非常大的图像的危险以及如何使用 BitmapFactory.Options.inJustDecodeBounds 处理它们。 .
关于Android 从图库中选取图像的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5192159/