I am having some trouble setting the value of an edit text box using the onPostExecute() method of the async task.
The background tasks takes an bitmap input and performs OCR using the Microsoft Vision API and returns the value of the OCR result.
It works on most devices however it fails to work on Galaxy Note 3 running Android 5.0.
The result of the background task can be retrieved and toasted without any errors.
Async Task
private class OCRImg extends AsyncTask<Bitmap, String, String> {
EditText gluET;
@Override
protected void onPreExecute() {
super.onPreExecute();
gluET = (EditText) ((Activity) context).findViewById(R.id.glucoseETm);
}
@Override
protected String doInBackground(Bitmap... params) {
String finalResult = "";
try {
VisionServiceClient client;
client = new VisionServiceRestClient("xxxx");
Bitmap bitmap = params[0];
ByteArrayOutputStream output = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, output);
ByteArrayInputStream inputStream = new ByteArrayInputStream(output.toByteArray());
Gson gson = new Gson();
OCR ocr = client.recognizeText(inputStream, LanguageCodes.English, true);
String result = gson.toJson(ocr);
Log.d("result", result);
if (result != null) {
OCR r = gson.fromJson(result, OCR.class);
for (Region reg : r.regions) {
for (Line line : reg.lines) {
for (Word word : line.words) {
finalResult += word.text + " ";
}
finalResult += "\n";
}
finalResult += "\n\n";
}
}
} catch (MalformedURLException ex) {
ex.printStackTrace();
} catch (ProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
Log.e("Post Error", "multipart post error " + e + "(");
}
Log.v("Final OCR Result", finalResult);
return finalResult;
}
@Override
protected void onPostExecute(String result) {
try {
result = result.replaceAll("\\D+", "");
gluET.setText(result);
Toast.makeText(context, result, Toast.LENGTH_LONG).show();
}
}
}
Layout.XML
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/glucoseFrag"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:background="?android:attr/colorBackground"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="edu.tp.SWEN.GlucoseFragment">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/viewPager"
android:gravity="top"
android:stretchColumns="0,1">
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:stretchColumns="0,1">
<EditText
android:id="@+id/glucoseETm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_span="2"
android:ems="10"
android:hint="Enter glucose reading (mmol/dl)"
android:inputType="numberDecimal"
android:singleLine="true" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn_camera"
android:src="@drawable/ic_camera_alt_grey_700_18dp"
android:background="@color/white"/>
</TableRow>
</TableLayout>
</ScrollView>
</FrameLayout>
Method calling the async task
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 4;
glucose = (EditText) view.findViewById(R.id.glucoseET);
Bitmap bitmap = (Bitmap) data.getExtras().get("data");
new OCRImg().execute(bitmap)
}
}
EDIT Looking at the stack trace I see this line appearing on the phones that cannot set the text. It doesn't appear on phones that can setText().
`0`6-2`3 14:09:21.339 10547-10547/com.test.app W/FileUtils: Failed to chmod(/storage/emulated/0/com.test.app/SWEN): android.system.ErrnoException: chmod failed: EPERM (Operation not permitted)`