Skip to content

Commit 64c8f5b

Browse files
authored
Merge pull request #762 from Yalantis/improve/update_sample_app
Updated dependencies, resolve unexpected error on result screen
2 parents 0ff3e6a + 70546c1 commit 64c8f5b

File tree

6 files changed

+57
-39
lines changed

6 files changed

+57
-39
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
```
1919
allprojects {
2020
repositories {
21-
jcenter()
21+
...
2222
maven { url "https://jitpack.io" }
2323
}
2424
}

build.gradle

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
buildscript {
22
ext {
3-
androidx_appcompat_version = "1.1.0"
3+
androidx_appcompat_version = "1.3.0"
44
androidx_core_version = "1.1.0"
5-
androidx_exifinterface_version = "1.1.0-beta01"
6-
androidx_transition_version = "1.2.0-rc01"
5+
androidx_exifinterface_version = "1.3.2"
6+
androidx_transition_version = "1.4.1"
77
constraintlayout_version = "1.1.3"
88
}
99

sample/src/main/java/com/yalantis/ucrop/sample/ResultActivity.java

+11-7
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@
2222
import android.widget.TextView;
2323
import android.widget.Toast;
2424

25+
import androidx.annotation.NonNull;
26+
import androidx.appcompat.app.ActionBar;
27+
import androidx.appcompat.widget.Toolbar;
28+
import androidx.core.app.ActivityCompat;
29+
import androidx.core.app.NotificationCompat;
30+
import androidx.core.content.FileProvider;
31+
32+
import com.yalantis.ucrop.util.BitmapLoadUtils;
2533
import com.yalantis.ucrop.view.UCropView;
2634

2735
import java.io.File;
@@ -34,13 +42,6 @@
3442
import java.util.Calendar;
3543
import java.util.List;
3644

37-
import androidx.annotation.NonNull;
38-
import androidx.appcompat.app.ActionBar;
39-
import androidx.appcompat.widget.Toolbar;
40-
import androidx.core.app.ActivityCompat;
41-
import androidx.core.app.NotificationCompat;
42-
import androidx.core.content.FileProvider;
43-
4445
import static android.content.Intent.FLAG_GRANT_READ_URI_PERMISSION;
4546
import static android.content.Intent.FLAG_GRANT_WRITE_URI_PERMISSION;
4647

@@ -167,6 +168,9 @@ private void saveCroppedImage() {
167168
Toast.makeText(ResultActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
168169
Log.e(TAG, imageUri.toString(), e);
169170
}
171+
} else if (BitmapLoadUtils.hasContentScheme(imageUri)){
172+
Toast.makeText(ResultActivity.this, getString(R.string.toast_already_saved), Toast.LENGTH_SHORT).show();
173+
finish();
170174
} else {
171175
Toast.makeText(ResultActivity.this, getString(R.string.toast_unexpected_error), Toast.LENGTH_SHORT).show();
172176
}

sample/src/main/res/values/strings.xml

+1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
<string name="toast_cannot_retrieve_selected_image">Cannot retrieve selected image</string>
4343
<string name="toast_cannot_retrieve_cropped_image">Cannot retrieve cropped image</string>
4444
<string name="toast_unexpected_error">Unexpected error</string>
45+
<string name="toast_already_saved">Your image is already saved</string>
4546

4647
<string name="file_provider_authorities">com.yalantis.ucrop.provider</string>
4748
<string name="channel_name">ucrop_chanel</string>

ucrop/src/main/java/com/yalantis/ucrop/task/BitmapCropTask.java

+31-24
Original file line numberDiff line numberDiff line change
@@ -164,27 +164,7 @@ private boolean crop() throws IOException {
164164
if (shouldCrop) {
165165
saveImage(Bitmap.createBitmap(mViewBitmap, cropOffsetX, cropOffsetY, mCroppedImageWidth, mCroppedImageHeight));
166166
if (mCompressFormat.equals(Bitmap.CompressFormat.JPEG)) {
167-
boolean hasImageInputUriContentSchema = hasContentScheme(mImageInputUri);
168-
boolean hasImageOutputUriContentSchema = hasContentScheme(mImageOutputUri);
169-
if (hasImageInputUriContentSchema && hasImageOutputUriContentSchema) {
170-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
171-
ImageHeaderParser.copyExif(context, mCroppedImageWidth, mCroppedImageHeight, mImageInputUri, mImageOutputUri);
172-
} else {
173-
Log.e(TAG, "It is not possible to write exif info into file represented by \"content\" Uri if Android < LOLLIPOP");
174-
}
175-
} else if (hasImageInputUriContentSchema) {
176-
ImageHeaderParser.copyExif(context, mCroppedImageWidth, mCroppedImageHeight, mImageInputUri, mImageOutputPath);
177-
} else if (hasImageOutputUriContentSchema) {
178-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
179-
ExifInterface originalExif = new ExifInterface(mImageInputPath);
180-
ImageHeaderParser.copyExif(context, originalExif, mCroppedImageWidth, mCroppedImageHeight, mImageOutputUri);
181-
} else {
182-
Log.e(TAG, "It is not possible to write exif info into file represented by \"content\" Uri if Android < LOLLIPOP");
183-
}
184-
} else {
185-
ExifInterface originalExif = new ExifInterface(mImageInputPath);
186-
ImageHeaderParser.copyExif(originalExif, mCroppedImageWidth, mCroppedImageHeight, mImageOutputPath);
187-
}
167+
copyExifForOutputFile(context);
188168
}
189169
return true;
190170
} else {
@@ -193,8 +173,35 @@ private boolean crop() throws IOException {
193173
}
194174
}
195175

196-
private boolean hasContentScheme(Uri uri) {
197-
return uri != null && CONTENT_SCHEME.equals(uri.getScheme());
176+
private void copyExifForOutputFile(Context context) throws IOException {
177+
boolean hasImageInputUriContentSchema = BitmapLoadUtils.hasContentScheme(mImageInputUri);
178+
boolean hasImageOutputUriContentSchema = BitmapLoadUtils.hasContentScheme(mImageOutputUri);
179+
/*
180+
* ImageHeaderParser.copyExif with output uri as a parameter
181+
* uses ExifInterface constructor with FileDescriptor param for overriding output file exif info,
182+
* which doesn't support ExitInterface.saveAttributes call for SDK lower than 21.
183+
*
184+
* See documentation for ImageHeaderParser.copyExif and ExifInterface.saveAttributes implementation.
185+
*/
186+
if (hasImageInputUriContentSchema && hasImageOutputUriContentSchema) {
187+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
188+
ImageHeaderParser.copyExif(context, mCroppedImageWidth, mCroppedImageHeight, mImageInputUri, mImageOutputUri);
189+
} else {
190+
Log.e(TAG, "It is not possible to write exif info into file represented by \"content\" Uri if Android < LOLLIPOP");
191+
}
192+
} else if (hasImageInputUriContentSchema) {
193+
ImageHeaderParser.copyExif(context, mCroppedImageWidth, mCroppedImageHeight, mImageInputUri, mImageOutputPath);
194+
} else if (hasImageOutputUriContentSchema) {
195+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
196+
ExifInterface originalExif = new ExifInterface(mImageInputPath);
197+
ImageHeaderParser.copyExif(context, originalExif, mCroppedImageWidth, mCroppedImageHeight, mImageOutputUri);
198+
} else {
199+
Log.e(TAG, "It is not possible to write exif info into file represented by \"content\" Uri if Android < LOLLIPOP");
200+
}
201+
} else {
202+
ExifInterface originalExif = new ExifInterface(mImageInputPath);
203+
ImageHeaderParser.copyExif(originalExif, mCroppedImageWidth, mCroppedImageHeight, mImageOutputPath);
204+
}
198205
}
199206

200207
private void saveImage(@NonNull Bitmap croppedBitmap) {
@@ -244,7 +251,7 @@ protected void onPostExecute(@Nullable Throwable t) {
244251
if (t == null) {
245252
Uri uri;
246253

247-
if (hasContentScheme(mImageOutputUri)) {
254+
if (BitmapLoadUtils.hasContentScheme(mImageOutputUri)) {
248255
uri = mImageOutputUri;
249256
} else {
250257
uri = Uri.fromFile(new File(mImageOutputPath));

ucrop/src/main/java/com/yalantis/ucrop/util/BitmapLoadUtils.java

+10-4
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,24 @@
1212
import android.view.Display;
1313
import android.view.WindowManager;
1414

15+
import androidx.annotation.NonNull;
16+
import androidx.annotation.Nullable;
17+
import androidx.exifinterface.media.ExifInterface;
18+
1519
import com.yalantis.ucrop.callback.BitmapLoadCallback;
1620
import com.yalantis.ucrop.task.BitmapLoadTask;
1721

1822
import java.io.Closeable;
1923
import java.io.IOException;
2024
import java.io.InputStream;
2125

22-
import androidx.annotation.NonNull;
23-
import androidx.annotation.Nullable;
24-
import androidx.exifinterface.media.ExifInterface;
25-
2626
/**
2727
* Created by Oleksii Shliama (https://github.com/shliama).
2828
*/
2929
public class BitmapLoadUtils {
3030

31+
private static final String CONTENT_SCHEME = "content";
32+
3133
private static final String TAG = "BitmapLoadUtils";
3234

3335
public static void decodeBitmapInBackground(@NonNull Context context,
@@ -171,4 +173,8 @@ public static void close(@Nullable Closeable c) {
171173
}
172174
}
173175

176+
public static boolean hasContentScheme(Uri uri) {
177+
return uri != null && CONTENT_SCHEME.equals(uri.getScheme());
178+
}
179+
174180
}

0 commit comments

Comments
 (0)