当前捕获和网页视图保存到SD卡 [英] Capture current Webview and save to SD card

查看:105
本文介绍了当前捕获和网页视图保存到SD卡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

确定,所以我尝试捕捉按钮点击web视图,该网页正在显示在我的WebView活动,并将其保存到SD卡。

我在我的清单写外部存储和所有的WebView功能都工作得很好,只是当我试图捕捉图像的权利许可。

 公共无效的onClick(视图v){
    开关(v.getId()){            案例R.id.button1:
                WebViewClientDemoActivity.web.goBack();
                打破;        案例R.id.button2:
                WebViewClientDemoActivity.web.goForward();
                打破;            案例R.id.button3:
                WebViewClientDemoActivity.web.capturePicture();
                //捕获图片
            画中画= WebViewClientDemoActivity.web.capturePicture();
            //创建一个新的画布
            帆布mCanvas =新的Canvas();
            //绘制图片到画布
            picture.draw(mCanvas);
            //创建一个位图
            位图撷取画面= Bitmap.createBitmap(picture.getWidth()
            picture.getHeight(),Config.ARGB_8888);
            //弗朗帆布复制内容为位图
            //mCanvas.drawBitmap(mBitmapScreenshot,0,0,NULL);
            mCanvas.drawBitmap(撷取画面,0,0,NULL);
            //保存位图来本地文件系统
            如果(撷取画面!= NULL){
                   ByteArrayOutputStream mByteArrayOpStream =新
            ByteArrayOutputStream();
                    sreenshot.com preSS(Bitmap.Com pressFormat.JPEG,90,
            mByteArrayOpStream);                尝试{
                    文件夹=新的文件(Environment.getExternalStorageDirectory()的toString()+/启蒙/图像。);
                    folder.mkdirs();
                    //创建父目录的File对象
                    文件OUTPUTFILE =新的文件(文件夹,enlighten.jpg);
                    //现在的OutputStream附加到文件对象,而不是字符串重新presentation
                    FOS的FileOutputStream =新的FileOutputStream(OUTPUTFILE);
                    fos.write(mByteArrayOpStream.toByteArray());
                    fos.close();
                    Toast.makeText(WebViewClientDemoActivity.this,文件创建,Toast.LENGTH_SHORT).show();
                }赶上(FileNotFoundException异常五){
                    e.printStackTrace();
                    Toast.makeText(WebViewClientDemoActivity.this,找不到文件,Toast.LENGTH_SHORT).show();
                }赶上(IOException异常五){
                    e.printStackTrace();
                }

即时得到该错误消息。 (修订版)

  11月10日至15日:33:10.336:W / System.err的(20577):java.io.FileNotFoundException:/mnt/sdcard/Enlighten/Images/enlighten.jpg:开放失败:ENOENT(没有这样的文件或目录)
11月10日至15日:33:10.336:W / System.err的(20577):在libcore.io.IoBridge.open(IoBridge.java:406)
11月10日至15日:33:10.336:W / System.err的(20577):在java.io.FileOutputStream中的<&初始化GT;(FileOutputStream.java:88)
11月10日至15日:33:10.336:W / System.err的(20577):在java.io.FileOutputStream中的<&初始化GT;(FileOutputStream.java:73)
11月10日至15日:33:10.336:W / System.err的(20577):在com.jaisonbrooks.enlighten.WebViewClientDemoActivity.onClick(WebViewClientDemoActivity.java:374)
11月10日至15日:33:10.336:W / System.err的(20577):在android.view.View.performClick(View.java:3565)
11月10日至15日:33:10.336:W / System.err的(20577):在android.view.View $ PerformClick.run(View.java:14165)
11月10日至15日:33:10.336:W / System.err的(20577):在android.os.Handler.handleCallback(Handler.java:605)
11月10日至15日:33:10.336:W / System.err的(20577):在android.os.Handler.dispatchMessage(Handler.java:92)
11月10日至15日:33:10.336:W / System.err的(20577):在android.os.Looper.loop(Looper.java:137)
11月10日至15日:33:10.336:W / System.err的(20577):在android.app.ActivityThread.main(ActivityThread.java:4517)
11月10日至15日:33:10.336:W / System.err的(20577):在java.lang.reflect.Method.invokeNative(本机方法)
11月10日至15日:33:103.46:W / System.err的(20577):在java.lang.reflect.Method.invoke(Method.java:511)
11月10日至15日:33:103.46:W / System.err的(20577):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:980)
11月10日至15日:33:103.46:W / System.err的(20577):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747)
11月10日至15日:33:103.46:W / System.err的(20577):在dalvik.system.NativeStart.main(本机方法)
11月10日至15日:33:103.46:W / System.err的(20577):libcore.io.ErrnoException:产生的原因打开失败:ENOENT(没有这样的文件或目录)
11月10日至15日:33:103.46:W / System.err的(20577):在libcore.io.Posix.open(本机方法)
11月10日至15日:33:103.46:W / System.err的(20577):在libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
11月10日至15日:33:103.46:W / System.err的(20577):在libcore.io.IoBridge.open(IoBridge.java:390)


解决方案

的<一个href=\"http://developer.android.com/reference/android/content/Context.html#openFileOutput%28java.lang.String,%20int%29\"相对=nofollow> openFileOutput()预计只是文件名,不路径分隔符,因为它假设该文件将在应用程序的私有数据区(创建,如果我的记忆没有骗我,那么就应该是/数​​据/数据​​/ [您的应用程序包名] /文件/).

要创建SD卡的文件,你可以直接使用FileOutputStream中,<一个href=\"http://stackoverflow.com/questions/2130932/how-to-create-directory-automatically-on-sd-card\">see这里的一个例子。

和另一个建议是,像到/ mnt / SD卡,因为SD卡的绝对路径可能在不同的设备不同,而应该使用<一个不使用硬codeD路径href=\"http://developer.android.com/reference/android/os/Environment.html#getExternalStorageDirectory%28%29\"相对=nofollow> Environment.getExternalStorageDirectory()

ok so im trying capture the webview on button click, of the web page that is being displayed on my webview activity and save it to the sd card.

i have in my manifest the right permission to write external storage and all webview features are working just great, just when i attempt to capture the image.

 public void onClick(View v) {
    switch(v.getId()) {

            case R.id.button1:
                WebViewClientDemoActivity.web.goBack(); 
                break;

        case R.id.button2:
                WebViewClientDemoActivity.web.goForward();
                break;

            case R.id.button3:
                WebViewClientDemoActivity.web.capturePicture();
                //Capture Picture
            Picture picture = WebViewClientDemoActivity.web.capturePicture();
            //Create a new canvas
            Canvas mCanvas = new Canvas();
            //Draw the Picture into the Canvas
            picture.draw(mCanvas);
            //Create a Bitmap
            Bitmap sreenshot = Bitmap.createBitmap(picture.getWidth(),
            picture.getHeight(),Config.ARGB_8888);
            //copy the content fron Canvas to Bitmap
            //mCanvas.drawBitmap(mBitmapScreenshot, 0, 0, null);
            mCanvas.drawBitmap(sreenshot, 0, 0, null);
            //Save the Bitmap to local filesystem
            if(sreenshot != null) {
                   ByteArrayOutputStream mByteArrayOpStream = new
            ByteArrayOutputStream();
                    sreenshot.compress(Bitmap.CompressFormat.JPEG, 90,
            mByteArrayOpStream);

                try {
                    File folder = new File(Environment.getExternalStorageDirectory().toString()+"/Enlighten/Images");
                    folder.mkdirs();
                    // create a File object for the parent directory
                    File outputFile = new File(folder, "enlighten.jpg");
                    // now attach the OutputStream to the file object, instead of a String representation
                    FileOutputStream fos = new FileOutputStream(outputFile);
                    fos.write(mByteArrayOpStream.toByteArray());
                    fos.close();
                    Toast.makeText(WebViewClientDemoActivity.this, "File Created", Toast.LENGTH_SHORT).show();
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                    Toast.makeText(WebViewClientDemoActivity.this, "File Not Found", Toast.LENGTH_SHORT).show();
                } catch (IOException e) {
                    e.printStackTrace();
                }               

Im getting this error message. (UPDATED)

10-15 11:33:10.336: W/System.err(20577): java.io.FileNotFoundException: /mnt/sdcard/Enlighten/Images/enlighten.jpg: open failed: ENOENT (No such file or directory)
10-15 11:33:10.336: W/System.err(20577):    at libcore.io.IoBridge.open(IoBridge.java:406)
10-15 11:33:10.336: W/System.err(20577):    at java.io.FileOutputStream.<init>(FileOutputStream.java:88)
10-15 11:33:10.336: W/System.err(20577):    at java.io.FileOutputStream.<init>(FileOutputStream.java:73)
10-15 11:33:10.336: W/System.err(20577):    at com.jaisonbrooks.enlighten.WebViewClientDemoActivity.onClick(WebViewClientDemoActivity.java:374)
10-15 11:33:10.336: W/System.err(20577):    at android.view.View.performClick(View.java:3565)
10-15 11:33:10.336: W/System.err(20577):    at android.view.View$PerformClick.run(View.java:14165)
10-15 11:33:10.336: W/System.err(20577):    at android.os.Handler.handleCallback(Handler.java:605)
10-15 11:33:10.336: W/System.err(20577):    at android.os.Handler.dispatchMessage(Handler.java:92)
10-15 11:33:10.336: W/System.err(20577):    at android.os.Looper.loop(Looper.java:137)
10-15 11:33:10.336: W/System.err(20577):    at android.app.ActivityThread.main(ActivityThread.java:4517)
10-15 11:33:10.336: W/System.err(20577):    at java.lang.reflect.Method.invokeNative(Native Method)
10-15 11:33:10.346: W/System.err(20577):    at java.lang.reflect.Method.invoke(Method.java:511)
10-15 11:33:10.346: W/System.err(20577):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:980)
10-15 11:33:10.346: W/System.err(20577):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747)
10-15 11:33:10.346: W/System.err(20577):    at dalvik.system.NativeStart.main(Native Method)
10-15 11:33:10.346: W/System.err(20577): Caused by: libcore.io.ErrnoException: open failed: ENOENT (No such file or directory)
10-15 11:33:10.346: W/System.err(20577):    at libcore.io.Posix.open(Native Method)
10-15 11:33:10.346: W/System.err(20577):    at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
10-15 11:33:10.346: W/System.err(20577):    at libcore.io.IoBridge.open(IoBridge.java:390)

解决方案

The openFileOutput() expects just the file name, without path separators, because it assumes that the file will be created in the app's private data area (if my memory doesn't cheat me then it should be /data/data/[your app package name]/files/).

To create a file on sdcard you can use FileOutputStream directly, see here for an example.

And another advice is that don't use hardcoded path like /mnt/sdcard because the absolute path of sdcard may vary on different devices, instead you should use Environment.getExternalStorageDirectory()

这篇关于当前捕获和网页视图保存到SD卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆