【发布时间】:2016-05-18 12:32:27
【问题描述】:
我即将创建一个简单的应用程序,从相机捕获照片并将其附加到 imageView :
观点
<Alloy>
<Window id="wCustomEvent" class="container">
<View layout="vertical" top="100dp">
<Label onClick='readF' id="lien" text='Image path' />
<View id="imageContainer" />
</View>
</Window>
控制器:
function writeToFile(data) {
app = {};
app.writeToFile = function(filedata) {
file = Titanium.Filesystem.getFile(Ti.Filesystem.applicationDataDirectory, 'avatar.txt');
if (!file.exists) {
file.createFile();
}
file.write(filedata, false);
Ti.App.fireEvent('datachanged', {
newdata : filedata
});
};
Ti.App.addEventListener('datachanged', function(evt) {
Ti.API.info('datachanged event' + JSON.stringify(evt));
});
//source = evt.source;
//data = source.dataText;
app.writeToFile(data);
}
function readF() {
file = Titanium.Filesystem.getFile(Ti.Filesystem.applicationDataDirectory, 'avatar.txt');
if (file.exists) {
var content = file.read();
return content.text;
}
//Ti.App.fireEvent('datachanged', {newdata:'tet'});
}
/* Android Camera */
var win = Titanium.UI.currentWindow;
$.imageContainer.addEventListener("click", function(_event) {
Titanium.Media.showCamera({
success : function(event) {
var image = event.media;
// I TRY TO RESIZE IMAGE HERE
var img = image.imageAsResized(80,80);
Ti.API.debug('Our type was: ' + event.mediaType);
Ti.Media.hideCamera();
if (event.mediaType == Ti.Media.MEDIA_TYPE_PHOTO) {
// THE IMAGE PATH
imagePath = img.nativePath;
/* Write image path to file */
writeToFile(imagePath);
/* Get image path from file */
fichier = readF();
if( $.wCustomEvent.avatarPlaceholder ){
$.wCustomEvent.remove(avatarPlaceholder);
}
var avatar = Ti.UI.createImageView({
id:'avatarPlaceholder',
width: 80,
height:80,
borderRadius:40,
backgroundColor:'red',
image : fichier
});
$.imageContainer.add(avatar);
$.imageContainer.backgroundImage = fichier;
} else {
alert("got the wrong type back =" + event.mediaType);
}
},
cancel : function() {
alert('You canceled the action.');
},
error : function(error) {
// create alert
var a = Titanium.UI.createAlertDialog({
title : 'Camera'
});
// set message
if (error.code == Titanium.Media.NO_CAMERA) {
a.setMessage('Please run this test on device');
} else {
a.setMessage('Unexpected error: ' + error.code);
}
// show alert
a.show();
},
saveToPhotoGallery : false,
showControls : true, // don't show system controls
mediaTypes : Ti.Media.MEDIA_TYPE_PHOTO,
autohide : false
});
});
当我运行它时,我可以捕捉照片,附加到视图,但我收到这条消息:
art: Throwing OutOfMemoryError "Failed to allocate a 38340876 byte allocation with 7738667 free bytes and 7MB until OOM"
[错误] : TiUIHelper: (main) [65,159991] 无法加载位图。内存不足:无法分配 38340876 字节分配,其中 7738667 可用字节和 7MB 直到 OOM
当我尝试捕捉第二张图像时,它不起作用。
这里有钛金咕噜吗?谢谢你的帮助。
【问题讨论】:
-
请在运行此应用时分享您的设备配置,我认为您的设备内存太低,甚至无法加载图像。你可以做的一件事:...1。将捕获的照片保存在外部存储卡中(如果您还没有这样做)... 2. 将此图像复制粘贴到应用程序的资产文件夹中... 3. 创建另一个图像视图并将图像属性设置为您放置的路径资产文件夹中的该图像。
标签: mobile appcelerator appcelerator-titanium