简体   繁体   English

Android手机上的canvas.toDataURL和drawimage问题

[英]trouble with canvas.toDataURL and drawimage on Android phone

I want to save an image from canvas to my Android phone. 我想将图片从画布保存到我的Android手机。 My code is as follows 我的代码如下

 var canvas = document.getElementById('canvas');
 var context = canvas.getContext("2d");
 context.drawImage(video, 0, 0, 320, 320);
 var strURI=canvas.toDataURL('image/png');
 document.location.href=strURI.replace('image/png', 'image/octet-stream');

The code works fine on my laptop and I can download the image with it. 代码在我的笔记本电脑上工作正常,我可以用它下载图像。 However, when I try it on my Android phone, nothing happens. 但是,当我在我的Android手机上试用它时,没有任何反应。

Today, I try to debug my code on my Android phone. 今天,我尝试在Android手机上调试我的代码。 I find out that my app crashes when it is at var strURI=canvas.toDataURL('image/png') . 我发现我的应用程序在var strURI=canvas.toDataURL('image/png')时崩溃了。 That's why I cannot download the image on my phone. 这就是我无法在手机上下载图像的原因。 Anyone know how to fix the problem? 有谁知道如何解决这个问题? Thank you very much. 非常感谢你。

Hmm, it works just fine on my Galaxy Nexus. 嗯,它在我的Galaxy Nexus上运行得很好。 I just used the code that I made to answer another question here: 我刚刚使用我所做的代码来回答另一个问题:

http://jsfiddle.net/7QMqX/2/ http://jsfiddle.net/7QMqX/2/

Does that code work for you? 这段代码对你有用吗?

If it does not create a popup with the image, then you have an android-specific problem, probably version specific. 如果它没有创建带有图像的弹出窗口,那么你有一个特定于Android的问题,可能是版本特定的。

If it DOES work for you then you probably have a cross-origin problem with your specific image, and it is throwing a security exception on your phone because the image location is not of the same origin as the page. 如果它适合您,那么您的特定图像可能存在跨域问题,并且它会在您的手机上引发安全例外,因为图像位置与页面的原点不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM