Problem Description
On Android, *Ti.Media.saveToPhotoGallery()* saves a file or a blob without any file extension, which makes it impossible for the photo gallery to recognize the file.
The issue can be reproduced with the following code, with the picture test.jpg being stored in project/app/assets. Note that in this example, I use *Ti.Media.saveToPhotoGallery* with a blob but I get exactly the same result with a file.
var file = Ti.Filesystem.getFile(Ti.Filesystem.resourcesDirectory, "/test.jpg");
Ti.Media.saveToPhotoGallery(file.read(), {
success: function() {
alert('success');
},
error: function() {
alert('error');
}
});
I receive a success, but the image is saved without extension and I get this message in the console:
D/MediaScannerReceiver(25405): action: android.intent.action.MEDIA_SCANNER_SCAN_FILE path: /storage/emulated/0/Pictures/TestApp/tia636479782jpeg
Steps to Reproduce:
1. Create a Classic project.
2. Paste this code in app.js file.
3. Paste a photo to the resources folder named test.jpg.
4. Run this code with the testing environment.
Test Code:
var win = Titanium.UI.createWindow({
backgroundColor : "#000",
});
var photo = Ti.UI.createButton({
title : 'Save Photo',
height : Ti.UI.SIZE,
width : Ti.UI.SIZE,
top : 20,
color : "#fff"
});
win.add(photo);
// Listen for click events.
photo.addEventListener('click', function() {
file = Ti.Filesystem.getFile(Titanium.Filesystem.resourcesDirectory, "test.jpg");
alert(file.nativePath);
Ti.Media.saveToPhotoGallery(file.read(), {
success : function() {
alert('success');
},
error : function() {
alert('error');
}
});
});
win.open();
Can anyone reproduce this issue?
confirmed on a Nexus 4 (4.4.4), SDK 3.3.0 09-05 18:05:50.721: D/MediaScannerReceiver(32662): action: android.intent.action.MEDIA_SCANNER_SCAN_FILE path: /storage/emulated/0/Pictures/testapp/tia333724776png When I look inside the folder the file is really saved without the dot (tia333724776png)
Issue reproduces Titanium SDK version 3.4.0 master, 3.3.0.GA Titanium Studio, build: 3.3.0.201407100905 Titanium Command-Line Interface CLI version 3.3.0, Android device : Motorola Moto G, Android version : 4.4.4
Issue reproduces Titanium SDK version 3.4.0. Android device: Nexus 5, Android version: 5.0.1 This issue happening when I use: . . CASE 1: photo.addEventListener('click', function() { file = Ti.Filesystem.getFile(Titanium.Filesystem.tempDirectory, "test.jpg"); alert(file.nativePath); Ti.Media.saveToPhotoGallery(file.read(), { . . . However, if I put a image on assets/android/images/, then, I make for example . . CASE 2: var imagen_prueba = Ti.UI.createImageView({ image : '/images/bandera.jpg', width : "300dp", height : "300dp" }); Ti.Media.saveToPhotoGallery(imagen_prueba.toBlob(), { . . . Using this way, work perfect!.
I solved the issue the following way: var imagen_prueba = Ti.UI.createImageView({ image : "/images/bandera.jpg", width : "50dp", height : "50dp", top : "0dp", zIndex : 2, left : "0dp" }); var imagen_view = Ti.UI.createView({ width : "300dp", height : "300dp", backgroundColor : "RED", zIndex : 1 }); imagen_view.add(imagen_prueba); $.contenedor.add(imagen_view); $.accion.addEventListener('click', function() { var blob = imagen_view.toImage(); var file = Titanium.Filesystem.getFile(Titanium.Filesystem.tempDirectory, "imagen.jpg"); if (file.write(blob.media)) { var image_tmp = Ti.UI.createImageView({ image: file.nativePath, height: "300dp", width: "300dp" }); Ti.Media.saveToPhotoGallery(image_tmp.toBlob(), { success : function(e) { Ti.API.info("listo, revisa!"); }, error : function() { Ti.API.info("Ocurrio un error!"); } }); } else { alert("No se guardo la imagen"); Ti.API.info("No se guardo la imagen"); } }); $.index.open(); . . . After to save the image I create a object image and then I make image_tmp.toBlob(), with this, the result is something like that: .....tia333724776.png :) !!!! Thanks, I wanted to shared with you. Good luck!
MediaModule uses TiMimeTypeHelper to get the mimetype of the image. This in turn uses android.webkit.MimeTypeMap which returns the extension minus the preceding ".". http://developer.android.com/reference/android/webkit/MimeTypeMap.html So, the fix is to adjust the call to TiMimeTypeHelper such that the returned value has the "." prefixed. line 372 changes from extension = TiMimeTypeHelper.getFileExtensionFromMimeType(mimetype, ".jpg"); to become: extension = "." + TiMimeTypeHelper.getFileExtensionFromMimeType(mimetype, "jpg"); This works for me.
PR: https://github.com/appcelerator/titanium_mobile/pull/6862
PR reviewed and functionally tested. Merged.
Verified the fix. The picture is saved with an extension. Closing. Environment: Appc Studio : 4.1.0.201505071004 Ti SDK : 4.1.0.v20150605164428 Ti CLI : 4.0.1 Alloy : 1.6.0 MAC Yosemite : 10.10.3 Appc npm : 4.0.0 Appc CLI : 4.0.2-rc2 Node: v0.10.37 Device - Android 5.0.1