[英]how to send base64 string in android?
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. 我正在尝试制作
HTML
页面上有一个“按钮”的液压应用程序。每当我单击该按钮时,我都想调用android代码函数(当我从javascript调用test
函数时,它工作正常) 。现在,我想单击按钮捕获图像单击我从URL下面获取帮助。 Capture Image from Camera and Display in Activity I do like this package com.example.myapp.myapplication; 从相机捕获图像并在活动中显示我确实喜欢这个包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://1.5.20.97:3671");
webView.loadUrl("http://15.16.74.160:6019");
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 我收到导入错误
I am trying to call a android function which open camera and capture image and return to javascript. 我正在尝试调用打开相机并捕获图像并返回到javascript的android函数。
on javascript I am calling function like this 在JavaScript上,我正在这样调用函数
console.log(window.JSInterface.test())
So to go back to your webView
when you capture the image from camera, change your onActivityResult
method to this 因此,当您从相机捕获图像时要返回到
webView
,请将onActivityResult
方法更改为
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST && resultCode == Activity.RESULT_OK) {
photo = (Bitmap) data.getExtras().get("data");
if (webView != null)
webView.loadUrl("http://125.16.74.160:30019");
}
}
Also you should declare you bitmap
as a global static variable like this 同样,您应该将
bitmap
声明为这样的全局静态变量
public static Bitmap photo; 公共静态位图照片;
Then bitMapToBase64
become like this 然后
bitMapToBase64
变成这样
@JavascriptInterface
public String bitMapToBase64()
{
if (MainActivity.photo != null) {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
//add support for jpg and more.
MainActivity.photo.compress(Bitmap.CompressFormat.PNG, 50, byteArrayOutputStream);
byte[] byteArray = byteArrayOutputStream .toByteArray();
String encoded = Base64.encodeToString(byteArray, Base64.DEFAULT);
return encoded;
}
return "null image";
}
In the javascript side you check if bitMapToBase64 function has a return value different than "null image" you convert the base64 image to a blob or image object and show it in a div for example. 在javascript端,您检查bitMapToBase64函数的返回值是否不同于“空图像”,您可以将base64图像转换为Blob或图像对象,并在div中显示它。
Here is an example how to convert your base64 string to an image object and append it to your body 这是一个如何将base64字符串转换为图像对象并将其附加到您的身体的示例
var image = new Image();
image.src = 'data:image/png;base64,iVBORw0K...';
document.body.appendChild(image);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.