最近在尝试做一个App,调用百度的图像识别api,希望能将返回的结果显示在所拍照片下面,之前已经在eclipse里实验成功了,只要传入照片,就会显示结果,但当我写到拍照的activity里面的onActivityResult()方法时,希望将所拍照片和结果一并显示在下面,我不确定是不是由于整个过程太慢的原因,结果一直显示为默认的值,我也尝试了同步块,进程的方法,但均无效,于是我设置了很多标记点,发现明明已经进入我的那个方法了,执行到照片转化为别的格式时,就结束了,下面的向网站传数据也根本没进行。
public synchronized void setph()throws InterruptedException{
Thread a = new Thread(new first());
a.start();
a.join();
ivPhoto.setImageURI(mCameraUri);//摆放照片
mtextview.setText(result); //将结果显示在照片下面
}
public class first implements Runnable {
@Override
public void run() {
synchronized (this) {//尝试写的同步块,也不知道是不是这样使用
result = "12";
result = Phototest.advancedGeneral("1");//我希望调用的方法,这个方法返回result结果
}
}
}
public static synchronized String advancedGeneral(String Url) {
// 请求url
String result="1111";
String url = "https://aip.baidubce.com/rest/2.0/image-classify/v2/advanced_general";
try {
// 本地文件路径
result="hi"; ..//不知道为什么,结果最终就显示为hi
Url="C:\\\\Users\\\\11420\\\\Desktop\\\\jar包\\\\鼠标.jpg";
byte[] imgData = FileUtil.readFileByBytes(Url);
result="nihao";
String imgStr = Base64Util.encode(imgData);
String imgParam = URLEncoder.encode(imgStr, "UTF-8");
String param = "image=" + imgParam;
String accessToken = "24.e15................";
result = HttpUtil.post(url, accessToken, param);
return result;
} catch (Exception e) {
e.printStackTrace();
}finally {
return result;
}
}