Reputation: 400
I am trying to display an image gallery as a GridView using ImageAdapter and ImageLoader. All portrait images are rotated +-90 degrees though. I know the getExifOrientation code to check for the image orientation and rotate it back, but I simply don't know where to use it..
Here is the ImageAdapter class I use, got most of it here and trying to use it correctly.
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;
}
}
Than I connect it to the 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);
Any help will be appreciated..
Upvotes: 2
Views: 2666
Reputation: 4857
Not sure but for me this works, but i dont think this is a best way to solve this problem, but it will help little bit.
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;
}
}
Upvotes: 2