Skip to content

Commit

Permalink
Fixing issue where Camera returned a content URI that File Reader cou…
Browse files Browse the repository at this point in the history
…ld not read
  • Loading branch information
macdonst committed Dec 16, 2010
1 parent ab8950a commit 8bad4eb
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions framework/src/com/phonegap/FileUtils.java
Expand Up @@ -15,6 +15,8 @@
import org.json.JSONException;
import org.json.JSONObject;

import android.net.Uri;

import com.phonegap.api.Plugin;
import com.phonegap.api.PluginResult;

Expand Down Expand Up @@ -191,7 +193,7 @@ else if (action.equals("writeAsText")) {
*/
public String readAsText(String filename, String encoding) throws FileNotFoundException, IOException {
byte[] bytes = new byte[1000];
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(filename), 1024);
BufferedInputStream bis = new BufferedInputStream(getPathFromUri(filename), 1024);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
int numRead = 0;
while ((numRead = bis.read(bytes, 0, 1000)) >= 0) {
Expand All @@ -209,7 +211,7 @@ public String readAsText(String filename, String encoding) throws FileNotFoundEx
*/
public String readAsDataURL(String filename) throws FileNotFoundException, IOException {
byte[] bytes = new byte[1000];
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(filename), 1024);
BufferedInputStream bis = new BufferedInputStream(getPathFromUri(filename), 1024);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
int numRead = 0;
while ((numRead = bis.read(bytes, 0, 1000)) >= 0) {
Expand Down Expand Up @@ -281,5 +283,21 @@ private long truncateFile(String filename, long size) throws FileNotFoundExcepti

return raf.length();
}


/**
* Get an input stream based on file path or content:// uri
*
* @param path
* @return an input stream
* @throws FileNotFoundException
*/
private InputStream getPathFromUri(String path) throws FileNotFoundException {
if (path.startsWith("content")) {
Uri uri = Uri.parse(path);
return ctx.getContentResolver().openInputStream(uri);
}
else {
return new FileInputStream(path);
}
}
}

0 comments on commit 8bad4eb

Please sign in to comment.