i need to get jpg from arduino host to android - android-studio

So i need to get picture from host, arduino is host with the esp32 and the camera. Arduino is hosting picture on local wifi and I can access that picture by typing ip. Now I need help with the android studio part to get the image from the ip.
So this is the code from android studio and its not working.
public class MainActivity extends AppCompatActivity {
private ImageView imageView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = findViewById(R.id.image_view);
new DownloadImageTask().execute("http://192.168.1.8/");
}
private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
#Override
protected Bitmap doInBackground(String... urls) {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(urls[0])
.build();
try (Response response = client.newCall(request).execute()) {
if (!response.isSuccessful()) {
throw new IOException("Unexpected code " + response.code());
}
byte[] bytes = response.body().bytes();
return BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
#Override
protected void onPostExecute(Bitmap bitmap) {
imageView.setImageBitmap(bitmap);
}
}
}

Related

how to open link from webview?

I'm new, just learning, don't swear. There is a task. A line comes in, it contains an HTML file with text and links, the webView layout is loaded, I display a line in the webView that contains a link, do I need to click on this link so that it opens in the browser on the phone?
public class MainActivity extends AppCompatActivity {
private String unEncodedHtml = "Hello https://nba.com";
#SuppressLint("SetJavaScriptEnabled")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
WebView webView = new WebView(this);
setContentView(webView);
String encodedHtml = Base64.encodeToString(unEncodedHtml.getBytes(), Base64.NO_PADDING);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setSupportZoom(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.setWebViewClient(new myWebViewClient());
webView.loadData(encodedHtml, "text/html", "base64");
String url = webView.getUrl();
Log.d("zzz", "url: "+url);
}
public class myWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
view.loadUrl(url);
return true;
}
}
}
try this.....
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.contains("your_display_line")) {
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(browserIntent);
return true;
} else {
view.loadUrl(url);
return true;
}
}

How to make progress bar invisible after load image?

When my app is installed I want to see a progress bar until the image is loaded by an API call. After that, I want to make it invisible.
Here is the code:
public class MainActivity extends AppCompatActivity {
private ImageView imageView;
// ...
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView=findViewById(R.id.imageView);
loud();
}
private void loud(){
setProgressBarVisibility(true);
RequestQueue queue = Volley.newRequestQueue(this);
String url ="https://meme-api.herokuapp.com/gimme";
// Request a string response from the provided URL.
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
String url= null;
try {
url = response.getString("url");
} catch (JSONException e) {
e.printStackTrace();
}
setProgressBarVisibility(false);
Glide.with(MainActivity.this).load(url).into(imageView);
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.d("ERROR ","Error ");
}
});
queue.add(jsonObjectRequest);
}

prioritise Async task over web view

My android application has web-view and control buttons, web view streams live video from raspberry pi camera and on button press the traction commands(on,off,right,left) goes to the redis-DB on the same raspberry-pi
my observations is when web view streams video the control commands on button pressed reach pi at very slow speed
my priority is control first and then streaming how to accomplish the same?
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.loadUrl("http://192.168.0.190:8000");
Log.e("aaa","Streaming video");
}
//on button clicked
public void StartTraction(View view) {
MyAsyncTaskStart myAsyncTasks = new MyAsyncTaskStart();
myAsyncTasks.execute();
}
public class MyAsyncTaskStart extends AsyncTask<String, String, String> {
#Override
protected String doInBackground(String... strings) {
try {
Jedis jedis = new Jedis("192.168.0.190", 6379);
jedis.set("motorKey","ON");
jedis.disconnect();
} catch (JedisConnectionException e) {
} catch (NullPointerException e1) {
} catch (Exception e1) {
}
return "";
}
#Override
protected void onPostExecute(String s) {
}
}
}

If Statement Not Executed in AutocompleteTextView

I'm new to Android Studio and currently working on my first app. Thanks to numerous examples on here i have managed to catch up on the basics very quickly. I now have a problem with my autocompletetextview, i want to get the text selected then use it in a if statement to determine what is shown on the "results" textview. The if statement works fine but only for the "else" part and i can't figure out where i went wrong.
I have commented out the onItemClickListener because the "testfoodph" method does the same.
Here is the activity code:
public class phTester extends AppCompatActivity {
Intent intent = getIntent();
AutoCompleteTextView text;
MultiAutoCompleteTextView text1;
TextView results;
String foodies;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ph_tester);
String[] foods = getResources().getStringArray(R.array.foodlist);
text=(AutoCompleteTextView)findViewById(R.id.autoCompleteTextView);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,foods);
text.setAdapter(adapter);
text.setThreshold(1);
results=(TextView)findViewById(R.id.phResults);
foodies = text.getText().toString();
text.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//if (foodies.equalsIgnoreCase("Mango")) {
//results.setText(getString(R.string.phAlk));
}
//else {
//results.setText(getString(R.string.Error));
//}
//}
});
}
public void TestFoodPH(View view) {
if (foodies.equalsIgnoreCase("Mango")) {
results.setText(getString(R.string.phAlk));
}
else {
results.setText(getString(R.string.Error));
}
}

It leaks when I startLeScan in onCreate and stopLeScan in onDestroy

I run my code and rotate the phone couple of times, then dump memory and analyze it.
Below is my code:
private BluetoothAdapter.LeScanCallback mLeScanCallback = new BluetoothAdapter.LeScanCallback() {
#Override
public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) {
LogUtils.e("111");
}
};
private boolean mScanning = false;
private BluetoothManager bm;
private void scanLeDevice(final boolean enable) {
LogUtils.e(enable);
try {
if (enable) {
mScanning = true;
if(bm.getAdapter()!=null)bm.getAdapter().startLeScan(mLeScanCallback);
} else {
mScanning = false;
if(bm.getAdapter()!=null)bm.getAdapter().stopLeScan(mLeScanCallback);
}
invalidateOptionsMenu();
}catch (Throwable e){
}
}
#Override
protected void onDestroy() {
super.onDestroy();
scanLeDevice(false);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initStage();
}
#Override
protected void initStage() {
bm = (BluetoothManager)getSystemService(Context.BLUETOOTH_SERVICE);
scanLeDevice(true);
}
The java heap:
The LeScanCallback is holding a reference to the Activity. I just ran into this when testing the BluetoothLeGatt sample provided by Google. I copied the scanning code into my app and I suddenly found massive leaks occurring.
I solved it by wrapping the scan callback into a static class which then holds a weak reference to the activity. Much like Google reccomends when using a Handler. Like this:
private final BluetoothAdapter.LeScanCallback mLeScanCallback = new LeScanCallbackClass(this);
private static final class LeScanCallbackClass implements BluetoothAdapter.LeScanCallback {
private String TAG = makeLogTag(LeScanCallbackClass.class.getName());
private final WeakReference<TrackActivity> mAct;
public LeScanCallbackClass(TrackActivity act) {
mAct = new WeakReference<>(act);
}
#Override
public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) {
LOGI(TAG, String.format("BLE LeScan Result: %s", device.getAddress()));
final TrackActivity act = mAct.get();
act.runOnUiThread(new Runnable() {
#Override
public void run() {
mLeDeviceListAdapter.addDevice(device);
mLeDeviceListAdapter.notifyDataSetChanged();
}
});
act.sendNotification();
}
}

Resources