This commit is contained in:
Jamie McDonald 2015-02-13 13:59:55 +01:00
parent fb412bb245
commit 7b02695b07
2 changed files with 10 additions and 10 deletions

View File

@ -19,6 +19,7 @@ android {
}
dependencies {
compile 'com.android.support:support-annotations:21.0.0'
androidTestCompile 'com.squareup:fest-android:1.0.7'
androidTestCompile 'com.android.support:support-v4:21.0.0'
androidTestCompile 'org.mockito:mockito-core:1.9.5'

View File

@ -25,6 +25,7 @@ import android.net.Uri;
import android.os.Handler;
import android.os.ParcelFileDescriptor;
import android.provider.MediaStore;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import java.io.Closeable;
@ -42,7 +43,7 @@ class CropUtil {
private static final String SCHEME_FILE = "file";
private static final String SCHEME_CONTENT = "content";
public static void closeSilently(Closeable c) {
public static void closeSilently(@Nullable Closeable c) {
if (c == null) return;
try {
c.close();
@ -86,6 +87,7 @@ class CropUtil {
}
}
@Nullable
public static File getFromMediaUri(Context context, ContentResolver resolver, Uri uri) {
if (uri == null) return null;
@ -121,11 +123,12 @@ class CropUtil {
}
private static String getTempFilename(Context context) throws IOException {
File outputDir = context.getCacheDir(); // context being the Activity pointer
File outputDir = context.getCacheDir();
File outputFile = File.createTempFile("image", "tmp", outputDir);
return outputFile.getAbsolutePath();
}
@Nullable
private static File getFromMediaUriPfd(Context context, ContentResolver resolver, Uri uri) {
if (uri == null) return null;
@ -139,21 +142,17 @@ class CropUtil {
String tempFilename = getTempFilename(context);
output = new FileOutputStream(tempFilename);
int read = 0;
int read;
byte[] bytes = new byte[4096];
while ((read = input.read(bytes)) != -1) {
output.write(bytes, 0, read);
}
return new File(tempFilename);
} catch (IOException ignored) {
// nothing we can do
// Nothing we can do
} finally {
if (input != null) try {
input.close();
} catch (Exception ignored) {}
if (output != null) try {
output.close();
} catch (Exception ignored) {}
closeSilently(input);
closeSilently(output);
}
return null;
}