[TIMOB-25619] Android: previewImage() crashes when given a non-file based blob
GitHub Issue | n/a |
---|---|
Type | Bug |
Priority | Medium |
Status | Closed |
Resolution | Fixed |
Resolution Date | 2018-02-28T18:17:14.000+0000 |
Affected Version/s | Release 6.0.0 |
Fix Version/s | Release 7.1.0 |
Components | Android |
Labels | android, blob, media, preview |
Reporter | Joshua Quick |
Assignee | Gary Mathews |
Created | 2017-12-20T02:33:36.000+0000 |
Updated | 2018-03-02T16:40:28.000+0000 |
Description
*Summary:*
The media.previewImage() method will crash if given a blob whose image is not file based, such as a screenshot returned by View.toImage().
*Steps to Reproduce:*
Build and run the below code on an Android device.
Tap the "Preview" button.
Select an app to preview the screenshot, if asked.
var window = Ti.UI.createWindow();
var button = Ti.UI.createButton({ title: "Preview" });
button.addEventListener("click", function(e) {
window.toImage(function(imageBlob) {
if (!imageBlob) {
return;
}
Ti.Media.previewImage(
{
image: imageBlob,
success: function(e) {
Ti.API.info("@@@ Successfully displayed preview.");
},
error: function(e) {
Ti.API.error("@@@ Failed to display preview");
},
});
});
});
window.add(button);
window.open();
*Result:*
App crashes with the following stack trace...
W/System.err( 5003): java.lang.ClassCastException: byte[] cannot be cast to org.appcelerator.titanium.io.TiBaseFile
W/System.err( 5003): at ti.modules.titanium.media.MediaModule.previewImage(MediaModule.java:1148)
W/System.err( 5003): at org.appcelerator.kroll.runtime.v8.V8Function.nativeInvoke(Native Method)
W/System.err( 5003): at org.appcelerator.kroll.runtime.v8.V8Function.callSync(V8Function.java:55)
W/System.err( 5003): at org.appcelerator.kroll.runtime.v8.V8Function.call(V8Function.java:41)
W/System.err( 5003): at org.appcelerator.kroll.runtime.v8.V8Function$1.run(V8Function.java:68)
W/System.err( 5003): at android.os.Handler.handleCallback(Handler.java:615)
W/System.err( 5003): at android.os.Handler.dispatchMessage(Handler.java:92)
W/System.err( 5003): at android.os.Looper.loop(Looper.java:137)
W/System.err( 5003): at android.app.ActivityThread.main(ActivityThread.java:4745)
W/System.err( 5003): at java.lang.reflect.Method.invokeNative(Native Method)
W/System.err( 5003): at java.lang.reflect.Method.invoke(Method.java:511)
W/System.err( 5003): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
W/System.err( 5003): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
W/System.err( 5003): at dalvik.system.NativeStart.main(Native Method)
*Expected Result:*
Should not crash. Should either display the in-memory image or invoke the error callback stating that the given blob must be file based. Ideally, the given memory based blob should be stored to a temp file and displayed.
*Work-Around:*
Titanium developer can save the blob to a temp file and preview the file instead.
*Note:*
The following pending PR will fix the "TiBaseFile" exception mentioned in the above stack trace...
https://github.com/appcelerator/titanium_mobile/pull/9223
However, the Java code proceeding the image.getData() method call in MediaModule.previewImage() still needs to be changed to handle a blob that'll return a null for its native file path.
https://github.com/appcelerator/titanium_mobile/blob/7_0_X/android/modules/media/src/java/ti/modules/titanium/media/MediaModule.java#L1148
The documentation for [PreviewImageOptions](http://docs.appcelerator.com/platform/latest/#!/api/PreviewImageOptions-property-image) states _'Must be a blob based on a file, such as from Titanium.Filesystem.File.read'_ The native path of TiBlob is used to create the preview intent.
It still shouldn't crash. Perhaps log a warning and no-op instead? Or save to temp file (that we clean up later) and then preview it? Here's a real case where this issue can happen. If you use Ti.Media.openPhotoGallery() to select a photo, but the photo comes from the cloud, then the gallery's listener might return the photo in memory-only blob form here... https://github.com/appcelerator/titanium_mobile/blob/master/android/modules/media/src/java/ti/modules/titanium/media/MediaModule.java#L1108
master: https://github.com/appcelerator/titanium_mobile/pull/9771
7_1_X: https://github.com/appcelerator/titanium_mobile/pull/9833
Closing ticket; Verified fix in SDK Version: 7.1.0.v20180228160738 and SDK Version: 7.2.0.v20180301101144. Test and other information can be found at: Master: https://github.com/appcelerator/titanium_mobile/pull/9771 7_1_X: https://github.com/appcelerator/titanium_mobile/pull/9833