mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-11-05 13:26:48 +08:00
Fix for cases where no external storage is attached (#58)
* Fix for cases where context.getExternalCacheDir() returns null because there is no external storage attached. * Fix crash when WRITE_EXTERNAL_STORAGE permission is missing on API level < 19
This commit is contained in:
@@ -16,8 +16,10 @@
|
|||||||
|
|
||||||
package org.nativescript.widgets.image;
|
package org.nativescript.widgets.image;
|
||||||
|
|
||||||
|
import android.Manifest;
|
||||||
import android.annotation.TargetApi;
|
import android.annotation.TargetApi;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
|
import android.content.pm.PackageManager;
|
||||||
import android.graphics.Bitmap;
|
import android.graphics.Bitmap;
|
||||||
import android.graphics.Bitmap.Config;
|
import android.graphics.Bitmap.Config;
|
||||||
import android.graphics.BitmapFactory;
|
import android.graphics.BitmapFactory;
|
||||||
@@ -333,11 +335,11 @@ public class Cache {
|
|||||||
public static File getDiskCacheDir(Context context, String uniqueName) {
|
public static File getDiskCacheDir(Context context, String uniqueName) {
|
||||||
// Check if media is mounted or storage is built-in, if so, try and use external cache dir
|
// Check if media is mounted or storage is built-in, if so, try and use external cache dir
|
||||||
// otherwise use internal cache dir
|
// otherwise use internal cache dir
|
||||||
final String cachePath =
|
final File cacheFilePath = Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()) ||
|
||||||
Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()) ||
|
!isExternalStorageRemovable() ? getExternalCacheDir(context) : context.getCacheDir();
|
||||||
!isExternalStorageRemovable() ? getExternalCacheDir(context).getPath() :
|
// In case there is no external storage isExternalStorageRemovable returns False and we get Null from getExternalCacheDir.
|
||||||
context.getCacheDir().getPath();
|
// If this is the case - fall back to internal cache dir.
|
||||||
|
final String cachePath = cacheFilePath != null ? cacheFilePath.getPath() : context.getCacheDir().getPath();
|
||||||
return new File(cachePath + File.separator + uniqueName);
|
return new File(cachePath + File.separator + uniqueName);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -417,7 +419,12 @@ public class Cache {
|
|||||||
@TargetApi(VERSION_CODES.FROYO)
|
@TargetApi(VERSION_CODES.FROYO)
|
||||||
public static File getExternalCacheDir(Context context) {
|
public static File getExternalCacheDir(Context context) {
|
||||||
if (Utils.hasFroyo()) {
|
if (Utils.hasFroyo()) {
|
||||||
return context.getExternalCacheDir();
|
if (Utils.hasKitKat() ||
|
||||||
|
context.checkPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE, android.os.Process.myPid(), android.os.Process.myUid()) == PackageManager.PERMISSION_GRANTED) {
|
||||||
|
return context.getExternalCacheDir();
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Before Froyo we need to construct the external cache dir ourselves
|
// Before Froyo we need to construct the external cache dir ourselves
|
||||||
|
|||||||
Reference in New Issue
Block a user