android - 为什么使用 ImageLoader 显示的肖像图像会旋转?

标签 android gridview rotation orientation universal-image-loader

我正在尝试使用 ImageAdapter 和 ImageLoader 将图像库显示为 GridView。 不过,所有肖像图像都会旋转 +-90 度。 我知道 getExifOrientation 代码来检查图像方向并将其旋转回来,但我根本不知道在哪里使用它..

这是我使用的 ImageAdapter 类,在这里获取了大部分内容并尝试正确使用它。

public class ImageAdapter extends BaseAdapter {

    ArrayList<String> _list;
    LayoutInflater _inflater;
    Context _context;

    public ImageAdapter(Context context, ArrayList<String> imageList) {

        _context = context;
        _inflater = LayoutInflater.from(_context);
        _list = new ArrayList<String>();
        this._list = imageList;
    }

    @Override
    public int getCount() {
        return _imageUrls.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if(convertView == null) {
            convertView = _inflater.inflate(R.layout.multiphoto_item, null);
        }

        final ImageView imageView = (ImageView) convertView.findViewById(R.id.image_view);
        imageView.setTag(position);
        imageView.setScaleType(ScaleType.CENTER);

        _imageLoader.displayImage("file://"+_imageUrls.get(position), imageView, _options, new SimpleImageLoadingListener() {
            @Override
            public void onLoadingComplete(Bitmap loadedImage) {
                Animation anim = AnimationUtils.loadAnimation(ViewCategoryActivity.this, R.anim.fade_in);
                imageView.setAnimation(anim);
                anim.start();
            }


        });

        imageView.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                Integer position = (Integer)imageView.getTag();
                String url = _imageUrls.get(position);
                Intent intent = new Intent("com.eibimalul.smartgallery.SingleImageDisplay");
                intent.putExtra("selectedImagePosition", position);
                intent.putStringArrayListExtra("imageUrls", _imageUrls);
                intent.putExtra("Title", _activityTitle);
                startActivityForResult(intent, 0);
            }
        });

        return convertView;
    }
}

然后我将它连接到 GridView:

_options = new DisplayImageOptions.Builder()
    .showStubImage(R.drawable.stub_image)
    .showImageForEmptyUri(R.drawable.image_for_empty_url)
    .cacheInMemory()
    .cacheOnDisc()
    .build();

    _imageAdapter = new ImageAdapter(this, _imageUrls);

    GridView gridView = (GridView) findViewById(R.id.gridview);
    gridView.setAdapter(_imageAdapter);

任何帮助将不胜感激..

最佳答案

不确定,但对我来说这有效,但我不认为这是解决这个问题的最佳方法,但它会有一点帮助。

mImageLoader.displayImage(photoBean.getPhotoURL(), imageView, options, new ImageRotationListener());

    options = new DisplayImageOptions.Builder()
    .cacheOnDisk(true)
    .cacheInMemory(true)
    .considerExifParams(true)
    .bitmapConfig(Bitmap.Config.RGB_565)
    .showImageForEmptyUri(R.drawable.icon_venue_default)
    .showImageOnLoading(R.drawable.icon_venue_default)
    .showImageOnFail(R.drawable.icon_venue_default)
    .build();

package com.urbanft.utils;

import android.graphics.Bitmap;
import android.media.ExifInterface;
import android.view.View;
import android.widget.ImageView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;

import java.io.File;

/**
 * Created by kiwitech on 30/8/16.
 */
public class ImageRotationListener implements ImageLoadingListener {

    @Override
    public void onLoadingStarted(String imageUri, View view) {
    }

    @Override
    public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
    }

    @Override
    public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
        try {
            if(loadedImage != null){
                File file = ImageLoader.getInstance().getDiskCache().get(imageUri);
                if(file == null){
                    return;
                }
                int rotation = getCameraPhotoOrientation(file);
                Bitmap bitmap = null;
                if(rotation != 0){
                    bitmap = resizeBitmap(loadedImage, 1000, 1000);
                    if(bitmap != null){
                        if(view instanceof ImageView){
                            ((ImageView) view).setImageBitmap(bitmap);
                        }
                    }
                }
                view.setBackgroundDrawable(null);
            }
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onLoadingCancelled(String imageUri, View view) {
    }

    public Bitmap resizeBitmap( Bitmap input, int destWidth, int destHeight )
    {
        int srcWidth = input.getWidth();
        int srcHeight = input.getHeight();
        boolean needsResize = false;
        float p;
        if (srcWidth > destWidth || srcHeight > destHeight) {
            needsResize = true;
            if ( srcWidth > srcHeight && srcWidth > destWidth) {
                p = (float)destWidth / (float)srcWidth;
                destHeight = (int)( srcHeight * p );
            }
            else {
                p = (float)destHeight / (float)srcHeight;
                destWidth = (int)( srcWidth * p );
            }
        }
        else {
            destWidth = srcWidth;
            destHeight = srcHeight;
        }
        if (needsResize) {
            Bitmap output = Bitmap.createScaledBitmap( input, destWidth, destHeight, true );
            return output;
        }
        else {
            return input;
        }
    }

    public int getCameraPhotoOrientation(File imageFile) throws Exception {
        int rotate = 0;
        try {
            if(imageFile.exists()){
                ExifInterface exif = new ExifInterface(imageFile.getAbsolutePath());
                int orientation = Integer.parseInt(exif.getAttribute(ExifInterface.TAG_ORIENTATION));
                switch (orientation) {
                    case ExifInterface.ORIENTATION_NORMAL:
                        rotate = 0;
                        break;

                    case ExifInterface.ORIENTATION_ROTATE_270:
                        rotate = 270;
                        break;

                    case ExifInterface.ORIENTATION_ROTATE_180:
                        rotate = 180;
                        break;

                    case ExifInterface.ORIENTATION_ROTATE_90:
                        rotate = 90;
                        break;
                }
            }
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        return rotate;
    }
}

关于android - 为什么使用 ImageLoader 显示的肖像图像会旋转?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25250274/

相关文章:

android - 如何制作没有文字的单选按钮?

c# - 如何使用c#获取gridview中的特定数据?

Android surfaceChanged 不正确的宽度/高度值

javascript - 使用旋转 Div 暂停 jQuery 动画

java - 在java中查找匹配后的字符串

android - 如何同步2个音频文件?一个是另一个的录制版本?

Android 多种屏幕尺寸和密度问题

asp.net - Gridview 在 firefox 浏览器中没有内容占位符

c# - 在 Win8 XAML 中使用 SemanticZoom 对分组 GridView 中的滚动使用react

swift - 如何旋转键盘,是否可以同时进行两个不同的文本输入?