错误存储器A / libc:致命信号11(SIGSEGV)位于0x00000008(代码= 1),线程22151(WebViewCoreThre)

时间:2019-02-15 18:54:59

标签: javascript java android android-studio webview

我在应用程序中使用webview,我想传输输入类型的文件,因此当我在webview中单击此文件时,我在sdcard中选择了图像,因此我想使用javascript将此图像传输到base64,以获取该文件base64并继续工作它在我的源Java中,并使用base64这个源设置我的imageview,我希望你能理解我的意思,因为英语不是我的母语。

此java文件:

static WebView webView;
private ValueCallback<Uri> mUploadMessage;
public ValueCallback<Uri[]> uploadMessage;
public static final int REQUEST_SELECT_FILE = 100;
private final static int FILECHOOSER_RESULTCODE = 1;

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view=inflater.inflate(R.layout.webview_fragment,container,false);

    webView=view.findViewById(R.id.webview);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setAllowFileAccess(true);
    webView.getSettings().setAllowContentAccess(true);
    webView.getSettings().setBuiltInZoomControls(true);
    //webView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
    webView.clearCache(false);
    webView.addJavascriptInterface(new DB(),"AndroidFunction");

    webView.loadUrl("file:///android_asset/register/index.html");




    webView.setWebChromeClient(new WebChromeClient() {
    // For 3.0+ Devices (Start)
    // onActivityResult attached before constructor
    protected void openFileChooser(ValueCallback uploadMsg, String acceptType)
    {
        mUploadMessage = uploadMsg;
        Intent i = new Intent(Intent.ACTION_GET_CONTENT);
        i.addCategory(Intent.CATEGORY_OPENABLE);
        i.setType("image/*");
        startActivityForResult(Intent.createChooser(i, "File Browser"), FILECHOOSER_RESULTCODE);
    }


    // For Lollipop 5.0+ Devices
    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public boolean onShowFileChooser(WebView mWebView, ValueCallback< Uri[]> filePathCallback, WebChromeClient.FileChooserParams fileChooserParams)
    {
        if (uploadMessage != null) {
            uploadMessage.onReceiveValue(null);
            uploadMessage = null;
        }

        uploadMessage = filePathCallback;

        Intent intent = fileChooserParams.createIntent();
        try
        {
            startActivityForResult(intent, REQUEST_SELECT_FILE);
        } catch (ActivityNotFoundException e)
        {
            uploadMessage = null;
            Toast.makeText(getActivity().getApplicationContext(), "Cannot Open File Chooser", Toast.LENGTH_LONG).show();
            return false;
        }
        return true;
    }

    //For Android 4.1 only
    protected void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture)
    {
        mUploadMessage = uploadMsg;
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        intent.addCategory(Intent.CATEGORY_OPENABLE);
        intent.setType("image/*");
        startActivityForResult(Intent.createChooser(intent, "File Browser"), FILECHOOSER_RESULTCODE);
    }


    protected void openFileChooser(ValueCallback<Uri> uploadMsg)
    {
        mUploadMessage = uploadMsg;
        Intent i = new Intent(Intent.ACTION_GET_CONTENT);
        i.addCategory(Intent.CATEGORY_OPENABLE);
        i.setType("image/*");
        startActivityForResult(Intent.createChooser(i, "File Chooser"), FILECHOOSER_RESULTCODE);
    }
});
    return view;
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent intent)
{
    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
    {
        if (requestCode == REQUEST_SELECT_FILE)
        {
            if (uploadMessage == null)
                return;
            uploadMessage.onReceiveValue(WebChromeClient.FileChooserParams.parseResult(resultCode, intent));
            uploadMessage = null;
        }
    }
    else if (requestCode == FILECHOOSER_RESULTCODE)
    {
        if (null == mUploadMessage)
            return;
        // Use MainActivity.RESULT_OK if you're implementing WebView inside Fragment
        // Use RESULT_OK only if you're implementing WebView inside an Activity
        Uri result = intent == null || resultCode != MainActivity.RESULT_OK ? null : intent.getData();
        mUploadMessage.onReceiveValue(result);
        mUploadMessage = null;
    }
    else
        Toast.makeText(getActivity().getApplicationContext(), "Failed to Upload Image", Toast.LENGTH_LONG).show();
}

}

DB类{

public DB(){

}

@JavascriptInterface
public void addToDB(String name, String lastName, String email, String pwd, String yearChild, String gender, Base64 srcImg){

    System.out.println(" le nom est: "+name+" prenom est: "+lastName+" email est: "+email+" password est: "+pwd+" le year child est: "+yearChild+" le gender est: "+gender);

}

}

这是我输入的图像类型图像:

    <input type="file" name="name" id="image" placeholder="Photo" required/>

以及该javascript代码:

<script>

function stor(){

AndroidFunction.addToDB(encodeImageFileAsURL());

}

function encodeImageFileAsURL(){

var filesSelected = document.getElementById("image").files;
var srcData;

if (filesSelected.length > 0) {
  var fileToLoad = filesSelected[0];

  var fileReader = new FileReader();

  fileReader.onload = function(fileLoadedEvent) {
     srcData = fileLoadedEvent.target.result; // <--- data: base64

  }
  fileReader.readAsDataURL(fileToLoad);
  return srcData;

}

}

解决此问题的任何方法,base64的替代解决方案或替代方法,

0 个答案:

没有答案