Decode image from File instead of content resolver

This commit is contained in:
macdonst 2012-06-26 13:41:53 -04:00
parent 87b81e53f0
commit a74f71c935

View File

@ -40,6 +40,7 @@ import android.content.ContentValues;
import android.content.Intent; import android.content.Intent;
import android.database.Cursor; import android.database.Cursor;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix; import android.graphics.Matrix;
import android.graphics.Bitmap.CompressFormat; import android.graphics.Bitmap.CompressFormat;
import android.media.MediaScannerConnection; import android.media.MediaScannerConnection;
@ -85,7 +86,7 @@ public class CameraLauncher extends Plugin implements MediaScannerConnectionClie
public String callbackId; public String callbackId;
private int numPics; private int numPics;
private MediaScannerConnection conn; // Used to update gallery app with newly-written files private MediaScannerConnection conn; // Used to update gallery app with newly-written files
//This should never be null! //This should never be null!
@ -373,14 +374,14 @@ public class CameraLauncher extends Plugin implements MediaScannerConnectionClie
} else { } else {
bitmap = scaleBitmap(getBitmapFromResult(intent)); bitmap = scaleBitmap(getBitmapFromResult(intent));
// Add compressed version of captured image to returned media store Uri // Add compressed version of captured image to returned media store Uri
OutputStream os = this.cordova.getActivity().getContentResolver().openOutputStream(uri); OutputStream os = this.cordova.getActivity().getContentResolver().openOutputStream(uri);
bitmap.compress(Bitmap.CompressFormat.JPEG, this.mQuality, os); bitmap.compress(Bitmap.CompressFormat.JPEG, this.mQuality, os);
os.close(); os.close();
// Restore exif data to file // Restore exif data to file
if (this.encodingType == JPEG) { if (this.encodingType == JPEG) {
String exifPath; String exifPath;
if (this.saveToPhotoAlbum) { if (this.saveToPhotoAlbum) {
@ -391,15 +392,15 @@ public class CameraLauncher extends Plugin implements MediaScannerConnectionClie
exif.createOutFile(exifPath); exif.createOutFile(exifPath);
exif.writeExifData(); exif.writeExifData();
} }
} }
// Send Uri back to JavaScript for viewing image // Send Uri back to JavaScript for viewing image
this.success(new PluginResult(PluginResult.Status.OK, uri.toString()), this.callbackId); this.success(new PluginResult(PluginResult.Status.OK, uri.toString()), this.callbackId);
} }
this.cleanup(FILE_URI, this.imageUri, bitmap); this.cleanup(FILE_URI, this.imageUri, bitmap);
bitmap = null; bitmap = null;
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
this.failPicture("Error capturing image."); this.failPicture("Error capturing image.");
@ -505,15 +506,18 @@ public class CameraLauncher extends Plugin implements MediaScannerConnectionClie
} }
private Bitmap getBitmapFromResult(Intent intent) private Bitmap getBitmapFromResult(Intent intent)
throws IOException, FileNotFoundException { throws IOException {
Bitmap bitmap = null; Bitmap bitmap = null;
try { //try {
bitmap = android.provider.MediaStore.Images.Media.getBitmap(this.ctx.getActivity().getContentResolver(), imageUri); Log.d(LOG_TAG, "Image URI = " + imageUri.toString());
} catch (FileNotFoundException e) { String fileName = FileUtils.stripFileProtocol(imageUri.toString());
Uri uri = intent.getData(); bitmap = BitmapFactory.decodeFile(fileName);
android.content.ContentResolver resolver = this.ctx.getActivity().getContentResolver(); //bitmap = android.provider.MediaStore.Images.Media.getBitmap(this.ctx.getActivity().getContentResolver(), imageUri);
bitmap = android.graphics.BitmapFactory.decodeStream(resolver.openInputStream(uri)); //} catch (FileNotFoundException e) {
} // Uri uri = intent.getData();
// android.content.ContentResolver resolver = this.ctx.getActivity().getContentResolver();
// bitmap = android.graphics.BitmapFactory.decodeStream(resolver.openInputStream(uri));
//}
return bitmap; return bitmap;
} }
@ -530,20 +534,20 @@ public class CameraLauncher extends Plugin implements MediaScannerConnectionClie
null, null,
null); null);
} }
/** /**
* Cleans up after picture taking. Checking for duplicates and that kind of stuff. * Cleans up after picture taking. Checking for duplicates and that kind of stuff.
*/ */
private void cleanup(int imageType, Uri oldImage, Bitmap bitmap) { private void cleanup(int imageType, Uri oldImage, Bitmap bitmap) {
bitmap.recycle(); bitmap.recycle();
// Clean up initial camera-written image file. // Clean up initial camera-written image file.
(new File(FileUtils.stripFileProtocol(oldImage.toString()))).delete(); (new File(FileUtils.stripFileProtocol(oldImage.toString()))).delete();
checkForDuplicateImage(imageType); checkForDuplicateImage(imageType);
// Scan for the gallery to update pic refs in gallery // Scan for the gallery to update pic refs in gallery
this.scanForGallery(); this.scanForGallery();
System.gc(); System.gc();
} }
@ -616,12 +620,12 @@ public class CameraLauncher extends Plugin implements MediaScannerConnectionClie
public void failPicture(String err) { public void failPicture(String err) {
this.error(new PluginResult(PluginResult.Status.ERROR, err), this.callbackId); this.error(new PluginResult(PluginResult.Status.ERROR, err), this.callbackId);
} }
private void scanForGallery() { private void scanForGallery() {
if(this.conn!=null) this.conn.disconnect(); if(this.conn!=null) this.conn.disconnect();
this.conn = new MediaScannerConnection(this.ctx.getActivity().getApplicationContext(), this); this.conn = new MediaScannerConnection(this.ctx.getActivity().getApplicationContext(), this);
conn.connect(); conn.connect();
} }
public void onMediaScannerConnected() { public void onMediaScannerConnected() {
try{ try{
@ -630,10 +634,10 @@ public class CameraLauncher extends Plugin implements MediaScannerConnectionClie
e.printStackTrace(); e.printStackTrace();
LOG.d(LOG_TAG, "Can;t scan file in MediaScanner aftering taking picture"); LOG.d(LOG_TAG, "Can;t scan file in MediaScanner aftering taking picture");
} }
} }
public void onScanCompleted(String path, Uri uri) { public void onScanCompleted(String path, Uri uri) {
this.conn.disconnect(); this.conn.disconnect();
} }
} }