gpt4 book ai didi

android - 将java变量从java代码传递到react-native中的javascript代码

转载 作者:行者123 更新时间:2023-11-29 01:02:15 26 4
gpt4 key购买 nike

我想将通过 Intents 在 MainActivity.java 文件中以 android 代码接收到的图像和文本传递并显示到我在 react-native 中的 JavaScript 文件中。

下面是我的MainActivity.java文件代码中通过intent获取数据的代码

@Override
protected void onCreate(Bundle savedInstanceState) {
Intent intent = getIntent();
String action = intent.getAction();
String type = intent.getType();

if (Intent.ACTION_SEND.equals(action) && type != null) {
if ("text/plain".equals(type)) {
handleSendText(intent);
} else if (type.startsWith("image/")) {
setContentView(R.layout.activity_main);
imageView = (ImageView) findViewById(R.id.sentImage);
handleSendImage(intent);
}
}else {
// Handle other intents, such as being started from the home screen
}
}


void handleSendText(Intent intent) {
String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT);
if (sharedText != null) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setMessage(sharedText);
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
}

void handleSendImage(Intent intent) {
Uri imageUri = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM);
if (imageUri != null) {
imageView.setImageURI(imageUri);
}else{

}
}

最佳答案

试试这个

import com.facebook.react.bridge.Callback;


// a callback is used to provide the function call result to JavaScript.

@ReactMethod
public void getImageNText(Callback successCallback,Callback errorCallback)

{
try{

//Do your stuff here
// let say result of text is storedin respText and image in respImg
// to send it to Javascript side
successCallback.invoke(respText,respImg);
}catch(Exception e){
errorCallback.invoke(e.getMessage());
}



}

现在在 javascript 端阅读它

"YourModuleName".getImageNText(

(text,image) => {/*successCallback*/
// do your stuff with image and
},
(error) => {// handle error} ///*errorCallback*/
);

同时给出this阅读以更好地理解

关于android - 将java变量从java代码传递到react-native中的javascript代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50229344/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com