javascript - 在 Android 浏览器中将图像渲染为数据流

标签 javascript android html5-canvas

我整理了一个小test web app将 HTML Canvas 转换为图像(通过使用 Nihilogic 的 canvas2image JavaScript 库),然后用生成的图像替换 Canvas 并显示一条消息,通知用户触摸(长按)该图像以保存它到他们的电话。

我遇到的问题是 Android 的默认网络浏览器(“Internet”)不呈现表示图像的 base64 编码数据流,而是显示一个问号符号。有办法解决这个问题吗?如果是,那么如何?

最佳答案

使用自定义 ContentProvider 并覆盖 openFile() 以将流作为临时文件返回。

可以在html A 标签中使用URI 作为src。

<a src="Example://file"></a>
public class ExampleProvider extends ContentProvider {

    @Override
    public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException {         
        //Get your bitmap
        Bitmap bmp = BitmapFactory.decodeResource(getContext().getResources(), R.drawable.file_example);
        File tempFile = null;
        try {
            //Create the tempfile form a stream
            tempFile = File.createTempFile("tempfile", ".thumb", getContext().getCacheDir());
            BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(tempFile));
            bmp.compress(Bitmap.CompressFormat.JPEG, 100, out);
            out.close();
            if(mode.equals("r")) {
              return ParcelFileDescriptor.open(tempFile, ParcelFileDescriptor.MODE_READ_ONLY);
            }
        }
        catch(IOException e)  {
            LOGGER.error("Couldn't generate temp file for thumb view, guid:" + guid, e);
        }
        finally {
            if(tempFile != null {
                //The unix filesystem automatically deletes the file once all handles are gone
                tempFile.delete();
            }
        }
    }
}

关于javascript - 在 Android 浏览器中将图像渲染为数据流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12113616/

相关文章:

javascript - 当属性更改时,AngularJS 绑定(bind)不起作用

css - 将图表绘制到在移动设备上响应的 html5 Canvas 中的最佳实践

android - 使用 "Parameters for notification messaging by platform GCM"时发送参数 Activity (android)

android - PreferenceCategory.removePreference() 返回 false,因此不会删除首选项

android - 从 Activity 调用 FragmentPagerAdapter

javascript - 如何提高 HTML Canvas 中的 map 渲染性能?

javascript - 如何使用 HTML、Javascript 或 CamanJS 将图像分成几 block 并重新排列?

javascript - 如何采取通过阻塞调用调度的多个操作

php - 使用Javascript动态生成文件上传框

javascript - CSS 边框不扩展整个 Div