Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#107 Resolves image rotation issue #108

Merged
merged 1 commit into from
Dec 31, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import nl.changer.polypicker.Config;
import nl.changer.polypicker.ImagePickerActivity;
import nl.changer.polypicker.model.Image;
import nl.changer.polypicker.utils.ImageInternalFetcher;

public class MainActivity extends AppCompatActivity {
Expand All @@ -34,6 +35,7 @@ public class MainActivity extends AppCompatActivity {

private ViewGroup mSelectedImagesContainer;
HashSet<Uri> mMedia = new HashSet<Uri>();
HashSet<Image> mMediaImages = new HashSet<Image>();

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand Down Expand Up @@ -87,19 +89,26 @@ protected void onActivityResult(int requestCode, int resuleCode, Intent intent)
if (resuleCode == Activity.RESULT_OK) {
if (requestCode == INTENT_REQUEST_GET_IMAGES || requestCode == INTENT_REQUEST_GET_N_IMAGES) {
Parcelable[] parcelableUris = intent.getParcelableArrayExtra(ImagePickerActivity.EXTRA_IMAGE_URIS);

int[] parcelableOrientations = intent.getIntArrayExtra((ImagePickerActivity.EXTRA_IMAGE_ORIENTATIONS));
if (parcelableUris == null) {
return;
}

// Java doesn't allow array casting, this is a little hack
Uri[] uris = new Uri[parcelableUris.length];
int[] orientations = new int[parcelableUris.length];
System.arraycopy(parcelableUris, 0, uris, 0, parcelableUris.length);
System.arraycopy(parcelableOrientations, 0, orientations, 0, parcelableOrientations.length);

if (uris != null) {
for (Uri uri : uris) {
/*for (Uri uri : uris) {
Log.i(TAG, " uri: " + uri);
mMedia.add(uri);

}*/
for (int i=0; i<orientations.length; i++) {
mMediaImages.add(new Image(uris[i], orientations[i]));

}

showMedia();
Expand All @@ -113,13 +122,13 @@ private void showMedia() {
// adding the new ones.
mSelectedImagesContainer.removeAllViews();

Iterator<Uri> iterator = mMedia.iterator();
Iterator<Image> iterator = mMediaImages.iterator();
ImageInternalFetcher imageFetcher = new ImageInternalFetcher(this, 500);
while (iterator.hasNext()) {
Uri uri = iterator.next();
Image image = iterator.next();

// showImage(uri);
Log.i(TAG, " uri: " + uri);
Log.i(TAG, " uri: " + image);
if (mMedia.size() >= 1) {
mSelectedImagesContainer.setVisibility(View.VISIBLE);
}
Expand All @@ -130,12 +139,12 @@ private void showMedia() {
// initRemoveBtn(removeBtn, imageHolder, uri);
ImageView thumbnail = (ImageView) imageHolder.findViewById(R.id.media_image);

if (!uri.toString().contains("content://")) {
if (!image.mUri.toString().contains("content://")) {
// probably a relative uri
uri = Uri.fromFile(new File(uri.toString()));
image.mUri = Uri.fromFile(new File(image.mUri.toString()));
}

imageFetcher.loadImage(uri, thumbnail);
imageFetcher.loadImage(image.mUri, thumbnail, image.mOrientation);

mSelectedImagesContainer.addView(imageHolder);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public View getView(int position, View convertView, ViewGroup parent) {
((FrameLayout) convertView).setForeground(isSelected ? getResources().getDrawable(R.drawable.gallery_photo_selected) : null);

if (holder.mImage == null || !holder.mImage.equals(image)) {
mActivity.mImageFetcher.loadImage(image.mUri, holder.mThumbnail);
mActivity.mImageFetcher.loadImage(image.mUri, holder.mThumbnail, image.mOrientation);
holder.mImage = image;
}
return convertView;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public class ImagePickerActivity extends AppCompatActivity {
* Returns the parcelled image uris in the intent with this extra.
*/
public static final String EXTRA_IMAGE_URIS = "nl.changer.changer.nl.polypicker.extra.selected_image_uris";
public static final String EXTRA_IMAGE_ORIENTATIONS = "nl.changer.changer.nl.polypicker.extra.selected_image_orientations";

private Set<Image> mSelectedImages;
private LinearLayout mSelectedImagesContainer;
Expand Down Expand Up @@ -133,7 +134,7 @@ public boolean addImage(Image image) {
View rootView = LayoutInflater.from(ImagePickerActivity.this).inflate(R.layout.pp__list_item_selected_thumbnail, null);
ImageView thumbnail = (ImageView) rootView.findViewById(R.id.pp__selected_photo);
rootView.setTag(image.mUri);
mImageFetcher.loadImage(image.mUri, thumbnail);
mImageFetcher.loadImage(image.mUri, thumbnail, image.mOrientation);
mSelectedImagesContainer.addView(rootView, 0);

int px = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 60, getResources().getDisplayMetrics());
Expand Down Expand Up @@ -180,13 +181,16 @@ public void onClick(View view) {
if (view.getId() == R.id.pp__btn_done) {

Uri[] uris = new Uri[mSelectedImages.size()];
int[] orientations = new int[mSelectedImages.size()];
int i = 0;
for (Image img : mSelectedImages) {
uris[i++] = img.mUri;
uris[i] = img.mUri;
orientations[i++] = img.mOrientation;
}

Intent intent = new Intent();
intent.putExtra(EXTRA_IMAGE_URIS, uris);
intent.putExtra(EXTRA_IMAGE_ORIENTATIONS, orientations);
setResult(Activity.RESULT_OK, intent);
} else if (view.getId() == R.id.pp__btn_cancel) {
setResult(Activity.RESULT_CANCELED);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ protected ImageWorker(Context context) {
* @param data The URL of the image to download.
* @param imageView The ImageView to bind the downloaded image to.
*/
public void loadImage(Object data, ImageView imageView) {
public void loadImage(Object data, ImageView imageView, int orientation) {
if (data == null) {
return;
}
Expand All @@ -85,13 +85,14 @@ public void loadImage(Object data, ImageView imageView) {
if (value != null) {
// Bitmap found in memory cache
imageView.setImageDrawable(value);
imageView.setRotation(orientation);
} else if (cancelPotentialWork(data, imageView)) {
//BEGIN_INCLUDE(execute_background_task)
final BitmapWorkerTask task = new BitmapWorkerTask(data, imageView);
final AsyncDrawable asyncDrawable =
new AsyncDrawable(mResources, mLoadingBitmap, task);
imageView.setImageDrawable(asyncDrawable);

imageView.setRotation(orientation);
// NOTE: This uses a custom version of AsyncTask that has been pulled from the
// framework and slightly modified. Refer to the docs at the top of the class
// for more info on what was changed.
Expand Down