Base64编码的String不会传递给WebView

时间:2016-09-14 09:27:24

标签: javascript android android-intent webview

这一行

dojs("jQuery('#result').html('"+encoded+"')");

无效,且未在String上显示已编码的WebView。其他代码没有问题。

如果我更换

dojs("jQuery('#result').html('"+encoded+"')");

对齐
Toast.makeText(this, encoded, Toast.LENGTH_SHORT).show();

它在String上显示编码Toast

public void dojs(String command) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        webView.evaluateJavascript(command, null);
    }
    else {
        webView.loadUrl("javascript:" + command);
    }
}

public String getRealPathFromURI(Uri uri) {
    Cursor cursor = getContentResolver().query(uri, null, null, null, null);
    cursor.moveToFirst();
    int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
    return cursor.getString(idx);
}   

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == 1001 && resultCode == RESULT_OK && data != null && data.getData() != null) {
        Uri selectedImage = data.getData();
        File sfile = new File(getRealPathFromURI(selectedImage));
        int file_size = Integer.parseInt(String.valueOf(sfile.length()/1024));
        if (file_size>200) {
            dojs("jQuery('#result').html('File size is more than 200 kb')");
        } else {
            ContentResolver cR = getApplicationContext().getContentResolver();
            MimeTypeMap mime = MimeTypeMap.getSingleton();
            String type = mime.getExtensionFromMimeType(cR.getType(selectedImage));
            if (type=="jpeg" || type=="jpg" || type=="png") {
                try {
                    InputStream inputStream = new FileInputStream(sfile);
                    Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                    if (type=="png") {
                        bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
                    } else {
                        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream);
                    }
                    byte[] byteArray = byteArrayOutputStream .toByteArray();
                    String encoded = Base64.encodeToString(byteArray, Base64.DEFAULT);
                    dojs("jQuery('#result').html('"+encoded+"')");
                } catch (FileNotFoundException e) {
                    dojs("jQuery('#result').html('"+e.getMessage()+"')");
                } catch (IOException e) {
                    dojs("jQuery('#result').html('"+e.getMessage()+"')");
                }
            } else {
                dojs("jQuery('#result').html('png or jpg is accepted')");
            }
        }
    }
}

logcat没有任何错误。请建议。

0 个答案:

没有答案