@@ -158,6 +158,9 @@ public void cropImage(
158
158
String uri , ReadableMap options , final Callback success , final Callback error ) {
159
159
ReadableMap offset = options .hasKey ("offset" ) ? options .getMap ("offset" ) : null ;
160
160
ReadableMap size = options .hasKey ("size" ) ? options .getMap ("size" ) : null ;
161
+ boolean allowExternalStorage =
162
+ options .hasKey ("allowExternalStorage" ) ? options .getBoolean ("allowExternalStorage" ) : true ;
163
+
161
164
if (offset == null
162
165
|| size == null
163
166
|| !offset .hasKey ("x" )
@@ -178,6 +181,7 @@ public void cropImage(
178
181
(int ) offset .getDouble ("y" ),
179
182
(int ) size .getDouble ("width" ),
180
183
(int ) size .getDouble ("height" ),
184
+ allowExternalStorage ,
181
185
success ,
182
186
error );
183
187
if (options .hasKey ("displaySize" )) {
@@ -195,6 +199,7 @@ private static class CropTask extends GuardedAsyncTask<Void, Void> {
195
199
final int mY ;
196
200
final int mWidth ;
197
201
final int mHeight ;
202
+ final boolean mAllowExternalStorage ;
198
203
int mTargetWidth = 0 ;
199
204
int mTargetHeight = 0 ;
200
205
final Callback mSuccess ;
@@ -207,6 +212,7 @@ private CropTask(
207
212
int y ,
208
213
int width ,
209
214
int height ,
215
+ boolean allowExternalStorage ,
210
216
Callback success ,
211
217
Callback error ) {
212
218
super (context );
@@ -220,6 +226,7 @@ private CropTask(
220
226
mY = y ;
221
227
mWidth = width ;
222
228
mHeight = height ;
229
+ mAllowExternalStorage = allowExternalStorage ;
223
230
mSuccess = success ;
224
231
mError = error ;
225
232
}
@@ -267,8 +274,17 @@ protected void doInBackgroundGuarded(Void... params) {
267
274
throw new IOException ("Could not determine MIME type" );
268
275
}
269
276
270
- File tempFile = createTempFile (mContext , mimeType );
271
- writeCompressedBitmapToFile (cropped , mimeType , tempFile );
277
+ File tempFile ;
278
+ try {
279
+ tempFile = writeBitmapToInternalCache (mContext , cropped , mimeType );
280
+ } catch (Exception e ) {
281
+ if (mAllowExternalStorage ) {
282
+ tempFile = writeBitmapToExternalCache (mContext , cropped , mimeType );
283
+ } else {
284
+ throw new SecurityException (
285
+ "We couldn't create file in internal cache and external cache is disabled. Did you forget to pass allowExternalStorage=true?" );
286
+ }
287
+ }
272
288
273
289
if (mimeType .equals ("image/jpeg" )) {
274
290
copyExif (mContext , Uri .parse (mUri ), tempFile );
@@ -455,42 +471,36 @@ private static Bitmap.CompressFormat getCompressFormatForType(String type) {
455
471
return Bitmap .CompressFormat .JPEG ;
456
472
}
457
473
474
+ private static File writeBitmapToInternalCache (Context context , Bitmap cropped , String mimeType )
475
+ throws IOException {
476
+ File tempFile = createTempFile (context .getCacheDir (), mimeType );
477
+ writeCompressedBitmapToFile (cropped , mimeType , tempFile );
478
+ return tempFile ;
479
+ }
480
+
481
+ private static File writeBitmapToExternalCache (Context context , Bitmap cropped , String mimeType )
482
+ throws IOException {
483
+ File tempFile = createTempFile (context .getExternalCacheDir (), mimeType );
484
+ writeCompressedBitmapToFile (cropped , mimeType , tempFile );
485
+ return tempFile ;
486
+ }
487
+
458
488
private static void writeCompressedBitmapToFile (Bitmap cropped , String mimeType , File tempFile )
459
489
throws IOException {
460
490
OutputStream out = new FileOutputStream (tempFile );
461
- try {
462
- cropped .compress (getCompressFormatForType (mimeType ), COMPRESS_QUALITY , out );
463
- } finally {
464
- if (out != null ) {
465
- out .close ();
466
- }
467
- }
491
+ cropped .compress (getCompressFormatForType (mimeType ), COMPRESS_QUALITY , out );
468
492
}
469
493
470
494
/**
471
- * Create a temporary file in the cache directory on either internal or external storage,
472
- * whichever is available and has more free space.
495
+ * Create a temporary file in internal / external storage to use for image scaling and caching.
473
496
*
474
497
* @param mimeType the MIME type of the file to create (image/*)
475
498
*/
476
- private static File createTempFile (Context context , @ Nullable String mimeType )
499
+ private static File createTempFile (@ Nullable File cacheDir , @ Nullable String mimeType )
477
500
throws IOException {
478
- File externalCacheDir = context .getExternalCacheDir ();
479
- File internalCacheDir = context .getCacheDir ();
480
- File cacheDir ;
481
- if (externalCacheDir == null && internalCacheDir == null ) {
501
+ if (cacheDir == null ) {
482
502
throw new IOException ("No cache directory available" );
483
503
}
484
- if (externalCacheDir == null ) {
485
- cacheDir = internalCacheDir ;
486
- } else if (internalCacheDir == null ) {
487
- cacheDir = externalCacheDir ;
488
- } else {
489
- cacheDir =
490
- externalCacheDir .getFreeSpace () > internalCacheDir .getFreeSpace ()
491
- ? externalCacheDir
492
- : internalCacheDir ;
493
- }
494
504
return File .createTempFile (TEMP_FILE_PREFIX , getFileExtensionForType (mimeType ), cacheDir );
495
505
}
496
506
0 commit comments