Skip to content

Commit

Permalink
Switch Android SDK 29 deprecated methods to alternatives (#1110)
Browse files Browse the repository at this point in the history
Android SDK 29 deprecated the following method for getting external directories
that we use repeatedly.
https://developer.android.com/reference/android/os/Environment#getExternalStoragePublicDirectory(java.lang.String)

I converted the various use cases to use the suggested alternative in
the Context API.
https://developer.android.com/reference/android/content/Context.html#getExternalFilesDir(java.lang.String)
  • Loading branch information
phil-flyclops committed Sep 25, 2019
1 parent 9870d9a commit b35dfd8
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.reactnative.ivpusic.imagepicker;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
Expand All @@ -26,7 +27,7 @@

class Compression {

File resize(String originalImagePath, int maxWidth, int maxHeight, int quality) throws IOException {
File resize(Context context, String originalImagePath, int maxWidth, int maxHeight, int quality) throws IOException {
Bitmap original = BitmapFactory.decodeFile(originalImagePath);

int width = original.getWidth();
Expand Down Expand Up @@ -54,9 +55,8 @@ File resize(String originalImagePath, int maxWidth, int maxHeight, int quality)

Bitmap resized = Bitmap.createScaledBitmap(original, finalWidth, finalHeight, true);
resized = Bitmap.createBitmap(resized, 0, 0, finalWidth, finalHeight, rotationMatrix, true);

File imageDirectory = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES);

File imageDirectory = context.getExternalFilesDir(Environment.DIRECTORY_PICTURES);

if(!imageDirectory.exists()) {
Log.d("image-crop-picker", "Pictures Directory is not existing. Will create this directory.");
Expand Down Expand Up @@ -88,7 +88,7 @@ int getRotationInDegreesForOrientationTag(int orientationTag) {
}
}

File compressImage(final ReadableMap options, final String originalImagePath, final BitmapFactory.Options bitmapOptions) throws IOException {
File compressImage(final Context context, final ReadableMap options, final String originalImagePath, final BitmapFactory.Options bitmapOptions) throws IOException {
Integer maxWidth = options.hasKey("compressImageMaxWidth") ? options.getInt("compressImageMaxWidth") : null;
Integer maxHeight = options.hasKey("compressImageMaxHeight") ? options.getInt("compressImageMaxHeight") : null;
Double quality = options.hasKey("compressImageQuality") ? options.getDouble("compressImageQuality") : null;
Expand Down Expand Up @@ -123,7 +123,7 @@ File compressImage(final ReadableMap options, final String originalImagePath, fi
maxHeight = Math.min(maxHeight, bitmapOptions.outHeight);
}

return resize(originalImagePath, maxWidth, maxHeight, targetQuality);
return resize(context, originalImagePath, maxWidth, maxHeight, targetQuality);
}

synchronized void compressVideo(final Activity activity, final ReadableMap options, final String originalVideo, final String compressedVideo, final Promise promise) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -736,7 +736,8 @@ private void croppingResult(Activity activity, final int requestCode, final int
if (resultUri != null) {
try {
if (width > 0 && height > 0) {
resultUri = Uri.fromFile(compression.resize(resultUri.getPath(), width, height, 100));
File resized = compression.resize(this.reactContext, resultUri.getPath(), width, height, 100);
resultUri = Uri.fromFile(resized);
}

WritableMap result = getSelection(activity, resultUri, false);
Expand Down Expand Up @@ -783,8 +784,7 @@ private boolean isCameraAvailable(Activity activity) {
private File createImageFile() throws IOException {

String imageFileName = "image-" + UUID.randomUUID().toString();
File path = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES);
File path = this.reactContext.getExternalFilesDir(Environment.DIRECTORY_PICTURES);

if (!path.exists() && !path.isDirectory()) {
path.mkdirs();
Expand All @@ -802,8 +802,7 @@ private File createImageFile() throws IOException {
private File createVideoFile() throws IOException {

String videoFileName = "video-" + UUID.randomUUID().toString();
File path = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES);
File path = this.reactContext.getExternalFilesDir(Environment.DIRECTORY_PICTURES);

if (!path.exists() && !path.isDirectory()) {
path.mkdirs();
Expand Down

0 comments on commit b35dfd8

Please sign in to comment.