0

さて、私のWebビューアクティビティに表示されているWebページのボタンクリックでWebビューをキャプチャし、SDカードに保存しようとしています。

私はマニフェストに外部ストレージを書き込むための適切な権限を持っており、画像をキャプチャしようとしたときに、すべてのWebビュー機能がうまく機能しています。

 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();
                }               

このエラーメッセージが表示されます。(更新しました)

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)
4

2 に答える 2

2

openFileOutput()は、ファイルがアプリのプライベートデータ領域に作成されることを前提としているため、パス区切り文字なしのファイル名のみを想定しています(メモリが私をだまさない場合は、/ data / data / [your appパッケージ名]/files /)。

sdcardにファイルを作成するには、FileOutputStreamを直接使用できます。例については、こちらを参照してください

また、別のアドバイスとして、/ mnt / sdcardのようなハードコードされたパスは使用しないでください。sdcardの絶対パスはデバイスによって異なる可能性があるため、代わりにEnvironment.getExternalStorageDirectory()を使用する必要があります。

于 2012-10-15T03:43:14.243 に答える
1

このコードの問題を解決して、メニュー内から画面をキャプチャするようにしました

public boolean onOptionsItemSelected (MenuItem item){ 
        // Called when you tap a menu item
            switch (item.getItemId()){
            case R.id.settings_capture:
                item.setIcon(R.drawable.capture);                   
                //Resize the webview to the height of the webpage
                int pageHeight = web.getContentHeight();
                LayoutParams browserParams = web.getLayoutParams();
                web.setLayoutParams(new     LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, pageHeight));

                //Capture the webview as a bitmap
                web.setDrawingCacheEnabled(true);
                Bitmap bitmap =    Bitmap.createBitmap(web.getDrawingCache());
                web.setDrawingCacheEnabled(false);

                //Create the filename to use
                String randomFilenamepart = String.valueOf(new   SecureRandom().nextInt(1000000));
                String filename =   Environment.getExternalStorageDirectory().toString() + "/Enlighten_Mobile_" +   randomFilenamepart + ".jpg";

                File imageFile = new File(filename);
                //Stream the file out to external storage as a JPEG
                OutputStream fout = null;
                try {
                    fout = new FileOutputStream(imageFile);
                    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fout);
                    fout.flush();
                    fout.close();
                    Toast.makeText(WebViewClientDemoActivity.this,   "Screen Capture Saved!\n\nImage Saved at location : /sdcard\n\nSaved As:   Enlighten_Mobile_xxxxx.jpg", Toast.LENGTH_LONG).show();
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                    Toast.makeText(WebViewClientDemoActivity.this,   "Problem with Capturing Image or Location to Store Image", Toast.LENGTH_SHORT).show();
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    web.setLayoutParams(browserParams);
                }
                return true;
于 2012-10-16T06:36:12.503 に答える