Am trying to download an image using jsoup library and set it to image view but the downloaded image is cropped automatically. i don't know if it is the library or something else. Here is my code, Any one to help me please?
try {
// Connect to the web site
Document documentImage1 = Jsoup.connect(url).get();
// Using Elements to get the class data
Element img = documentImage1.select("div[class=media-img imgflare--river] img[src]").first();
// Locate the src attribute
String imgSrcImage1 = img.attr("src");
// Download image from URL
InputStream inputImage1 = new java.net.URL(imgSrcImage1).openStream();
// Decode Bitmap
image1 = BitmapFactory.decodeStream(inputImage1);
} catch (IOException e) {
e.printStackTrace();
}
Related
I have URL to PNG image file.
I want to get this image file and set it as a source for an ImageView.
My code:
URL iconURL = null;
try {
iconURL = new URL("https://maps.gstatic.com/mapfiles/place_api/icons/worship_general-71.png");
} catch (MalformedURLException e) {
e.printStackTrace();
}
Bitmap icon = null;
try {
icon = BitmapFactory.decodeStream(iconURL.openConnection().getInputStream());
} catch (IOException e) {
e.printStackTrace();
}
myImageView.setImageBitmap(icon);
If I place a breakpoint into the line with decodeStream() the app stops and I see the following:
If I run the app in regular mode, it just stops!
Can someone explain what is wrong here and how to do it correctly?
Thanks.
If you just want to show the image on your ImageView then you can use Picasso Library.
Put below dependency in build.gradle file
'com.squareup.picasso:picasso:2.71828'
And use below java code for loading your image from URL to your ImageView
Picasso.get()
.load(url)
.into(imageView);
I'm using this library to display my gif files. When my gif files are saved in my device storage it displays the gif file okay, by using
GifDrawable gifFromPath;
File gifFile;
GifImageView gifImageView = (GifImageView) findViewById(R.id.imageHome);
gifFile = new File(context.getFilesDir().getAbsolutePath(), imageName);
try {
gifFromPath = new GifDrawable(gifFile);
} catch (IOException e) {
e.printStackTrace();
}
gifImageView.setImageDrawable(gifFromPath);
I'm wondering how can I display the gif file if instead of using the path where it is saved I want to get the gif from a link / url.
I use this library. And I use ImageView instead.
ImageView myImageView = (ImageView) findViewById(R.id.myImageView);
Glide.with(this).load(url).into(myImageView);
I want to set the title when i encode a Web URL. I Use QRCodeEncoder class to encode the contents. I don't find anything to set title for the web URL. I use below code to encode the input text field content and generate image.
// check validation for input string
if (URLUtil.isHttpUrl(inputWebURL)) {
QRCodeEncoder qrCodeEncoder = new QRCodeEncoder(
inputWebURL, null, Contents.Type.TEXT,
BarcodeFormat.QR_CODE.toString(), smallerDimension);
try {
bitmapCreatedImage = qrCodeEncoder.encodeAsBitmap();
ImageView myImage = (ImageView) findViewById(R.id.imageViewCreatedWebUrl);
myImage.setImageBitmap(bitmapCreatedImage);
} catch (WriterException e) {
e.printStackTrace();
}
}
How I set Title for the Web URL QR encoding?
Thanks.
Hi all i want to display video thumbnail with rectangle shape as in given link .I have used some code and now video is displaying in thumbnail but not with rectangle as display in given link.So please suggest me, is this is possible in android or not .If possible then how..Thanks..
http://lh3.ggpht.com/_xBHTh_QdoYY/S81nyg57zsI/AAAAAAAAA2A/knYE08lHypU/video3e312550f115%5B15%5D.jpg
Use this code to get the thumbnails-
Bitmap _bitmap = null;
thumbnail = new ImageView(mContext);
thumbnail.setScaleType(ImageView.ScaleType.CENTER_CROP);
try {
String path = "SOME PATH GOES HERE...";
_bitmap = ThumbnailUtils.createVideoThumbnail(path, MediaStore.Video.Thumbnails.MICRO_KIND);
thumbnail.setImageBitmap(_bitmap);
} catch (Exception e) {
thumbnail.setBackgroundResource(R.drawable.icon);
e.printStackTrace();
};
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).