0

I am trying to make hydrid application in which there is a "button" on HTML page .whenever I click on that button i want to call android code function (which is working fine when I called test function from javascript) .Now I want to capture image on button click I take help from below url. Capture Image from Camera and Display in Activity I do like this package com.example.myapp.myapplication;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.util.Base64;
import android.util.Log;
import android.webkit.JavascriptInterface;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.widget.Toast;

import java.io.ByteArrayOutputStream;

public class MainActivity extends AppCompatActivity {
    private static final int CAMERA_REQUEST = 1888;
    private static final int MY_CAMERA_PERMISSION_CODE = 100;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        WebView webView = (WebView) findViewById(R.id.webview);
        // webView.loadUrl("http://10.5.200.97:32671");
        webView.loadUrl("http://125.16.74.160:30019");
        JavaScriptInterface jsInterface = new JavaScriptInterface(this);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.setWebChromeClient(new WebChromeClient());
        webView.addJavascriptInterface(jsInterface, "JSInterface");
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == MY_CAMERA_PERMISSION_CODE) {
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                Toast.makeText(this, "camera permission granted", Toast.LENGTH_LONG).show();
                Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(cameraIntent, CAMERA_REQUEST);
            } else {
                Toast.makeText(this, "camera permission denied", Toast.LENGTH_LONG).show();
            }
        }

    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == CAMERA_REQUEST && resultCode == Activity.RESULT_OK) {
            Bitmap photo = (Bitmap) data.getExtras().get("data");
          //  Log.d("My map",photo,"");
        }
    }

    public void checkTest(){
        if (checkSelfPermission(Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
            requestPermissions(new String[]{Manifest.permission.CAMERA}, MY_CAMERA_PERMISSION_CODE);
        } else {
            Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(cameraIntent, CAMERA_REQUEST);
        }
    }
}


class JavaScriptInterface {
    private MainActivity activity;


    public JavaScriptInterface(MainActivity activity) {
        this.activity = activity;
    }

    @JavascriptInterface
    public void test() {
        this.activity.checkTest();
    }


    @JavascriptInterface
    public String bitMapToBase64()
    {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        //add support for jpg and more.
        bitMap.compress(Bitmap.CompressFormat.PNG, 50, byteArrayOutputStream);
        byte[] byteArray = byteArrayOutputStream .toByteArray();

        String encoded = Base64.encodeToString(byteArray, Base64.DEFAULT);

        return encoded;
    }


}

I am getting import error enter image description here

I am trying to call a android function which open camera and capture image and return to javascript.

user944513
  • 12,247
  • 49
  • 168
  • 318

1 Answers1

0

You can send your image from your activty to javascript like this

First you create a method to convert your image to base64 string

@JavascriptInterface
public String bitMapToBase64()
{
  ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
  //add support for jpg and more.
  bitMap.compress(Bitmap.CompressFormat.PNG, 50, byteArrayOutputStream);
  byte[] byteArray = byteArrayOutputStream .toByteArray();

  String encoded = Base64.encodeToString(byteArray, Base64.DEFAULT);

  return encoded;
}

And in javascript you get it like this

<input type="button" value="Get from android" onClick="getFromAndroid()" />
<script type="text/javascript">
 var myVar = null;
 function getFromAndroid() {
    myVar = JSInterface.bitMapToBase64();
    alert(myVar);
 }

Amine
  • 2,241
  • 2
  • 19
  • 41