java - 从 WebView 保存图像

标签 java android canvas webview android-bitmap

我会尽量简短-所以我正在制作这个应用程序,其中图像上传到自定义Webview,绘制 Canvas 并保存它。现在我成功地执行了一切,直到保存它。我尝试了很多不同的方法,但都没有奏效。

我从 Url 保存未编辑的图像:

public void DownloadFromUrl(String fileName) {  //this is the downloader method
    try {
        URL url = new URL(wv2.getUrl()); //you can write here any link
        File file = new File(fileName);
        Canvas canvas = new Canvas();
        wv2.draw(canvas );

        long startTime = System.currentTimeMillis();                   
        Log.d("ImageManager", "download begining");
        Log.d("ImageManager", "download url:" + url);
        Log.d("ImageManager", "downloaded file name:" + fileName);
        URLConnection ucon = url.openConnection();

        InputStream is = ucon.getInputStream();
        BufferedInputStream bis = new BufferedInputStream(is);

        ByteArrayBuffer baf = new ByteArrayBuffer(50);
        int current = 0;
        while ((current = bis.read()) != -1) {
            baf.append((byte) current);
        }

        FileOutputStream fos = new FileOutputStream(file);
        fos.write(baf.toByteArray());
        fos.close();
        Log.d("ImageManager", "download ready in"
                        + ((System.currentTimeMillis() - startTime) / 1000)
                        + " sec");
    } catch (IOException e) {
        Log.d("ImageManager", "Error: " + e);
    }
}

我在自定义 webview 中的绘制函数如下所示:

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw (canvas);
    /*code that draws different stuff*/
}

要显示编辑后的图像,我想我需要使用这个:

canvas = new Canvas(mutableBitmap);                 
wv2.draw(canvas);
tstImage.setImageBitmap(mutableBitmap);

但同样,我不知道如何将 Canvas 合并到图像中。我应该使用绘图缓存吗?如果是,那么如何?

[更新]

好的,所以我已经设法使用此代码保存了编辑后的图像

wv2.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(wv2.getDrawingCache());
wv2.setDrawingCacheEnabled(false);
tstImage.setImageBitmap(bitmap);
SaveImage(bitmap);

保存图片的位置:

private void SaveImage(Bitmap finalBitmap) {
    String root = Environment.getExternalStorageDirectory().toString();
    File myDir = new File(root + "/Pictures/");    
    myDir.mkdirs();

    String fname = "Image-"+ count +".jpg";
    File file = new File (myDir, fname);
    if (file.exists ()) file.delete (); 
    try {
        FileOutputStream out = new FileOutputStream(file);
        finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
        out.flush();
        out.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

我唯一的问题是我得到的图像是当时 web View 中显示的图像。因此,如果我缩放图像,它只会保存一部分。

[更新 2]

我已经修改了一个答案以寻找解决方案,这就是我所写的。没有图像被显示。

   File imageFile = new File("file://" +    
    Environment.getExternalStorageDirectory() + "/Pictures/" + count + ".jpg");
                        String location = new String("file://" + Environment.getExternalStorageDirectory() + "/Pictures/" + count + ".jpg");
                        if(imageFile.exists()){
                            Bitmap myBitmap = BitmapFactory.decodeFile(location);
                            Bitmap mutableBitmap = myBitmap.copy(Bitmap.Config.ARGB_8888, true);
                            Canvas canvas = new Canvas(mutableBitmap);
                            wv2.draw(canvas);
                            tstImage.setImageBitmap(mutableBitmap);
                        }

最佳答案

试试这个

Bitmap workingBitmap = Bitmap.createBitmap(chosenFrame);
Bitmap mutableBitmap = workingBitmap.copy(Bitmap.Config.ARGB_8888, true);
Canvas canvas = new Canvas(mutableBitmap);

关于java - 从 WebView 保存图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34187752/

相关文章:

java - 如何禁用添加时区

运行时后未创建的 Android fragment

java - 如何自行删除可执行Jar文件?

java - 如何在 ImageView 中显示捕获的图像?

android - 任务 ':YAMSTestApp:preDexDebug' 执行失败

html - 如何将颜色设置为圆形 Canvas 的边框

javascript - html5 Canvas 圆形调色板

javascript - 我的游戏自己玩?

java - 带引号的字符串输出

java - Android 中 undefined reference native 库