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

And*_*iga 11 javascript android html5-canvas

我整理了一个小的测试Web应用程序,它将HTML画布转换为图像(通过使用Nihilogic的canvas2image JavaScript库),然后用生成的图像替换画布并显示一条消息,通知用户触摸(长)该图像以便把它保存到手机上.

我遇到的问题是Android的默认Web浏览器("Internet")不呈现代表图像的base64编码数据流,而是显示问号标记.有办法解决这个问题吗?如果是,那怎么样?

tsm*_*ith 4

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

您可以使用 URI 作为 html A 标记中的 src。

<a src="Example://file"></a>
Run Code Online (Sandbox Code Playgroud)
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();
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)