Problem
I've come across a nasty bug when using openPhotoGallery() where accessing the returned media.nativePath causes a NullPointException. Now, this only happens when the selected media, for some reason, appears to become a remote media (i.e. after automatic backup of Photo Gallery).
Even weirder, if I use an ImageView to display the image, it is displayed correctly. Initially I hit the bug when trying to upload the selected image. I was then able to reproduce the bug with this minimal code:
Test case
1. Create a new Default Alloy Project
2. Replace the initial doClick() function, within index.js:
function doClick(e){
Titanium.Media.openPhotoGallery({
success:function(event) {
try {
Ti.API.info('event.media.nativePath: ' + event.media.nativePath);
}
catch(e) {
Ti.API.error(e.message);
}
Ti.API.info('event.media.file.nativePath: ' + event.media.file.nativePath);
}
});
};
$.index.open();
3. Select a picture (using Google Photos, now the default on Android L) that has been backed up to the Cloud
Logs
Output for a picture before it has been backed up / local picture:
[INFO] : event.media.nativePath: file:///storage/emulated/0/DCIM/Camera/IMG_20141210_232913.jpg
[INFO] : event.media.file.nativePath: content://media/external/images/media/20559
Output for a picture after it has been backed up / remote picture:
[ERROR] : Attempt to invoke virtual method 'char[] java.lang.String.toCharArray()' on a null object reference
[INFO] : event.media.file.nativePath: content://com.google.android.apps.photos.content/0/https%3A%2F%2Flh6.googleusercontent.com%2FxAiJOX8hSsSutQeA6X96og8RzpRo2PyOtWi_49FaNSMv%3Ds0-d
I've searched through the QA and I've found this unanswered question, where the user seems to be having the same problem: http://developer.appcelerator.com/question/176856/nullpointerexception-when-uploading-a-file As you can see, the cause of the problem seems to be trying to get the path of a remote image (either Picasa or backed up Google Photos).
I get the same crash while trying to resize an image with imageAsResized. I found a work around, by saving the image to the filesystem and reading it back in as a blob you are then able to use imageAsResized without a crash. Not 100% sure this is that same bug but it seems to be.
That's the way to handle this: http://dimitar.me/how-to-get-picasa-images-using-the-image-picker-on-android-devices-running-any-os-version/ I added something like this to titanium locally.
Hi, I'm having the same issue. Any ETA on this fix or any advice on a workaround? Thanks, Avi
@grebulon Would you mind sharing your work around? Yeah an ETA on this fix would be great since it's been four months already.
I have a really hacky work around. It will break if Google changes the scheme for their content url. if(OS_ANDROID) { var regex = /content\:\/\/com\.google\.android\.apps\.photos\.content\/\d\/(http.+)/i; var fileNativePath = event.media.file.nativePath; var match = fileNativePath.match(regex); if(match && match.length > 0) { var remoteImageUrl = decodeURIComponent(match[1]); imageView.setImage(remoteImageUrl); } else { imageView.setImage(event.media); } }
@aviram Thanks! Using your work around now which works great for now. Hope the bug gets fixed before the work around stops working.
[~rtlechuga] Why was this closed as fixed?