在混合Android应用程序中通过js显示图像

时间:2017-05-21 11:20:52

标签: javascript android hybrid-mobile-app

我正试图从设备中选择图像并显示它,但它崩溃了。我经常尝试和搜索,但无法解决它。

这是我的代码:

javaBridge = new JavaBridge() {
            @android.webkit.JavascriptInterface
            @Override
            public void pickImage() {
                Intent TestGalleryIntent = new Intent();
                TestGalleryIntent.setType("image/*");
                TestGalleryIntent.setAction(Intent.ACTION_GET_CONTENT);
                startActivityForResult(TestGalleryIntent,PICK_IMG_REQUEST);
            }
        };
        mWebView.addJavascriptInterface(javaBridge,"javaBridge");

,这是onActivityResult。

 @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == PICK_IMG_REQUEST && resultCode == RESULT_OK && data.getData() != null){
            Uri imageUri = data.getData();
            String[] projection = { MediaStore.Images.Media.DATA };

            Cursor cursor = getContentResolver().query(imageUri, projection, null, null, null);
            cursor.moveToFirst();

            //Log.d("TOOOOOOOOOOT", DatabaseUtils.dumpCursorToString(cursor));

            int columnIndex = cursor.getColumnIndex(projection[0]);
            String picturePath = cursor.getString(columnIndex); // returns null
            Log.d("PICTURE PATH :","-------->"+picturePath);
            mWebView.loadUrl("javascript:displayProfilePic('"+picturePath+"')");
            cursor.close();
        }
    }

,这是JavaScript。

function pickImage(){
javaBridge.pickImage();
}


function displayProfilePic(imagePath){
document.getElementById("profilePic").style.visibility = "visible";
document.getElementById("profilePic").src=imagePath;
}

这是HTML。

<div class="file-input">
   <input class="image-upload" type="file" accept="image/*" capture="camera" onclick="pickImage()" />
   <img class="image-upload" id="profilePic" src="">
  </div> 

在拿起并尝试显示后,它显示为this

0 个答案:

没有答案