Displaying Images parsed from JSOUP in android? - android

I've been trying to develop an android app that parses through a specific (business's) website, and display the head image of the site on the top, news on the bottom, and have buttons below it that will open a new activity to display the schedule for the business, and any other information about the business. I decided to use JSOUP, which I am very new to, but I can't figure out how it is that I go about displaying images. I tried something like this, but it didn't work:
ImageView image = (ImageView) findViewById(R.id.headImage);
Document doc = Jsoup.connect("http://www.example.com/").get();
Elements divs = doc.select("img");
for (Element div : divs) {
Log.d("web Stuff",div.text());
text.setText(text.getText() + "\n" + div.text());
Element myImage = div;
String url = myImage.absUrl("src");
image.setImageDrawable(Drawable.createFromPath(url));
}
how am I supposed to correctly implement something like this?

I would load the image in a new thread if you want to do it for more than one image.
public void run() {
URL url = new URL(url);
Bitmap bitmap = BitmapFactory.decodeStream(url.openStream());
}
when the Thread is finished you can set the image of the imageview.
image.setImageBitmap(bitmap);

Drawable drw =LoadImageFromWebOperations(image_url);
im.setImageDrawable(drw);
private Drawable LoadImageFromWebOperations(String strPhotoUrl)
{
try
{
InputStream is = (InputStream) new URL(strPhotoUrl).getContent();
Drawable d = Drawable.createFromStream(is, "src name");
return d;
}catch (Exception e) {
System.out.println("Exc="+e);
return null;
}
}

Related

how to download images with bad quality?

i have a recyclerview with a imageview and textview , and i would like populate using a download in bad quality to accelerate the download process.
the imageview has a onclick method which display a dialog with image downloaded like background, so, i want download image in bad quality and only when user clicked in image , then download the image in hight quality.
this is my code for download:
try {
ruta = "http://192.168.1.67/apptequila/fotos/" + usuariojSON + "/" + fotojSON;
URL url = new URL(ruta);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setConnectTimeout(15000);
connection.connect();
InputStream input = connection.getInputStream();
bitmap = BitmapFactory.decodeStream(input);
}catch (SocketTimeoutException e){
cancelarHilo(3);
} catch (Exception e) {
Log.i("SMS", "ERROR AL OBTENER FOTO: " + e.toString());
bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.sin_perfil);
}
data.add(new MiModel(nombrejSON, categoriajSON, likesjSON, lemajSON, bitmap, idsjSON,latitudjSON,longitudjSON,distanciajSON));
code of onclick imageview:
public void Pressimage(final int position) {
customDiag = new Dialog(MainActivity.this);
customDiag.requestWindowFeature(Window.FEATURE_NO_TITLE);
customDiag.setContentView(R.layout.dialog);
RelativeLayout relativeLayout = (RelativeLayout)customDiag.findViewById(R.id.root);
Drawable drawable = new BitmapDrawable(getApplicationContext().getResources(),
data.get(position).getImagen());
if(Build.VERSION.SDK_INT > 16) {
relativeLayout.setBackground(drawable);
}else{
relativeLayout.setBackgroundDrawable(drawable);
}
customDiag.show();
}
Why would you want to do that?
Use Picasso or Volley as they will allow downloading/caching of images.
They also display images in listviews extremely well. This will mitigate your issue of attempting to reduce the memory burden by making your images look "bad".

How to load one image in android WITH OUT download

know anyone of you how can I use one photo from URL with out download? Because I can have a lot of images and if my application CANT download all of them...
I've found one function on web he how can do this but it don't work.
public static Drawable LoadImageFromWebOperations(String url) {
try {
InputStream is = (InputStream) new URL(url).getContent();
Drawable d = Drawable.createFromStream(is, "src name");
return d;
} catch (Exception e) {
return null;
}
}
I really need that, so have anyone any idea?
You can use Picasso lib for downloading image from url:
http://square.github.io/picasso/
Example:
Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView);

Downloading and showing images in Android

I want download some images from an android application, and then show these images.
I want know where I have to put my downloaded images and how to refer to them in order to show them, for example using them as background of a View object.
Here is an example of downloading an image from a URL and showing it in an ImageView.
This is the download method:
void downloadFile(String fileUrl) {
try{
InputStream is = (InputStream) new URL(fileUrl).getContent();
Drawable d = Drawable.createFromStream(is, "src name");
imgView.setImageDrawable(d);
} catch (IOException e) {
e.printStackTrace();
}
}

Why my Image from remote server appeared smaller?

I have an image in one of my Activities, and this is control by a remote server. My problem is, whenever I use this code, the image appears smaller on my phone but not in emulator. I want the image to appear just like its original size. Can someone tell me what I am doing wrong. Thanks in advance!
ImageView imgView =(ImageView )findViewById(R.id.image01);
Drawable drawable = LoadImageFromWebOperations("http://www.yourimage.com");
imgView.setBackgroundDrawable(drawable);
}
private Drawable LoadImageFromWebOperations(String url) {
try {
InputStream is = (InputStream) new URL(url).getContent();
Drawable d = Drawable.createFromStream(is, "src name");
return d;
}
catch (Exception e) {
System.out.println("Exc="+e);
return null;
}
}
Here is my xml:
<ImageView
android:id="#+id/image01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
you can try changing drawable to Bitmap with commant:
(BitmapDrawable)drawable).getBitmap())
and change the size of bitmap
b = Bitmap.createScaledBitmap(((BitmapDrawable)drawicon).getBitmap(), your_size_W, your_size_H, true);
and use: setImageBitmap to show in ImageView

Invoke the image directly from website to our app

I want to use the image directly from website. I did not place that image in drawable folder. For this purpose what can I do?
Please, give me some suggestions.
I've written a function that creates a Drawable from an image url, look below:
private Drawable downloadThumbnails(String url) {
Drawable d = null;
try {
InputStream is = (InputStream) this.fetch(url);
d = Drawable.createFromStream(is, "src_from_stream");
return d;
} catch (MalformedURLException e) {
} catch (IOException e) {
}
return null;
}
public Object fetch(String address) throws MalformedURLException, IOException {
URL url = new URL(address);
Object content = url.getContent();
return content;
}
then use it like this:
Drawable img = downloadThumbnails("http://yoururl.com/image.png");
yourImageView.setImageDrawable(img);
I strongly recommend using Picasso:
Picasso.with(context).load("http://an.url.com/lovely_cats.png").into(imageView);
It has a lot of usefull functions that allows you to:
crop the image
resize the image
rotate the image
fit an image into the target view
replace not downloaded file with local placeholder
an even more - mostly in a single line (or call).

Categories

Resources