【问题标题】:Android image picking issue from gallery从图库中选择 Android 图像问题
【发布时间】:2011-03-04 09:53:07
【问题描述】:

我是 android 新手,我正在开发一个使用图库和从图库中挑选图片的应用程序,但我遇到了一个错误,我试图以不同的方式解决,因为

当我选择水平图像(宽度大于高度)时,我正在使用位于 SD 卡上的图库,它在图像视图中加载图像非常好,但问题在于垂直图像(高度大于宽度)它显示图像在左侧旋转的图像视图中(或从原始位置说 -90 度)。我已经打印了两个图像的宽度和高度(水平和垂直),两个图像的显示宽度 = 2592 和高度 = 1936,

我真的很震惊,大约 3 周都无法解决它,请任何人帮助我。

提前致谢

这是我的代码:

用于启动画廊意图

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);
}

【问题讨论】:

标签: android


【解决方案1】:

我为此写了一封article。这取决于我制作的库,但您可以使用文章本身中的代码获得大部分功能。这是一个摘要...

启动活动的正确方式。基本上,如果有人在从图库中挑选时退出应用程序,然后在 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 处理它们。

【讨论】:

    【解决方案2】:

    加载位图时,必须考虑照片的方向。话虽如此,您必须从图像文件或通过内容解析器获取 exif 信息。

    您可以查看https://gist.github.com/Mariovc/f06e70ebe8ca52fbbbe2 中所述的旋转图像部分,或者查看我的库droid-image-picker 以选择/裁剪图像。

    【讨论】:

      猜你喜欢
      • 2011-07-15
      • 2016-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多