From eb0e0d9d118a1d4df3d5d91ceb8b021468977531 Mon Sep 17 00:00:00 2001 From: macdonst Date: Fri, 15 Jul 2011 03:30:22 +0800 Subject: [PATCH] Issue #156: Camera.DestinationType.FILE_URI on Android not conforming to API Spec Instead of capturing the orginal image to /sdcard/Pic.jpg or /sdcard/Capture.jpg we detect if the SD card is mounted. If mounted the file is placed in the apps temp directory at: /sdcard/Android/data/{package name}/cache/ If the SD card is not mounted we default to internal storage at: /data/data/{package name}/cache/ --- .../src/com/phonegap/CameraLauncher.java | 8 +++--- framework/src/com/phonegap/Capture.java | 2 +- .../src/com/phonegap/DirectoryManager.java | 28 +++++++++++++++++++ 3 files changed, 33 insertions(+), 5 deletions(-) diff --git a/framework/src/com/phonegap/CameraLauncher.java b/framework/src/com/phonegap/CameraLauncher.java index ddeb9e16..1ad4bac0 100755 --- a/framework/src/com/phonegap/CameraLauncher.java +++ b/framework/src/com/phonegap/CameraLauncher.java @@ -28,6 +28,7 @@ import android.graphics.Bitmap.CompressFormat; import android.net.Uri; import android.os.Environment; import android.provider.MediaStore; +import android.util.Log; /** * This class launches the camera view, allows the user to take a picture, closes the camera view, @@ -119,14 +120,14 @@ public class CameraLauncher extends Plugin { // Specify file so that large image is captured and returned // TODO: What if there isn't any external storage? - File photo = new File(Environment.getExternalStorageDirectory(), "Pic.jpg"); + File photo = new File(DirectoryManager.getTempDirectoryPath(ctx), "Pic.jpg"); intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo)); this.imageUri = Uri.fromFile(photo); this.ctx.startActivityForResult((Plugin) this, intent, (CAMERA+1)*16 + returnType+1); } - - /** + + /** * Get image from photo library. * * @param quality Compression quality hint (0-100: 0=low quality & high compression, 100=compress of max quality) @@ -161,7 +162,6 @@ public class CameraLauncher extends Plugin { // If CAMERA if (srcType == CAMERA) { - // If image available if (resultCode == Activity.RESULT_OK) { try { diff --git a/framework/src/com/phonegap/Capture.java b/framework/src/com/phonegap/Capture.java index 1d41b863..25ed6460 100644 --- a/framework/src/com/phonegap/Capture.java +++ b/framework/src/com/phonegap/Capture.java @@ -168,7 +168,7 @@ public class Capture extends Plugin { Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); // Specify file so that large image is captured and returned - File photo = new File(Environment.getExternalStorageDirectory(), "Capture.jpg"); + File photo = new File(DirectoryManager.getTempDirectoryPath(ctx), "Capture.jpg"); intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo)); this.imageUri = Uri.fromFile(photo); diff --git a/framework/src/com/phonegap/DirectoryManager.java b/framework/src/com/phonegap/DirectoryManager.java index a659527d..e3ac3074 100644 --- a/framework/src/com/phonegap/DirectoryManager.java +++ b/framework/src/com/phonegap/DirectoryManager.java @@ -9,6 +9,7 @@ package com.phonegap; import java.io.File; +import android.content.Context; import android.os.Environment; import android.os.StatFs; @@ -111,4 +112,31 @@ public class DirectoryManager { } return newPath; } + + /** + * Determine if we can use the SD Card to store the temporary file. If not then use + * the internal cache directory. + * + * @return the absolute path of where to store the file + */ + protected static String getTempDirectoryPath(Context ctx) { + File cache = null; + + // SD Card Mounted + if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { + cache = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + + "/Android/data/" + ctx.getPackageName() + "/cache/"); + } + // Use internal storage + else { + cache = ctx.getCacheDir(); + } + + // Create the cache directory if it doesn't exist + if (!cache.exists()) { + cache.mkdirs(); + } + + return cache.getAbsolutePath(); + } } \ No newline at end of file