【问题标题】:Titanium Unable to load bitmap. Not enough memory: Failed to allocateTitanium 无法加载位图。内存不足:分配失败
【发布时间】: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


【解决方案1】:

您面临的不是 Titanium 问题,而是 Android 问题(例如:Android:java.lang.OutOfMemoryError: Failed to allocate a 23970828 byte allocation with 2097152 free bytes and 2MB until OOM)。

发生的情况是,Android 将图像加载为位图,并且需要根据图像分辨率(图像大小)分配内存 - 对于非常大的图像 - 它不能 - 而且真的不需要按顺序在屏幕上显示它们。所以为了显示图像需要做的是在显示它们之前使它们变小 - ImageView 控件会为你做这件事。

您正在做的是尝试将图像显示为backgroundImage 用于View 控件。相反,请使用 ImageView 并将其设置为 image 属性。

【讨论】:

    猜你喜欢
    • 2020-06-25
    • 2018-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-05
    • 2012-10-10
    • 1970-01-01
    • 2013-08-24
    相关资源
    最近更新 更多