来自file:// system或file:// storage的WebView图像源

时间:2014-09-24 18:55:50

标签: android webview

我想在 WebView 中显示图库中的图片,但有些内容无效。我的webview不希望显示包含以下内容的图像: file://storage/sdcard0/Download/obrazek.jpg file://system/media/Andy29WP.png

我的javascript桥:

private class MyJavaScriptInterface {
         @SuppressWarnings("unused")
        Context mContext;
         @SuppressWarnings("unused") 
        private static final int SELECT_PICTURE = 1;
            MyJavaScriptInterface(Context c) {
                mContext = c;
            }
            @JavascriptInterface
            public void imageChooser(final String id){   
                Intent intent = new Intent();
                intent.setType("image/*");
                intent.setAction(Intent.ACTION_GET_CONTENT);
                startActivityForResult(Intent.createChooser(intent, "Select Picture"),1);
                String imagePath = "";
                String[] imgData = { MediaStore.Images.Media.DATA };

                Cursor imgCursor = getContentResolver().query(m_image_uri, imgData, null, null, null);
                if(imgCursor!=null) {
                    int index = imgCursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
                    imgCursor.moveToFirst();
                    imagePath = imgCursor.getString(index);
                }
                else
                    imagePath = m_image_uri.getPath();

                m_image_path = imagePath;
                String test_image_path = "file:/" + m_image_path;
                Log.i("url obrazka", "url obrazka" + test_image_path ); 
                m_web_view.loadUrl("javascript:changeImage(\""+ id + "\",\"" + test_image_path +"\")");
            }
        }

我的onActivityResult:

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) { 
            if (resultCode == RESULT_OK) {
                    if (requestCode == 1) {
                            // currImageURI is the global variable I'm using to hold the content:// URI of the image
                            m_image_uri = data.getData();
                    }
            }
    }

应用: Image not displaying in browser

1 个答案:

答案 0 :(得分:0)

图像路径应该有三个斜杠而不是两个斜杠。

旧:

file://storage/sdcard0/Download/obrazek.jpg 

新:

file:///storage/sdcard0/Download/obrazek.jpg