Having trouble with setting an image url to an image view. I have this, but it doesn't work:
try {
URL url = new URL(pathToImage);
InputStream is = new BufferedInputStream(url.openStream());
Bitmap bmp = BitmapFactory.decodeStream(is);
is.close();
imageView.setImageBitmap(bmp);
}
catch (Exception e) {
Toast.makeText(context, e.getMessage(), Toast.LENGTH_SHORT).show();
}
Also tried this:
try {
URL url = new URL(pathToImage);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap bitmap = BitmapFactory.decodeStream(input);
imageView.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
return null;
}
but get a runtime error telling me I must either set a text or a view?
Another test which returns an empty runtime error message:
@SimpleFunction("loads image from url in image component")
public void GetImage(Image imageComponent, String imageUrl) {
ImageView imageView = (ImageView) imageComponent.getView();
try {
InputStream is = new java.net.URL(imageUrl).openStream();
Bitmap bitmap = BitmapFactory.decodeStream(is);
imageView.setImageBitmap(bitmap);
} catch (IOException e) {
Toast.makeText(context, "Could not load Bitmap from url", Toast.LENGTH_SHORT).show();
}
}
I have tried a few other methods on offer as well, and these fail too.
Also, I need to put working code into an async task.