Skip to main content

Android phonegap app : unable to retrieve an image taken by the camera using my own code (not phonegap"s)



Note- even though im using phonegap, the question is not about some issue in that.











Hi, im developing a android app. my app is using phonegap 1.3.





mi problem...


Im using phonegap apis to take a picture and display it in my app. But whats happening is due to some reason the os kills my app after the camera is launched, so that after the photo is clicked, my app is relaunched and it doesnt get the info abt the taken picture.


As a workaround to this problem ( the problem ), i designed a phonegap plugin which on app start checks if the app had crashed while taking a picture (some flags in code), and if it is restarting after crash, it retrieves the Pic.jpg taken by the camera and tries to displays it. The problem is that its not able to get the right image, or even a proper .jpg file for that matter.









mi code...


Phonegap makes an intent to start the camera and passes the uri for the Pic.jpg that it creates into that intent which it passes to startActivityForResult.







Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");

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, 0);







Note- The above code is from the file CameraLauncher.java of Phonegap.


Now, im assuming that the startActivityForResult stores the picture the user captures at the file which was created by 'photo' in the above code. So even if after the os closes the app while the camera is open (refer - the problem ) the photo will be stored there. PLEASE correct me if this assumption is wrong, or if the photo might be being saved somewhere else.


So taking into account this assumption i wrote a plugin which retrieves this image using the same logic which Phonegap uses in CameraLauncher.java














Uri imageUri;

ExifHelper exif = new ExifHelper();

exif.createInFile(getTempDirectoryPath(ctx) + "/Pic.jpg");

exif.readExifData();

File photo = new File(getTempDirectoryPath(ctx), "Pic.jpg");

imageUri = Uri.fromFile(photo);



Bitmap bitmap = null;



bitmap = android.provider.MediaStore.Images.Media.getBitmap(this.ctx.getContentResolver(), imageUri);





ContentValues values = new ContentValues();

values.put(android.provider.MediaStore.Images.Media.MIME_TYPE, "image/jpeg");

Uri uri = null;

try {

uri = this.ctx.getContentResolver().insert(android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);

} catch (UnsupportedOperationException e) {

uri = this.ctx.getContentResolver().insert(android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI, values);

}



// Add compressed version of captured image to returned media store Uri



OutputStream os = this.ctx.getContentResolver().openOutputStream(uri);

bitmap.compress(Bitmap.CompressFormat.JPEG, 20, os);

os.close();



exif.createOutFile(getRealPathFromURI(uri, this.ctx));

exif.writeExifData();



bitmap.recycle();

bitmap = null;

System.gc();

result = new PluginResult(Status.OK, uri.toString());







This is roughly the same logic as Phonegap uses. (I've removed all exception handling n all for posting. So assume there are no compile errors n all.) So, im basically trying to retrieve the Pic.jpg and return it to my phonegap app.


BUT whats happening is that im getting a corrupted file of abt 150kb that isn't even a jpg (doesnt open).








Please tell me if its even possible to retrieve images in this manner after the activity that started the camera as died. And if its possible then, what am i doing wrong. Please help!


Comments

  1. As I mentioned over in your other SO question,

    phonegap android app crashing due to low memory on opening camera

    the problem is most probably the Sony implementation of the camera intent. You should try testing by adding a third party camera app and when you take a picture select that app and see if it still crashes. It probably won't.

    ReplyDelete

Post a Comment

Popular posts from this blog

Slow Android emulator

I have a 2.67 GHz Celeron processor, 1.21 GB of RAM on a x86 Windows XP Professional machine. My understanding is that the Android emulator should start fairly quickly on such a machine, but for me it does not. I have followed all instructions in setting up the IDE, SDKs, JDKs and such and have had some success in staring the emulator quickly but is very particulary. How can I, if possible, fix this problem?

CCNA 3 Final Exam => latest version

1 . Which security protocol or measure would provide the greatest protection for a wireless LAN? WPA2 cloaking SSIDs shared WEP key MAC address filtering   2 . Refer to the exhibit. All trunk links are operational and all VLANs are allowed on all trunk links. An ARP request is sent by computer 5. Which device or devices will receive this message? only computer 4 computer 3 and RTR-A computer 4 and RTR-A computer 1, computer 2, computer 4, and RTR-A computer 1, computer 2, computer 3, computer 4, and RTR-A all of the computers and the router   3 . Refer to the exhibit. Hosts A and B, connected to hub HB1, attempt to transmit a frame at the same time but a collision occurs. Which hosts will receive the collision jamming signal? only hosts A and B only hosts A, B, and C only hosts A, B, C, and D only hosts A, B, C, and E   4 . Refer to the exhibit. Router RA receives a packet with a source address of 192.168.1.65 and a destination address of 192.168.1.161...