Bitmap from url without scaling - android

i am getting bitamap from a url. Everything is fine but i m having small issue i.e my bitmap from url gets scales down. I want bitmap of original size without scaling. How to achieve this. I tried options but no success yet. here is my code:
try {
URL url = new URL(src);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
return myBitmap;
} catch (IOException e) {
e.printStackTrace();
return null;
}
I set this drawable to imagview. But image is scaled down. I want original size and original size is not too big so need to worry about outofmemory exception. Is there any way to avoid scaling while parsing bitmap from url stream.
Thanks.

Directly show image from web without downloading it. Please check the below function . It will show the images from the web into your image view.
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;
}
}
then set image to imageview using code in your activity. Maybe this will help you.

Related

Andorid BitmapRegionDecoder(For cutting large images into rectangular pieces) code not working properly

i need help to understand BitmapRegionDecoder and how to use. I am not able to figure out how to calculate the x and y of screen to cut the image to make grid. I want to cut a specific grid on image click. how can i do it. This image is large and normally the image size vary based on different situation.
PFB code which is not working and throwing Failed to decode region exception:
try {
InputStream in = null;
URL url = new URL("http://www.libpng.org/pub/png/img_png/pnglogo-blk.png");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
try
{
in = new BufferedInputStream(urlConnection.getInputStream());
}catch (Exception e){
e.printStackTrace();
}
BitmapFactory.Options options = new BitmapFactory.Options();
BitmapRegionDecoder regionDecoder = BitmapRegionDecoder.newInstance(in, false);
Bitmap img = regionDecoder.decodeRegion(new Rect(100,100,100,100), options);
imageView.setImageBitmap(img);
} catch (IOException e) {
e.printStackTrace();
}
Try this:
URL url = new URL("http://www.libpng.org/pub/png/img_png/pnglogo-blk.png");
Bitmap bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
imageView.setImageBitmap(bmp);

How do i set relative layout background image from URL using Volley or something else?

I want to set layout background image from web URL,
either using volley or Picasso or some else
is there any way to set this!
I found below code this from web but not working !!
Bitmap myImage = getBitmapFromURL("http://looksok.files.wordpress.com/2011/12/me.jpg");
RelativeLayout rLayout=(RelativeLayout)findViewById(R.id.relativeLayout);
//BitmapDrawable(obj) convert Bitmap object into drawable object.
Drawable dr = new BitmapDrawable(myImage);
rLayout.setBackgroundDrawable(dr);
public Bitmap getBitmapFromURL(String imageUrl) {
try {
URL url = new URL(imageUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
return myBitmap;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
Please try this Android Smart Image View library.
http://loopj.com/android-smart-image-view/
I use the below link for set image from url:
https://android-arsenal.com/details/1/211
If you are using android studio then put compile 'com.squareup.picasso:picasso:2.5.2' in your build.gradle file and compile the project.
And then you can use Picasso library
Picasso.with(mContext)
.load("http://looksok.files.wordpress.com/2011/12/me.jpg")
.into(rLayout);
This will load image from given url into rLayout.
Reference : http://square.github.io/picasso/

Where Image gets stored in Android phone when i use Universal Image Loader library?

I am using:
ImageLoader.getInstance().displayImage(imageUrls.get(position), holder.ivPhoto, options);
Where imageUrls is a ArrayList of String which is the urls list from where the images are loaded and holder contains the ImageView. To share this image i want to use this Intent But i don't know the location of the image saved. I have searched on Stack Overflow but can't fine the appropriate solution, or If there is any other way to share the image that will very helpful.
You can download the image from url
public Bitmap getBitmapFromURL(String imageUrl) {
try {
URL url = new URL(imageUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
return myBitmap;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
OR
You can add your own image loader class eg : link
you will find DownImageLoader.java class in the constructor a cache directory is created where you will get all your files
I think you are using the wrong library. UIL library is used for caching the image. It is not used for downloading the image and storing it in sdcard.
You can refer this link for downloading the image and retreiving the image path.
http://www.oodlestechnologies.com/blogs/Downloading-and-Retrieving-Files-on-SD-card-in-Android-using-Android-SDK-in-Eclipse

set url image to image view [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Is it possible to use BitmapFactory.decodeFile method to decode a image from http location?
i have a problem in set url image to image view. i tried below methods
Method 1:
Bitmap bimage= getBitmapFromURL(bannerpath);
image.setImageBitmap(bimage);
public static Bitmap getBitmapFromURL(String src) {
try {
Log.e("src",src);
URL url = new URL(src);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
Log.e("Bitmap","returned");
return myBitmap;
} catch (IOException e) {
e.printStackTrace();
Log.e("Exception",e.getMessage());
return null;
}
}
Method 2:
Drawable drawable = LoadImageFromWebOperations(bannerpath);
image.setImageDrawable(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;
}
}
i tried above two methods but are not working . both are showing DEBUG/skia(266): --- decoder->decode returned false . i used demo url image paths that is working. but this path is not working .so please tell me what is the wrong and what i will do
Thank you in advance.
Best Regards.
Just Tested your "Method 1" in my app and it works just fine.
You might have forgotten this line in your AndroidManifest.xml file:
<uses-permission android:name="android.permission.INTERNET" />

how to display external image in android?

I want to display external image like:
"http://abc.com/image.jpg"
in my android phone application.
can any one guide me how to achieve this?
There are many ways to achieve your request. Basically you have to download the image with an urlrequest and then using the InputStream to create a Bitmap object.
Just a sample code:
URL url = new URL("http://asd.jpg");
URLConnection conn = url.openConnection();
conn.connect();
InputStream is = conn.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
Bitmap bm = BitmapFactory.decodeStream(bis);
bis.close();
is.close();
After you obtain the Bitmap object you can use it on your ImageView for instance
Just another approach to download the image from a url
try {
Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL("http://abc.com/image.jpg").getContent());
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

Categories

Resources