diff --git a/android/src/main/java/com/reactnativecommunity/imageeditor/ImageEditorModule.java b/android/src/main/java/com/reactnativecommunity/imageeditor/ImageEditorModule.java index c053ab4..83db220 100644 --- a/android/src/main/java/com/reactnativecommunity/imageeditor/ImageEditorModule.java +++ b/android/src/main/java/com/reactnativecommunity/imageeditor/ImageEditorModule.java @@ -173,6 +173,7 @@ public void cropImage( Promise promise) { ReadableMap offset = options.hasKey("offset") ? options.getMap("offset") : null; ReadableMap size = options.hasKey("size") ? options.getMap("size") : null; + boolean useInternalCache = options.hasKey("useInternalCache") ? options.getBoolean("useInternalCache") : false; if (offset == null || size == null || !offset.hasKey("x") || !offset.hasKey("y") || !size.hasKey("width") || !size.hasKey("height")) { @@ -189,6 +190,7 @@ public void cropImage( (int) offset.getDouble("y"), (int) size.getDouble("width"), (int) size.getDouble("height"), + useInternalCache, promise); if (options.hasKey("displaySize")) { ReadableMap targetSize = options.getMap("displaySize"); @@ -206,6 +208,7 @@ private static class CropTask extends GuardedAsyncTask { final int mY; final int mWidth; final int mHeight; + final boolean mUseInternalCache; int mTargetWidth = 0; int mTargetHeight = 0; final Promise mPromise; @@ -217,6 +220,7 @@ private CropTask( int y, int width, int height, + boolean useInternalCache, Promise promise) { super(context); if (x < 0 || y < 0 || width <= 0 || height <= 0) { @@ -230,6 +234,7 @@ private CropTask( mWidth = width; mHeight = height; mPromise = promise; + mUseInternalCache = useInternalCache; } public void setTargetSize(int width, int height) { @@ -275,7 +280,7 @@ protected void doInBackgroundGuarded(Void... params) { throw new IOException("Could not determine MIME type"); } - File tempFile = createTempFile(mContext, mimeType); + File tempFile = createTempFile(mContext, mimeType, mUseInternalCache); writeCompressedBitmapToFile(cropped, mimeType, tempFile); if (mimeType.equals("image/jpeg")) { @@ -467,23 +472,28 @@ private static void writeCompressedBitmapToFile(Bitmap cropped, String mimeType, * * @param mimeType the MIME type of the file to create (image/*) */ - private static File createTempFile(Context context, @Nullable String mimeType) + private static File createTempFile(Context context, @Nullable String mimeType, boolean useInternalCache) throws IOException { File externalCacheDir = context.getExternalCacheDir(); File internalCacheDir = context.getCacheDir(); - File cacheDir; - if (externalCacheDir == null && internalCacheDir == null) { + File cacheDir = null; + + if (internalCacheDir == null && (externalCacheDir == null || useInternalCache)) { throw new IOException("No cache directory available"); } - if (externalCacheDir == null) { + + if (useInternalCache || externalCacheDir == null) { cacheDir = internalCacheDir; + } else if (externalCacheDir != null && internalCacheDir != null) { + cacheDir = (externalCacheDir.getFreeSpace() > internalCacheDir.getFreeSpace() ? + externalCacheDir : internalCacheDir ); } - else if (internalCacheDir == null) { - cacheDir = externalCacheDir; - } else { - cacheDir = externalCacheDir.getFreeSpace() > internalCacheDir.getFreeSpace() ? - externalCacheDir : internalCacheDir; + + if (cacheDir == null) { + throw new IOException("No cache directory available"); } + + return File.createTempFile(TEMP_FILE_PREFIX, getFileExtensionForType(mimeType), cacheDir); } diff --git a/lib/ImageEditor.js b/lib/ImageEditor.js index c663da6..3faefab 100644 --- a/lib/ImageEditor.js +++ b/lib/ImageEditor.js @@ -44,6 +44,11 @@ type ImageCropData = { cover: string, stretch: string, }>, + + /** + * (Optional) if true, will disable the use of external cache. + * */ + useInternalCache?: boolean, }; class ImageEditor { @@ -59,7 +64,10 @@ class ImageEditor { * will point to the image in the cache path. Remember to delete the * cropped image from the cache path when you are done with it. */ - static cropImage(uri: string, cropData: ImageCropData): Promise { + static cropImage( + uri: string, + cropData: ImageCropData + ): Promise { return RNCImageEditor.cropImage(uri, cropData); } } diff --git a/typings/index.d.ts b/typings/index.d.ts index 5c000be..40dafa1 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -29,6 +29,11 @@ export type ImageCropData = { * `displaySize` param is not specified, this has no effect. */ resizeMode?: $Maybe<"contain" | "cover" | "stretch">, + + /** + * (Optional) if true, will disable the use of external cache. + **/ + useInternalCache?: boolean, }; declare class ImageEditor { @@ -46,8 +51,8 @@ declare class ImageEditor { */ static cropImage: ( uri: string, - cropData: ImageCropData, + cropData: ImageCropData ) => Promise } -export default ImageEditor \ No newline at end of file +export default ImageEditor