来自图库的Android图片选择问题

时间:2011-03-04 09:53:07

标签: android

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

2 个答案:

答案 0 :(得分:0)

我写了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();

此代码包装处理的案例比DB查找内容更多。它也更清洁。

这将让你获得最大的收益,甚至可能足以生产代码。我将详细介绍如何加载非常大的图像以及如何使用BitmapFactory.Options.inJustDecodeBounds处理它们的危险。

答案 1 :(得分:0)

加载位图时,必须考虑照片的方向。话虽这么说,你必须从图像文件或内容解析器获取exif信息。

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

相关问题