How to load one image in android WITH OUT download - android

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);

Related

The most efficient way to show the local image in android

I am currently work on a magazine like apps. Since there is an option to zoom in(150%, 200%, 250% of original source) , I would prefer not to scale down the image. However , the app will force restart when I try to decode the image because of the out of memory. Are there any suggestion to fix that?
The image are local (SD card) , can be retrieve in any approach, but eventually need to be a bitmap as I use something like cavans.drawbitmap to display it. I tried, input stream-> bytes , input stream->bitmap etc... but are there any most efficient way or at least
I can sure the app does not force restart / close? Thanks
try {
InputStream is = (InputStream) new URL(url).getContent();
try {
return BitmapFactory.decodeStream(is);
} finally {
is.close();
}
} catch (Exception e) {
return BitmapFactory.decodeResource(context.getResources(), defaultDrawable);
}
You should consider using nostra's image loader, it deals with memory quite efficiently, you can specify lots of config stuffs, im using it and its working pretty well for large images aswell
This is what smartImageView(by loopj - you can find him on http://loopj.com/) uses to retrieve files from the drive/sd.
private Bitmap getBitmapFromDisk(String imgID) {
Bitmap bitmap = null;
if(diskCacheEnabled){
String filePath = getFilePath(imgID);
File file = new File(filePath);
if(file.exists()) {
bitmap = BitmapFactory.decodeFile(filePath);
}
}
return bitmap;
}

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();
}
}

Displaying Images parsed from JSOUP in 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;
}
}

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